0

I try to optimize a bit and i want to load some shortcodes only if they are found in global $post

Unfortunately its not working, the script is not loaded when i open a page which contains the shortcode.

    function enqueue_script_if_shortcode_is_detected() {
        global $post;

        wp_register_script( 'leweb-widgets', get_stylesheet_directory_uri() . '/includes/leweb-widgets.php', '1.0', true );

        $pattern = get_shortcode_regex();

        if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
            && array_key_exists( 2, $matches )
            && in_array( 'leweb_suggested_events', $matches[2] )
        ) {
            wp_enqueue_script('leweb-widgets');
        }
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_script_if_shortcode_is_detected' );
  • By the time shortcodes run, all the scripts have been enqueued and printed out in the header already. When things are enqueued it's usually too early. Instead try to do this the other way around and enqueue things inside your shortcode, that way they'll at least be in the footer – Tom J Nowell Nov 1 '19 at 1:31
  • i try to enqueue the whole shortcode function and action php file. So i cant enqueue the script in the shortcode, because the script IS the shortcode, or am i wrong? – LovinQuaQua Nov 1 '19 at 1:48
  • A shortcode is just a way to put stuff in post content that's more complex than basic tags, e.g. embeds or basic forms. In the future it's made obsolete by blocks. It isn't a tag or a piece of meta data. If you need to enqueue something so that your shortcode works, enqueue it in your shortcode, or enqueue it everywhere. But if you're using shortcodes to change the rest of the template, as markers, etc, that's not what shortcodes are for. – Tom J Nowell Nov 1 '19 at 3:11
1

There's already a function for checking if a post has a shortcode: has_shortcode():

function enqueue_script_if_shortcode_is_detected() {
    global $post;

    wp_register_script( 'leweb-widgets', get_stylesheet_directory_uri() . '/includes/leweb-widgets.php', '1.0', true );

    if ( has_shortcode( $post->post_content, 'leweb_suggested_events' ) ) {
        wp_enqueue_script('leweb-widgets');
    }
}
add_action( 'wp_enqueue_scripts', 'enqueue_script_if_shortcode_is_detected' );

Just keep in mind that checking the post this way:

  1. Will not know if the shortcode is used in a widget or custom field.
  2. Will not work properly on archive pages.
  3. Is using the global $post variable, which should be avoided where possible. Especially outside the loop.

I'd advise checking is_singular() and using get_queried_object() to get the current post.

function enqueue_script_if_shortcode_is_detected() {
    wp_register_script( 'leweb-widgets', get_stylesheet_directory_uri() . '/includes/leweb-widgets.php', '1.0', true );

    if ( is_singular() ) {
        $post = get_queried_object();

        if ( has_shortcode( $post->post_content, 'leweb_suggested_events' ) ) {
            wp_enqueue_script('leweb-widgets');
        }
    }
}
add_action( 'wp_enqueue_scripts', 'enqueue_script_if_shortcode_is_detected' );

But if your script is loaded in the footer, which yours is, you'll get more reliable results if you add wp_enqueue_script('leweb-widgets'); to the shortcode callback. This way you can be sure the script will be loaded whenever the shortcode is used, no matter where it's used.

  • thanks, but its not working with your script – LovinQuaQua Nov 1 '19 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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