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)
wp_enqueue_scriptsis thrown before the call of the shortcode. to conditionally load a script, you can use has_shortcode in the actionwp_enqueue_scripts.has_shortcodeis 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