I added/enqueue a style inside shortcode, it works fine but loaded in footer (before starting the .js files) rather than header. I did the same way like this solution:
Enqueue Scripts / Styles when shortcode is present
Is it normal in WP or how can I load style in header ?
Sample Code Here:
class Cbwsppb_Related {
public function __construct()
{
add_shortcode('cbwsppb_related', array($this, 'shortcode_func'));
}
public function shortcode_func($atts, $content = null)
{
$atts = shortcode_atts( array(
'style' => '',
'display_style' => '',
'show' => '',
'orderby' => ''
), $atts );
if ($atts['display_style'] === 'carousel') {
wp_enqueue_style('flexslider-style');
}
$show = (!empty($atts['show'])) ? $atts['show'] : 2;
$orderby = (!empty($atts['orderby'])) ? $atts['orderby'] : 'rand';
$output="";
ob_start();
if ($atts['style'] === 'custom') {
$output .= woocommerce_get_template( 'single-product/related.php', array(
'posts_per_page' => $show,
'orderby' => $orderby,
)
);
} else {
$output .= woocommerce_get_template( 'single-product/related.php');
}
$output .= ob_get_clean();
return $output;
}
}