WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
May I ask you why you want dynamic functions? What content will you put there when you don't know how many functions you need? – Rilwis Sep 1 '12 at 8:11
Which solution did you use? – Alvaro Feb 23 '14 at 11:17

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
share|improve this answer
create_function() should never be used. It breaks object caches and it is hard to debug (and to read …). – toscho Sep 1 '12 at 12:35
Thanks toscho for this precision, I never use this function but I believed it would be practical here. I will continue not to use it then. :) – Mathias Sep 2 '12 at 6:21

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;

But I think you need to find a better architecture.

share|improve this answer

Your Answer


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.