1

I am developing a plugin and using do_action() instead of shortcodes to display plugin content on the site. The plugin will only be used by me, but I am making the plugin to separate components of the site from the theme itself.

How would I go about conditionally displaying javascript and css on a page, IF a specific do_action is called in a theme, from the plugin?

I have found a ton on shortcodes but I wanted to avoid using shortcodes, as I will be calling the actions directly in the theme.

I tried something like this

add_action('wp_footer', 'print_my_script');
function print_my_script() {
    if(did_action('my_custom_action')) {
        wp_enqueue_scripts('listings');
        wp_enqueue_scripts('isotope');
    }
}

edit: its wp_enqueue_script() not scripts and changing that solved my problem

but it did not work. I also looked at Scribu's article on the subject for shortcodes and use globals, but that still did not work.

I suspect that I might be able to rearrange priorities and make this work, but I am not sure.

  • 1
    have you tried the correct function - wp_enqueue_script? – Milo Jun 25 '14 at 1:19
  • @Milo wow. I had taken this code from Scribus article which used wp_print_scripts() and changed print to enqueue, and stopped there. thank you! I am in shock that this worked. – Jacob Raccuia Jun 25 '14 at 1:25
1

And.... the answer was actually correct, if you are using the correct functions. As Milo pointed out, it's wp_enqueue_script not scripts.

add_action('wp_footer', 'print_my_script');
function print_my_script() {
    if(did_action('my_custom_action')) {
        wp_enqueue_script('listings');
        wp_enqueue_script('isotope');
    } 
}

Hopefully this helps someone else in need!

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.