WordPress custom meta box when adding/editing a page with certain template type

I know how to add custom fields to pages just fine. However, I would like my meta box to only appear once the user has selected the template “Foo” (filname “foo.php”). I have jQuery loaded on the admin section but I have had no luck in trying to add a custom class to the container that holds the meta box.

Note: I do not want to select by the ID of the meta box div because there are going to be several hidden meta boxes and I will have no way of hiding all of them at once without a class assigned (hence my problem).

Related posts

Leave a Reply

2 comments

  1. This is not a complete solution, but might get you started in the right direction:

    In the code which are displaying the meta box, you can use the get_post_meta function to get information about which template the current page is using. Something like this:

    if( get_post_meta($post_id, '_wp_page_template', true) != 'foo.php' ) return;
    

    The downside of this is that the user must choose template first, then save the page once in order to make the admin page reload with the right settings.

    Otherwise I think the solution you describe using jQuery somehow to hide the meta boxes is the best way to do it.

    Good luck