Looks like the best way is using:

add_action('wp_footer', returnJs);

But if my returnJs function is inside my shortcode function and it's used more than once on a page it will return an error trying to re-create the function.

Is there a way to use add_action and have my js code in a variable instead of wrapped in a function or maybe another way to include it.

It is dynamically generated based on a variable in the short code so the js will be unique for each shortcode.

Thanks in advance.


1 Answer 1


What you need is the function_exists method

you would use it like this when defining your returnJs function:

if( ! function_exists('returnJs') ){
    function returnJs(){
        //Code goes here

However, better practice would be to just define the returnJs function in your plugin

Your Answer

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

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