Avatar of Adam
Adam
Flag for Canada asked on

Creating a Custom Metabox in Wordpress

I'm trying to create a custom metabox in Wordpress for a custom post type. I found a nice little tutorial that actually explains it rather well:

http://wptheming.com/2010/08/custom-metabox-for-post-type/

The only part of the tutorial that I don't understand is where he adds the text input field.

// The Event Location Metabox
 
function wpt_events_location() {
    global $post;
 
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
 
    // Get the location data if its already been entered
    $location = get_post_meta($post->ID, '_location', true);
 
    // Echo out the field
    echo '<input type="text" name="_location" value="' . $location  . '" class="widefat" />';
 
}

Open in new window


I don't know where "wp_create_nonce( plugin_basename(__FILE__) )" comes from or what exactly it does. Having said that, I'm actually looking to have checkboxes in my metabox rather then an input field. But because I don't completely understand the code behind adding an input field, I'm not entirely sure what I would need to do for checkboxes. Can someone help me out?

A second part to that question—is it possible to configure it so that it is mandatory for an author to select one of the checkboxes in my custom metabox before they can publish the post?
WordPressPHPWeb Development

Avatar of undefined
Last Comment
Adam

8/22/2022 - Mon
gwkg

wp_create_nonce() creates a value in a hidden form field to use for a security check when submitting.

To use checkboxes instead of the text field you would change this code to checkboxes.

    echo '<input type="text" name="_location" value="' . $location  . '" class="widefat" />';

One way to force a checkbox to be checked would be to use javascript/jquery to check the field when the Publish button is clicked.

 
gwkg

Adam

ASKER
Thanks for the reply gwkg.

I changed the input type to something like this:

echo '<input type="checkbox" name="option1" value="Option 1" /><br/>
<input type="checkbox" name="option2" value="Option 2" />';

Open in new window


The checkboxes are showing up in my meta box but they don't have any values. Any idea why?

Metabox
Also, the tutorial I referenced in my question provides a way to save the meta box data (by default, once you hit publish, whatever you enter/select disappears). But because I believe the code is specific to an input text field, the code provided isn't working for the checkboxes. Any ideas of what I need to change in order to make it work?

Lastly, for the javascript validation, I assumed that I would probably need to take that route but I'm not entirely sure where to begin. You wouldn't happen to have any tutorials that can guide me through something like that?  
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Adam

ASKER
I found another similar tutorial that shows how to add a custom meta box with radio buttons. So it's somewhat closer to what I'm looking for. But I still can't seem to get it to work properly.

http://shibashake.com/wordpress-theme/add-metabox-custom-post-type

Any help would be greatly appreciated. Thanks.
Adam

ASKER
Okay, so I think I may have this working. Here is what I've put together:

<?php
// Create a custom meta box to check for publication type

add_action("admin_init", "pub_issue_type");

function pub_issue_type() {
	add_meta_box("pub_issue-meta", "What &quot;Issues&quot; is this publication related to?", "pub_issue", "publication", "normal", "low");
}

function pub_issue() {
	global $post;
	$custom = get_post_custom($post->ID);
	$pub_issue = $custom["pub_issue"][0];
?>

<ul>
  <li>
    <input type="checkbox" name="pub_issue" value="1" <?php if( $pub_issue == 1 ) echo 'checked="yes"'; ?> />
    <label>Issue One</label>
  </li>
  <li>
    <input type="checkbox" name="pub_issue" value="2" <?php if( $pub_issue == 2 ) echo 'checked="yes"'; ?> />
    <label>Issue Two</label>
  </li>
  <li>
    <input type="checkbox" name="pub_issue" value="3" <?php if( $pub_issue == 3 ) echo 'checked="yes"'; ?> />
    <label>Issue Three</label>
  </li>
  <li>
    <input type="checkbox" name="pub_issue" value="4" <?php if( $pub_issue == 4 ) echo 'checked="yes"'; ?> />
    <label>Issue Four</label>
  </li>
</ul>

<?php
}

add_action('save_post', 'save_pub_issue');

function save_pub_issue() {
	global $post;
	update_post_meta($post->ID, "pub_issue", $_POST["pub_issue"]);
}

?>

Open in new window


To put this in context, I have a custom post type for "Publications". Each publication falls under a particular category (i.e. speeches, articles, press releases, etc.). However, the publication can also be related to one or more "issues" related to the content on the site (hence why I needed the extra meta box).

And here is what my meta box looks like:

Meta box
For now, this seems to be working but I am still open to improvements. If anyone knows of a better or more efficient approach for this, please let me know.
Adam

ASKER
I may have spoke too soon... :)

If I only select one checkbox, it seems to work like a charm. But once I select multiple options and update my post, it removes all of my selections except one. So obviously, I need to figure out how to enable multiple selections.

Once again, any help or suggestions would be appreciated. Thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gwkg

One solution is to change the name of the checkbox fields so they aren't all the same.

<input type="checkbox" name="issue_3" value="1" <?php if( $issue_3 ) echo 'checked="yes"'; ?> />
<input type="checkbox" name="issue_4" value="1" <?php if( $issue_4 ) echo 'checked="yes"'; ?> />

Otherwise you need to store the values in an array and then deal with the array with something like

<input type="checkbox" name="pub_issue[]" value="4" <?php if( in_array(4, $pub_issue) ) echo 'checked="yes"'; ?> />


 
Adam

ASKER
Thanks for the reply gwkg.

I tried both of your suggestions and can't seem to get either of them to work.

Changing the "name" values didn't seem to do anything (in fact, when I publish or update a post after selecting a checkbox, it now becomes unchecked—not sure if changing the name would have anything do to with that).

For the array suggestion, I received this error in my metabox for each checkbox:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /wp-content/themes/default/functions.php on line 297 /> Issue 1

Any ideas what I'm doing wrong?
ASKER CERTIFIED SOLUTION
Adam

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Adam

ASKER
The tutorial explained how to accomplish what I was looking to do. I appreciate the help received by gwkg but in the end, I figured it out myself.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck