After looking for days throught the net and no avail - i'd like to ask all of you guys.

PROBLEM: i need to add custom code to the head of the file as part of shortcode use for my plugin. The same files and code insert fine in the admin page (backend) but fail to load in the template file.

The files are inserted in the admin head with add_action('admin_print_scripts', 'add_swm_scripts'); & add_action('admin_print_styles', 'add_swm_styles'); calls pointing to functions that use wp_enqueue_script() and wp_register_style() / wp_enqueue_style() This does fine - however - when i try to get the same files registered in the function of the shortcode (for use in the template), nothing workds.

I checked that the shortcode is read and it's according function is fired, but neither add_action( 'wp_enqueue_scripts', 'swm_scripts' ); , add_action('init', 'register_swm_scripts'); nor add_action('wp_head', 'print_swm_scripts'); will work. I've tried more than this not don't find a working solution. If somebody knows a valid trick - i more than appreciate some advice.

Thanks in advance!


  • The question is sort of answered. add_action calls are not possible from within the shortcode handler. Thanks helgatheviking – herbie Mar 13 '12 at 2:44

put wp_enqueue_script() in your shortcode handler.

add_shortcode('myshortcode', 'my_shortcode_handler');

function my_shortcode_handler($atts) {
  wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);

  // actual shortcode handling here
  return "bacon flavored shortcode here";

this definitely loads the my-script.js file for me. it gets loaded in the footer though. i don't think there is a way for it to load conditionally in the header as it won't have gotten to the post content to know whether the shortcode is used or not.

check this: http://scribu.net/wordpress/conditional-script-loading-revisited.html

  • i do and still nothing. the plugin main page holds the add_shortcode and refers to a function in my functions file. it gets called and also errors when messed with but the add_action calls do nothing... – herbie Mar 12 '12 at 18:35
  • 2
    It is helpful to copy relevant example code into the WPSE answer, in case the linked resource goes away in the future. – Chip Bennett Mar 12 '12 at 18:35
  • could it be that the theme supresses the calls? – herbie Mar 12 '12 at 19:03
  • I tried the method from the link you posted: add_shortcode('myshortcode', 'my_shortcode_handler'); function my_shortcode_handler($atts) { wp_enqueue_script('my-script', plugins_url('my-script.js', FILE), array('jquery'), '1.0', true); // actual shortcode handling here } but it won't do anything... In the same article it is mentioned that calls from within the body won't work - but the shortcode is only used in posts - that seems like a contradiction. – herbie Mar 12 '12 at 19:47
  • ChipBennett you're right. done. but it'd be a sad day for WP if scribu's site goes away! herbie - there is no add_action call inside your shortcode. i just tested and the code in my answer works. i also verified that you don't even need to specific that last true parameter (which means load in footer) as it will automatically be added in the footer – helgatheviking Mar 12 '12 at 21:43

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.