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 ?

  • 1
    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

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.

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.


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.