Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this JS function named as "myjsfunction()".

function myjsfunction() {

jQuery('#html_admin_show').hide();
jQuery('#html_admin_edit').show();

}

One limitation is that I cannot edit the original JS function like to put some PHP tags, etc.

How is it possible to call this JS function inside a Wordpress do_action hook?

I'm preferring to add it as third parameter because I'm already using the second parameter, so something like:

do_action('myhelp', 'mysecondfunction', 'myjsfunction');

But it is not showing the element #html_admin_edit when the page runs. Any ideas how to call this function?

Thanks.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

First, your callback must be PHP:

function myjsfunction_callback() {
?>
<script>myjsfunction();</script>
<?php
}

Second, you can add multiple callbacks to one action:

add_action( 'myhelp', 'myjsfunction_callback' );
add_action( 'myhelp', 'mysecondfunction' );

Now, you can call do_action() without parameter …

do_action( 'myhelp' );

… and both callbacks will be executed.


I reply to your comment: You can use just one callback:

do_action( 'myhelp', 'mysecondfunction', 'myjsfunction_callback' );

You have to use a rather abstract callback then, and accept more parameters:

add_action( 'myhelp', 'multicall', 10, 99 );

function multicall()
{
    $functions = func_get_args();

    foreach ( $functions as $function )
    {
        if ( function_exists( $function ) )
            $function();
    }
}

Now you can pass up to 99 function names, and all of them will be executed for that action.

share|improve this answer
    
Thanks, this one will work but I cannot edit the JS function because it belongs to a third party plugin. Any ideas how to make the callback in PHP? –  Codex Meridian Jan 10 '13 at 3:42
    
@CodexMeridian See my update. :) –  toscho Jan 10 '13 at 3:43
    
Thanks Toscho, I will try this one. –  Codex Meridian Jan 10 '13 at 3:47
    
One problem I noticed is that I only want to have one line of do_action with the myjsfunction_callback as the third parameter, In your example, you have a do_action without a parameter. can you give me an example of a do_action above using myjsfunction_callback as the third parameter? Thanks. –  Codex Meridian Jan 10 '13 at 5:15
    
@CodexMeridian See my update. –  toscho Jan 10 '13 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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