Custom Metaboxes 0.7

This post has been marked as old. The code might no longer work. Comments have been disabled as this is no longer maintained. Use the search on the right to find a newer tutorial.

» Download the CMB Library here

It’s been about six months since my original Custom Metaboxes post announcing the release of this code library for easily creating metaboxes. If you haven’t had a look at it since then, I highly recommend you hop on Github and look at the latest version and the wiki.

Some of the features:

My favorite field types:

  • WYSIWYG. If you’re using WordPress 3.3, this will automatically use the new wp_editor which gives you a ton of flexibility.
  • Taxonomy field types (select, radio, multicheck). If you’re dealing with a lot of taxonomies, the default metabox provided by WordPress takes up a lot of screen real estate. Include taxonomies in your custom metabox instead. Using select or radio is also a great way to force users to only select one taxonomy term.
  • File upload. Use the built-in media uploader to allow the user to upload any file they want. You can also store the attachment ID in case you want to retrieve other image sizes.
  • Title. If your metabox has a lot of fields in it, use the title field type to create subheadings and instructions.
  • Text (standard, medium, and small). This is as simple as it comes, but also the one you’ll use most often.
  • text_date_timestamp. This one uses a date picker to let the user select a date, and then saves it as a UNIX timestamp. This is great for event metaboxes because you can then query based on the date.

The contributors of this code library, in no particular order, include: Andrew Norcross, Jared Atchison, Travis Smith, Randy Hoyt, Jeremy Pry, Andrew Ryno, Patrick Forringer, Chris Olbekson, Stefan Crain  …and me.

Please use this code, and consider contributing any improvements you make. If you create any field types or show_on filters, post them on the wiki. If you find any bugs, post them in issues (and even better, post the fix!).

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Tim says

    Hi Bill.
    I would like to ask few question about this plugin.

    1. If I use the wsyig type metabox, when I put a video url from url, will it autoembed?
    2. If I want to use the taxonomy radio or taxonomy select type, will the output linked to the taxonomy archive?

    • Bill Erickson says

      1. There are no filters applied to the meta fields. But, if you wrap your output in ‘the_content’ filter it will receive all the same filters as the post content (add paragraphs, do shortcodes, autoembed…). Example.

      2. The taxonomy fields should ONLY be used for replacing the backend metabox for selecting taxonomy terms. When you register the taxonomy, specify show_ui => false. You can then create your own metabox for managing the taxonomy (radio, select…). Don’t use get_post_meta() to access this data. The metaboxes will store something (I think the taxonomy term slug), but the important thing is it updates the actual taxonomy terms on the post. So use get_the_terms() to retrieve the taxonomy terms attached to the current post, and display them however you want.

  2. Patrick Johanneson says

    This is great.

    One question, though: Is there something I can do to stop the auto-insert of a value into the text_date_timestamp fields? I’m working on a site for a local indie theatre, and they usually show movies 3 times a weekend, so I’ve set up 3 ‘showtime’ fields using text_date_timestamp CMBs. But sometimes a show will happen only twice or even once (say a midnight movie). If I leave the text_date_timestamp fields blank and publish or update the post, they get filled in with the current date and time. I’d like them to be empty. Is there a jQuery or WP function I can call to ensure this happens?

    (I did find a workaround — if I enter zeroes into both fields, it’ll be left blank. But I don’t want to have to remember that little kludge every time I fix a typo or something.)

    Thanks!

      • Patrick Johanneson says

        That worked, thanks.
        I think that’s the fastest tech support I’ve ever received, too. Four minutes flat.
        Happy new year!

  3. Melson says

    I want to replace the standard Wysiwyg editor for the post content with a smaller textarea. I’ve removed the editor using ‘supports’ => false in my Custom Post Type definition.
    But which ID value do I need to use for the metabox? I’ve tried ‘post_content’ and a couple of other terms, but the text is not saved. (When I use ‘post_title’, the text is saved to the title field, so that does work.)

  4. Leebaz says

    Hey, I’m posting this again as I’m not sure it got through! I’m using the custom meta boxes to control how many images a user can attach to their post, which is working pretty well. The only problem is that when I retrieve the image I was to be able to dictate which size to use from my custom image sizes, but I can’t seem to pass this to the link. Is there a way to do this?
    echo “ID, ‘image1’,300, true).”>”;
    Thanks

    • Bill Erickson says

      When you loop through the attached images, use wp_get_attachment_image_src( $image_id, $size ) to get the image at the size you want.

  5. Lee Baz says

    Thanks for your previous reply – it pointed me in the right direction and eventually I was able to retrieve the image at the size I want.
    Another question: I’d like to simplify the media uploader to get rid of the fields such as the size pickers, the alt, title and description boxes as users of my site are finding that confusing. I’ve done some digging but not found anything as yet – is there a way to customise the meta plugins to do this?
    Thanks!

  6. Kev says

    I’m using a CMB on a project and I can’t seem to retrieve posts based on custom meta from my custom post type via url. An example would be trying to retrieve all active wholesale properties.

    Here is my meta

    wpb_dealtypeselect

    and the value that I’m trying to retrieve is Active Wholesale.

    Now, when I create taxos and custom meta manually, the value would be active-wholesale, so I’m assuming the same is in this case…but I’m not getting anywhere for whatever reason. I know I’m missing something.

    I’ve tried domain.com/wpb_dealtypeselect/active-wholesale/ but that doesn’t seem to be doing the trick

    If you could point me in the right direction that would be awesome.

      • Kev says

        yeah…thanks Bill…I remembered that after I went to bed. I’ve been working on too many things over the weekend with little sleep and after my comment last night, I threw my hands up in the air and said “ah whatever, I’m going to bed”

        no sooner than I went to bed, I remembered that custom_meta isn’t queriable within url parameters and decided that I’m going to have to convert it over to a custom taxo.

        HOWEVER….I’m one of those ambitious guys that always says “you can do anything with code”, so when I got up this morning, I was like “blzzzt, there has to be a way, and I’m going to find it” Then I checked my email and saw your response….so thank you for keeping me from spending an hour or two in the codex trying to find a solution for something that doesn’t exist. this morning. lol

  7. Jonas says

    Bill, how would you remove post metaboxes like “Layout Settings” and “Theme SEO Settings”. I don’t want my users writing posts and feeling confused if they should use any of those options. Ideally only non-admins should not see those posts boxes, but I should in case I want to choose the layouts etc.

    • Bill Erickson says

      remove_post_type_support( ‘post’, ‘genesis-layouts’ );
      remove_post_type_support( ‘post’, ‘genesis-seo’ );