Save checkbox value in metabox

This isn’t a WP question, but moreso a general form issue. Regardless, a checkbox will not pass anything if unchecked, and 1 or on if checked. // Sanitize user input. $my_data = $_POST[‘home_slider_display’] ? true : false; // Update the meta field in the database. update_post_meta( $post_id, ‘home_slider_display’, $my_data );

Removing meta boxes: remove-meta_box() or unset()?

When in doubt use the API. Let’s say the structure of $wp_meta_boxes will change or go away one day. remove_meta_box() will still work, because the API is a contract between core and developers. Unsetting some keys in a global variable might break. unset() is easier to write when you want to remove a whole group: … Read more

Custom Meta Boxes: multiple fields within a repeatable field

Try Fieldmanager. It was built with repeating groups being priority #1. Your new code would look something like this: add_action( ‘init’, function() { $fm = new Fieldmanager_Group( array( ‘name’ => ‘artists’, ‘limit’ => 0, ‘label’ => ‘New Artist’, ‘label_macro’ => array( ‘Artist: %s’, ‘name’ ), ‘add_more_label’ => ‘Add another Artist’, ‘children’ => array( ‘name’ => … Read more

How do I position meta_box on post edit screen after the title?

You cannot use a real metabox to do that, hook into edit_form_after_title instead. Here is a simple example: add_action( ‘edit_form_after_title’, ‘wpse_87478_pseudo_metabox’ ); add_action( ‘save_post’, ‘wpse_87478_save_metabox’ ); function wpse_87478_pseudo_metabox() { global $post; $key = ‘_wpse_87478’; if ( empty ( $post ) || ‘post’ !== get_post_type( $GLOBALS[‘post’] ) ) return; if ( ! $content = get_post_meta( $post->ID, … Read more

One metabox for multiple post types

Define an array of post types, and register the metabox for each one separately: function add_custom_meta_box() { $post_types = array ( ‘post’, ‘page’, ‘event’ ); foreach( $post_types as $post_type ) { add_meta_box( ‘custom_meta_box’, // $id ‘Custom Meta Box’, // $title ‘show_custom_meta_box’, // $callback $post_type, ‘normal’, // $context ‘high’ // $priority ); } }

How to reorder meta box position?

Users can drag and drop meta boxes and WordPress will save the order. So, in a sense, the order in which they were originally rendered does not matter. You know which one comes first because when you call add_meta_box it simply pushes your meta box’s args onto a global array called $wp_meta_boxes. Whichever call to … Read more