0

I'm developing a custom plugin that uses a shortcode to display content on specific pages. Locally I had the Twenty Twenty-Four theme activated for development. While, in production, I'm using Scapeshot Wedding theme, a child theme of Scapeshot. I have some scripts and css files that I need to enqueue for everything to work and look nice. Once I was done and happy I decided to publish to production, where nothing worked. I found out that none of the enqueued scripts and styles actually make it to the DOM when Scapeshot (Wedding) is activated.

The same happens when I activate Scapeshot (Wedding) locally. I have written a simple script to test:

<?php
/*
Plugin Name: Test Plugin
Description: A minimal plugin to test hooks.
Version: 1.0
Author: Test Author
*/

// Log after the header or footer
add_action('wp_head', function () {
  global $wp_scripts, $wp_styles;
  foreach ($wp_scripts->queue as $handle) {
      error_log('Script Enqueued: ' . $handle . ' (hook: wp_footer)');
  }
  foreach ($wp_styles->queue as $handle) {
      error_log('Style Enqueued: ' . $handle . ' (hook: wp_footer)');
  }
});

add_action('wp_enqueue_scripts', function(){
  error_log('Outside shortcode enqueue action');
});

// Add Shortcode for Registration Form with Nonce
function test_shortcode()
{
    error_log('test_shortcode');
    function enqueue_styles_and_scripts()
    {
        error_log('Enqueue1');
    }
    add_action('wp_enqueue_scripts', 'enqueue_styles_and_scripts', 0);

    // Enqueue JavaScript
    function enqueue_custom_registration_scripts()
    {
        error_log('Enqueue2');
    }
    add_action('wp_enqueue_scripts', 'enqueue_custom_registration_scripts', 0);   
}
add_shortcode('custom_registration_form', 'test_shortcode');

The resulting log with Twenty Twenty-Four theme, where both test_shortcode, Enqueue1 and Enqueue2 are being logged:

[10-Jan-2025 20:59:34 UTC] test_shortcode
[10-Jan-2025 20:59:34 UTC] Enqueue1
[10-Jan-2025 20:59:34 UTC] Enqueue2
[10-Jan-2025 20:59:34 UTC] Outside shortcode enqueue action
[10-Jan-2025 20:59:34 UTC] Script Enqueued: admin-bar (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Script Enqueued: wp-block-template-skip-link (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: admin-bar (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-site-logo (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-site-title (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-group (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-page-list (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-navigation (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-template-part (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-spacer (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-post-title (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-post-featured-image (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-shortcode (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-paragraph (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-post-content (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-site-tagline (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-column (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-heading (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-navigation-link (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-columns (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-pattern (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-emoji-styles (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-library (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: global-styles (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: core-block-supports (hook: wp_footer)
[10-Jan-2025 20:59:34 UTC] Style Enqueued: wp-block-template-skip-link (hook: wp_footer)

And the Scapeshot Wedding theme activated, where none of the enqueues are hooked:

[10-Jan-2025 20:58:44 UTC] Outside shortcode enqueue action
[10-Jan-2025 20:58:44 UTC] Script Enqueued: admin-bar (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Script Enqueued: scapeshot-html5 (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Script Enqueued: scapeshot-skip-link-focus-fix (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Script Enqueued: scapeshot-script (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: admin-bar (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: wp-emoji-styles (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: wp-block-library (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: wp-block-library-theme (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: classic-theme-styles (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: global-styles (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-style (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-wedding-style (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-wedding-block-style (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-fonts (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-block-style (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] test_shortcode
[10-Jan-2025 20:58:44 UTC] Outside shortcode enqueue action
[10-Jan-2025 20:58:44 UTC] Script Enqueued: admin-bar (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Script Enqueued: scapeshot-html5 (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Script Enqueued: scapeshot-skip-link-focus-fix (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Script Enqueued: scapeshot-script (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: admin-bar (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: wp-emoji-styles (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: wp-block-library (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: wp-block-library-theme (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: classic-theme-styles (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: global-styles (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-style (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-wedding-style (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-wedding-block-style (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-fonts (hook: wp_footer)
[10-Jan-2025 20:58:44 UTC] Style Enqueued: scapeshot-block-style (hook: wp_footer)

What are potential causes for this? Where in the theme files could I investigate further?

3
  • 1
    in your log you can see that the action wp_enqueue_scripts is thrown before the call of the shortcode. to conditionally load a script, you can use has_shortcode in the action wp_enqueue_scripts. Commented Jan 11 at 7:56
  • That's a very good work-around! Thank you! Feel free to provide it as an answer, else I will for posterity. Commented Jan 11 at 13:40
  • 1
    has_shortcode is pretty resource intensive. Couldn't you just register the script\style and then in the shortcode function enqueue your registered script. See this answer wordpress.stackexchange.com/a/191512/57589 Commented Jan 11 at 18:49

1 Answer 1

0
function my_register_scripts() {
    wp_register_script('footag-shortcode',get_stylesheet_directory_uri() . '/assets/js/example.js');
}
add_action( 'wp_enqueue_scripts', 'my_register_scripts' );


function my_register_footag_shortcode( $atts = [], $content = '' ) {
    $atts = shortcode_atts( [
        'foo' => 'no foo',
    ], $atts, 'footag' );

    wp_enqueue_script( 'footag-shortcode' );
}
add_shortcode( 'footag', 'my_register_footag_shortcode' );

You need to register the script as per this. After that, you can enqueue this script within your shortcode.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.