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

In my theme, i want to create a generic way of inserting a meta box to a post, page, portfolio or other custom item.

For now, this action is like :

add_action('admin_init', 'add_portfolio_settings');

Which works fine, but has a problem. It currently works only for creating an add_meta_box for a portfolio item. However, what if i wanted to add the same to a post or page ?

So, i would like to have add_action passing an argument to my add_portfolio_settings function, ideally an array, that specifies what the types that a new meta box should register under.

I've read stuff and tried different things about do_action, but i cannot get it to work. Any ideas ?

share|improve this question
TIP: Metaboxes should be added or removed on the add_meta_boxes or add_meta_boxes_$type action, it's why those 2 actions exist. – t31os Jul 18 '11 at 9:15
up vote 1 down vote accepted

What you want is quite possible, but not exactly mainstream technique. Take a look at my Implementing advanced add_* function wrappers question.

However your real issue here is not being unable to pass arguments. It is lack of context.

  1. The simplest way to track context is inside your hooked function (check what page you are at, do something accordingly).
  2. Slightly more complex is adding your own dynamic hook (Post Status Transirions are great example).
  3. Even more complex is contextual hook.
share|improve this answer

Basically that's how action and filter hooks work, with predefined parameters and arguments.

You can get around that by creating your own option using add_option() where you can save the types for the metabox and in your add_portfolio_settings() call that option using get_option() and there you have your array.

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.