122 responses to “How to Create A Custom WordPress Meta Box Instead of Using WordPress Custom Fields”

  1. Rio

    how to echo this to my theme function?

  2. Christopher B

    Elegant code and works properly on page or posts, but no luck with Custom Post Types in WP 3.0. Changed foreach (array('post','page') as $type) to foreach (array('feature') as $type) . Then the metaboxes only showed up on that custom post type which is great. But when I enter in the fields text, after clicking save it does not save. Changed if ($_POST['post_type'] == 'page') to if ($_POST['post_type'] == 'feature') as but still no luck. Also tried adding: // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
    }
    but still no luck.

    Desperately trying to figure this out before tomorrow morning, but will stay up all night til I get it. Thanks in advance!

    Cheers,
    Christopher

  3. Christopher

    Tried installing on the latest version of WP 3.0. Works for Posts but doesn’t seem for custom post types. Changed ==page to == the name of my custom post type. Have it set to only show the boxes on that post type, and everything appears to work but when you enter in data it does not save. Tried adding do not autosave as this article recommends but still no luck. Any ideas anyone? http://osdir.com/ml/wp-testers/2010-03/msg00205.html

  4. Tom

    Hi,

    I have everything set up looking perfect in the add page section of the site, but how do I get the value to show up in my template?

    I’m trying: echo get_post_meta($post->ID, ‘_my_meta’ true);

    Which echo’s “Array” in my template.

    Any guidance would be very much appreciated.

  5. Tom

    Perfect, thanks a lot man!

  6. Luke

    I’m trying to get the meta data to display and right now it is showing nothing. Data saves and updats in post. When I print_r the results, I get a 1. Any idea what I might be dong wrong.

    $arr = get_post_meta($post->ID,'_my_meta',TRUE);
    echo $arr['link'];

    By the way, does it make a difference if I have set up a custom post type (events).

  7. Luke

    Dimas,

    Thanks for the reply. I did get this working using WP 3.0 Beta 1 (current version of nightly build). I checked the DB for _my_meta and it was there along with checking the post_ID being correct.

    I ended up having to call the global $post;. So in the events template:


    global $post;
    $arr = get_post_meta($post->ID,'_my_meta',TRUE);
    echo $arr['link'];
    echo $arr['description'];

    Thanks again for the reply. This is by far the cleanest, organized and easiest way I’ve found to accomplish this. I’ve had luck with other methods but again, this is much more friendly way of doing it. Great post!

  8. Luke

    (I hope this doesn’t submit a numerous amount of times, it didn’t appear to show up initially.)

    Dimas,

    I’m not sure if you have used Gravity Forms (www.gravityforms.com) before or not. It is the best thing out there for forms and even lets you create a post by submitting the form info into custom posts. I have this working with the exception of it posting the custom fields into the “User Friendly” way you’ve provided in this post. I wanted to pick your brain to see what you might suggest as to what the custom field value may be (4 screenshots below to help)

    http://dl.dropbox.com/u/2557221/0-form-setup.png
    Gravity Form Setup – This allows the post to submit a custom field. What would I set the custom field value as?

    http://dl.dropbox.com/u/2557221/1-form-preview.png
    Form Preview – You can see the fields and the values being set.

    http://dl.dropbox.com/u/2557221/2-added-to-events.png
    It’s been added to “Events” – After submitting, it shows it posted to the Events Custom Posts.

    http://dl.dropbox.com/u/2557221/3-custom-fields.png
    Custom Fields View – This shows the custom fields being set as shown, although not in the “User Friendly” area you’ve demonstrated here.

    Thanks in advance for any thoughts you might have. I really appreciate it!

  9. donalyza

    Hi! Is there a way to show customized meta-boxes UI on each category?

    For instance.

    Category 1
    custom_field 1
    custom_field 2
    Category 2
    custom_field 1
    custom_field 3

  10. donalyza

    Thank you! I was able to make it to work. You just made my day. :)

  11. Christopher Beckwith

    Dimas,

    Thank you for the reply. Actually, still experiencing the problem. When I follow your instructions for custom meta boxes on WP 3.0 for the normal post type, works perfectly. When I use it for a custom post type I created, all the boxes show up however when I enter in information into a metabox and click save, the content in those boxes disappear. I can post some code, but first is their anything off the top of your head that might cause this, that I should for first? Thanks Dimas!

  12. Christopher Beckwith

    Thanks for the update. I updated to your new code, but it still does allow me to save in the boxes unfortunately. Also with the new code, now when I try to save in a normal post that does not work either. Where is before that did work, and it only didn’t work in custom post types. I have you functions.php code at the end of my own functions file for the site. I pasted the part of the file that relates to the issue: http://pastie.org/949123 If you get a chance maybe you can see where I’m going wrong, or point me in the right direction. To note, on this line foreach (array(‘post’,’work’,’page’) as $type) I only left post and page for testing, but I will ultimetly only wanting the metaboxes to show on the work post type. So foreach (array(‘work’) as $type) Thank you!

  13. Christopher Beckwith

    Actually, now I’m thinking maybe the problem is in my meta.php file. I created names for each field but now sure if I did it properly. http://pastie.org/949140

  14. Christopher Beckwith

    I think I’m definitely over quota on posts here today. But I noticed in the meta.php file I uploaded, I misspelled ‘url’ as ‘u’. Fixed that but still no luck. And tried you meta.php unchanged and still no luck on saving. Thanks again in advance. I’ll keep trying different things here to see if anything works. So close!

  15. Christopher Beckwith

    It’s good to know it’s working for you at least as the problem must lie else where in my code. (where I haven’t been looking!).

    I had fixed the typo prior but no luck. check1,check2,check3 all where echoed as well as the rest of the code. http://pastie.org/949220

    The last line however said Warning: Cannot modify header information – headers already sent by (output started at /dev/wp-content/themes/mytheme/epanel/custom_functions.php:465) in /dev/wp-includes/pluggable.php on line 887

    To note: 465 is echo “check-1″ the start of the function my_meta_save

    Does this mean I am using that same function name somewhere else? Your debugging tip certainly is pushing me in the right direction.

    Cheers

  16. Christopher Beckwith

    Now this is odd, so I tried changing the function name: my_meta_save to my_meta_saves in both locations. And it worked! Then I tried putting them both back to my_meta_save and it still works. Even after emptying my cache several times. I have no idea if using the debug script once, or changing the function name once somehow refreshed something or what. So strange, but looks like I’m good now. Thank you for sticking it out with me. If I can figure out exactly what fixed the issue I’ll be sure to post for other readers.

  17. Christopher Beckwith

    Now that I have been able to save meta box data successfully due to the dedicated help of Dimas I needed to figure out how to display the inputs in the template. The code above by Luke worked perfectly in WP 3.0b2. Everything is finally working as it should.

1 2 3 5

Leave a Reply