need help looping add_action in wp

i would like to know the proper way to do this

$dynValue=5;//loop this many times
//start of my loop
for ($num=1; $num <= $dynValue; $num++){

//note the incremented number ($num) in the call back
add_action('add_meta_boxes', 'mpc_meta_box_'.$num.');

//below is where im having the problem
// i am trying to increment this function name so i will not get the error that "function name has already been declared". i cannot add '.$num.' in the func name below cause its not valid php.
function mpc_meta_box_'.$num.'(){
//some content
}

}//end of my loop

i can do this with eval() but i know its not recommeneded

Related posts

Leave a Reply

2 comments

  1. You can try something like this:

    $dynValue=5;//loop this many times
    //start of my loop
    for ($num=1; $num <= $dynValue; $num++){
        add_action('add_meta_boxes', create_function('', '//some content'));
        // Write the php code as if it was to put it in an echo/print instruction.
        // More details: http://php.net/manual/en/function.create-function.php
    }//end of my loop
    
  2. Use always the same function as callback. You may use a static variable inside of the function to track how often it was called:

    for ($num=1; $num <= $dynValue; $num++) 
    {
        add_action( 'add_meta_boxes', 'mpc_meta_box' );
    }
    
    function mpc_meta_box()
    {
        static $counter = 1;
        echo $counter;
        counter++;
    }
    

    But I think you need to find a better architecture.