0

I did a plugin who call enqueue style inside a shortcode. But when i add my shortcode in a text widget the enqueue style are not included en the page.

wp_enqueue_style('css-rfnb', RFNB_PLUGIN_DIR_URL . 'css/rfnb.css');

1 Answer 1

1

To properly include style you need to do it with wp_enqueue_scripts action.

With a shortcode, you need to include the file differently as the header has already been send.

One way, Can be to include file in the shortcode. Other way, in the wp_enqueue_scripts callback function can be to scan the_content, detect shortcode and include the style like you've done.

If you make your own plugin, you can enqueue the style on each post conditionnaly with has_shortcode

function custom_shortcode_scripts(){
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-shortcode') ) {
        wp_enqueue_script( 'custom-script');
    }
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');
1
  • Hi, i already have this code working on my plugin, but if i put my shortcode in a text widget, It do not call the enqueue files.
    – Gino
    Commented Dec 19, 2016 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.