This brief guide will show you how to add the default Thesis Theme meta boxes to your custom post types. Use all the Thesis goodness with your custom posts.
Setting up a custom post type is pretty straight forward, for more information see the WordPress codex, again Justin Tadlock also has a great primer on custom post types.
The following example will add a custom post type of “Locations” (this could be anything really: books, press-releases, faqs, products, etc) … add the following to your custom_functions.php
file:
Additionally, do have a look at the register_post_type()
documentation for all the juicy details.
Don’t Forget To Flush
This is Important (how very true…), after you add a custom post type you must do one of two things for the rewrite rules to start taking affect (so you can begin testing your custom post types and not get 404 headers).
WordPress will NOT auto refresh it’s rewrite rules on its own, you will have to do this by:
- Going into Settings > Permalinks and click the Save Changes button (this is the easiest way), or…
- Use code to call
flush_rules()
(just once)
I recommend you do the first option, here is what WordPress has to say about the latter:
Keep in mind that the flush_rules function is quite slow, so in practice you never want to call it from the init filter that gets executed on each page load. Instead, call this function only when the rewrite rules change. If the rules get set up and then never change, it is enough to flush in register_activation_hook of your plugin.
So… What Does This Have To Do With Thesis?
By default Thesis will only add its custom meta boxes (SEO Details and Additional Style, Post Image and Thumbnail, Javascript and Multimedia Box Options) to “post” and “page” types. After adding your own custom post type, use the following to make the default Thesis meta boxes appear by also adding this to your custom_functions.php
file:
add_action('admin_menu','my_admin_menu');
function my_admin_menu()
{
$post_options = new thesis_post_options;
$post_options->meta_boxes();
foreach ($post_options->meta_boxes as $meta_name => $meta_box)
{
add_meta_box($meta_box['id'], $meta_box['title'], array('thesis_post_options', 'output_' . $meta_name . '_box'), 'location', 'normal', 'high'); #wp
}
add_action('save_post', array('thesis_post_options', 'save_meta')); #wp
}
Note: Once the latest version of Thesis is released, it will more than likely address this issue.
Umm, It’s Not Working
If you copy and paste the above code and then make changes, be sure that you adjust line 58 with the custom post type name that you used on line 45 (in the above example: location
is the name).
Hmm for some reason it didn’t add the thesis default meta boxes…
Justin, make sure that you adjust line 58 with the custom post type “name” that you added on line 45:
What a laughable error on my part – I completely missed the first chunk of code; I read your comment – line 45. Wait, there’s no line 45.. What’s going on here??
Thanks Dimas!
Actually I’m really having a tough time getting the hang of writing custom posts and inserting fields in these posts… I’m trying to create meta boxes that show up on custom post types. I saw your other tutorial on custom posts + meta boxes but am having a tough time following – do you have a code that I could implement in thesis?
Take a look at my WPAlchemy Meta Box PHP Class, download the code and you will find a good example on setting up meta boxes. The class is an attempt at making meta box setup really easy.
Will do! Thank you so much! If you have a minute and want to take a look over my code, I attempted to follow the “how to create a wordpress custom meta box” tutorial in conjunction with the “how to limit the display of a meta field” and also tried to incorporate this article –
here’s my error message:
Warning: call_user_func(my_meta_setup_3) [function.call-user-func]: First argument is expected to be a valid callback in /home/events/public_html/wp-admin/includes/template.php on line 2868
& here’s my custom_functions.php code: http://pastie.org/1043751
Great stuff. This worked perfectly for moi. Stumbled the article. 🙂
I used a plugin called Custom Post Types UI to create my custom post type. The plugin makes it very easy to manage custom post types. I waited to see if Chris added a way to have Thesis meta boxes in 1.8 but he just released it this morning and I’m not seeing the meta boxes. So, I added your code to add the custom meta boxes to my custom post type and it worked out great. Thanks for the tip Dimas!
I tried your code to add the Thesis Metabxes to my custom Posts. So far it worked like a charm.
But how can i make non Thesis Metaboxes (from plugins for exp.) show up in my custom post eidt Screen too???
Any Help Welcome
Thanx Sascha
Sascha, take a look at my WPAlchemy MetaBox helper …
Hey. Thanks for the tip / code.
I’ve added the code for the meta boxes on a custom post type on my development site. The meta boxes appear just as expected.
Unfortunately, changes to any of the added meta boxes do not save.
I’ve tracked the problem down to the save_meta() function in options_post.php. The data is not saving because it fails the nonce check (lines 146-150 of options_post.php)
I know it fails there because I commented out that particular section for testing and the data saves fine when it is commented out. I, obviously, want to keep the nonce check in there, for security reasons.
Any thoughts?
I have doublechecked and have not strayed from what you have suggested, other than excluding certain meta boxes from displaying based on “$meta_box[‘id’]”
Which, now that I think about it, is exactly why it is failing.
The loop in options_post is going through all the default metaboxes
Since I have prevented some from being displayed, their nonce doesn’t exist on my edit page, so the nonce check fails for those missing boxes.
So, the question is, how do I display working metaboxes for only those metaboxes I want?
Rich, see line 56 to 59 in the article above, the loop goes through the meta boxes by name, do a
var_dump()
of the array and see what the names are, then create a custom array and only use the names for the meta boxes you want to appear.I still don’t think that will work. I can easily choose the metaboxes I want to appear or not appear.
The problem is that in save_meta() in options_post.php, the loop is:
$post_options->meta_boxes doesn’t have anything to do with what I have decided should appear on my edit page. Or am I missing something? Even if I create a custom array (as opposed to skipping the add_meta_box() function call based on the metabox name / id), that custom array doesn’t equal the array that is being looped through in save_meta() in options_post.php.
From what I understand, $post_options->meta_boxes will contain all of the metaboxes, regardless of what I have decided to display? So, the nonce will fail when the loop in options_post.php is on a metabox in $post_options->meta_boxes that is not in the set of metaboxes I have elected to display on my custom post edit screen.
If I include ALL metaboxes, as is coded in lines 56-59, the meta information saves fine. It only fails if I decided to exclude anything.
Unless the scope of $post_options->meta_boxes in save_meta() (options_post.php) is different than I understand?
As reference, here is the code I am using that does display the metaboxes I want, yet will not save/update the metadata entered into the metaboxes. If I remove the if statement (so, including all metaboxes), the meta data saves fine:
Rich, I see what you mean… how about this (just pseudo code, you’ll have to adjust and test):
Smart man! I think that will work. I will give it a try and get back to you.
Dimas – You definitely pointed me in the right direction.
The trouble is the [expected-value].
During the save_meta() function call, the nonces are verified via:
So, when creating the [expected-value], you need to use the same $action argument that the save_meta() function is using. Since my save_post action is called from custom_functions.php, and the save_meta() function is called from options_post.php, plugin_basename(__FILE__) will be different, and the nonce would fail.
So, I did some ugliness (hard coded the path to the options_post.php file) to make it work for now, but here it is:
Note: during all of this, I realized that line 61 of your original post is not necessary. The action to save meta data during save_post had already been called. It isn’t necessary to call it again.
I also removed line 48, adding the action to admin_menu. Instead, I call the function that adds the meta boxes via the ‘register_meta_box_cb’ argument of register_post_type, which is called during ‘init’
Rich, glade you got it all working, there typically is always a way to solve a problem, just have to look at it from different angles … in terms of your hard coded issue you may try the following an see if that creates an accurate nonce:
I know this post is nearly a year old now but I have a question that i can’t seem to find an answer to.
I have multiple custom post types and I wish to add the thesis seo and meta boxes to all of them but I get a repeat error when I do. The first is fine and works perfectly using the code above. Unfortunately I cannot find a way to duplicate this for my other custom post types. Can anyone shed some light on this?
Thanks
@Mark B, try the following (adjust the
$post_types
array) …great tutorial…thank you for sharing
Great tutorial. Stopped by your blog after hyperarts linked to you and definitely will be back. I do a lot with thesis and so these kinda things come in handy.
Kim
PS: You asked in another post about spam control. I’ve had trouble with spamfree so I rely heavily on GASP (growmaps anti spambot plugin) which works well for stopping bot traffic.
@Kim, thanks for your comment, I will definitely checkout GASP, spamfree worked really well, but recently has started to let more SPAM comments through (akismet still catches them, but just more for me to deal with).
Thanks very much for this. The menu showed up fine. I reset the permalinks. Now, I’m curious, since I didn’t CPT up when I first started (it’s a recipe blog), and now I’ve added a new ‘recipes’ CPT; do I just go in and transfer the content from the regular post type in to the custom recipe post type? And then erase original post?
Hi, do you have a tutorial on how to reverse the chronology of posts in the homepage from the oldest to the newest? Thanks.
Hello,
Great custom meta box helper code you got here. I have been checking out your tool, but I wonder do I need to separately create custom post type or is there a better way to do it within your tools?
I have seen other that get you everything (post types and metaboxes in one place: http://wp.tutsplus.com/tutorials/creative-coding/custom-post-type-helper-class/)
Thanks
If you are referring to wpalchemy, it is only specific to meta box creation.