The only part of the tutorial that I don't understand is where he adds the text input field.
// The Event Location Metaboxfunction 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" />';}
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
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.
The checkboxes are showing up in my meta box but they don't have any values. Any idea why?
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?
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.
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:
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.
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
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.