1

How can I use do_action and add_action to return an array in do_action?

My sample code:

function name_fun_one(){
    $namearray[] = array('k1'=> 'text1', 'k2' => 'text1');
    $namearray[] = array('k1'=> 'text2', 'k2' => 'text2');
    do_action('add_in_namearray');
    foreach($namearray as $val)
    {
        //loop 
    }
}

function add_name_fun_one()
{
    $namearray[] = array('k1'=> 'text3', 'k2' => 'text3');
    $namearray[] = array('k1'=> 'text4', 'k2' => 'text4');
}
add_action('name_fun_one', 'add_name_fun_one');
  • 1
    Please edit your question and explain what you want to achieve with more words, examples etc. It's not clear. – Fayaz Mar 15 '17 at 5:06
0

If you want to make the array filterable, use apply_filters() and add_filter(). Also, you have to pass the array as an argument to apply_filters() and expect it in your callback that is changing that array.

Here is a basic example with slightly more meaningful names:

First we have a function that is running over an array of color names and values in order to print these with a styled example. It offers a hook, so other code (theme, plugins) can change the colors: apply_filters( 'arr_colors', $colors );.

function print_colors()
{
    $colors = [
        'red'   => '#f00',
        'green' => '#0f0',
        'blue'  => '#00f',
    ];

    $colors = apply_filters( 'arr_colors', $colors );

    foreach ( $colors as $name => $color ) {
        printf(
            '%1$s: <span style="background: %2$s;>&nbsp;</span><br>',
            $name,
            $color
        );
    }
}

Now we create a callback function elsewhere that can change the color. Note the check for isset ( $colors['green'] ): There may be more than one callback listening on that hook, so green could have been removed already before our callback is running. Always keep that in mind.

/**
 * @param  array $colors
 * @return array
 */
function change_colors( array $colors )
{
    // remove red
    unset( $colors['red'] );

    // soften the green
    if ( isset ( $colors['green'] ) ) {
        $colors['green'] = '#5f5';
    }

    // add fuchsia
    $colors['fuchsia'] = '#f0f';

    return $colors;
}

And now we register said callback for the hook in the first function:

add_filter( 'arr_colors', 'change_colors' );

That's all.

0

Your question is a little bit tough to understand. I think you want this.

Define the hook:

function sample_hook() {

    $namearray[] = array('k1'=> 'text1', 'k2' => 'text1');
    $namearray[] = array('k1'=> 'text2', 'k2' => 'text2');

    do_action('sample_hook',$namearray);
    //you can add multiple args in do_action hook like do_action('sample_hook',$arg1,$arg2,$arg2); 

}

Attach your function to the hook using add_action:

add_action('sample_hook', 'sample_function');

function sample_function($namearray) {
    print_r($namearray);
  echo 'Hey, that is amazing.';
}

Don’t forget to add the following line of code to your page, wherever you want the content to be displayed:

sample_hook();

If you want to return array this code must be work. Enjoy!!!

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.