I'm trying to save some data in WordPress database from a meta box.
I've got a dropdown list to select some options and I want to save the selected option in database thanks to meta box.
However I have some difficulty with the save function in PHP :
<?php
function add_admin_menu_class_meta_box() {
$pages = array('post', 'portfolio');
foreach( $pages as $page ) {
add_meta_box('custom_element_grid_class','Element grid size', 'custom_element_grid_class_meta_box', $page, 'side', 'high');
}
}
add_action( 'admin_menu', 'add_admin_menu_class_meta_box' );
function custom_element_grid_class_meta_box(){
?>
<label>Choose the size of the element : </label>
<select name="custom_element_grid_class" id="custom_element_grid_class">
<option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
<option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
<option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
<option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
</select>
<?php
}
add_action('save_post', 'save_custom_element_grid_class');
function save_custom_element_grid_class(){
global $post;
if(!isset($_POST["custom_element_grid_class"])):
return $post;
endif;
update_post_meta($post->ID, 'custom_element_grid_class', $meta_element_class);
}
?>
How can I get the select value an save it with update_post_meta()
?
To add a meta box to a number of post types screens – post , page and a book custom post type; create an array of the post types, iterate over the array and use add_meta_box() to add the meta box to them.
What is a Meta Box? When a user edits a post, the edit screen is composed of several default boxes: Editor, Publish, Categories, Tags, etc. These boxes are meta boxes. Plugins can add custom meta boxes to an edit screen of any post type.
What is a Custom Meta Box? Custom meta boxes allow users to add additional information to posts, pages and custom post types, apart from the default set of information that WordPress takes using default meta boxes. Plugins and Themes can use custom meta boxes to take additional structured user input.
Here is the code I tested and it works:
<?php
/**
* Plugin Name: Metabox test
*
*/
add_action( 'add_meta_boxes', 'so_custom_meta_box' );
function so_custom_meta_box($post){
add_meta_box('so_meta_box', 'Custom Box', 'custom_element_grid_class_meta_box', $post->post_type, 'normal' , 'high');
}
add_action('save_post', 'so_save_metabox');
function so_save_metabox(){
global $post;
if(isset($_POST["custom_element_grid_class"])){
//UPDATE:
$meta_element_class = $_POST['custom_element_grid_class'];
//END OF UPDATE
update_post_meta($post->ID, 'custom_element_grid_class_meta_box', $meta_element_class);
//print_r($_POST);
}
}
function custom_element_grid_class_meta_box($post){
$meta_element_class = get_post_meta($post->ID, 'custom_element_grid_class_meta_box', true); //true ensures you get just one value instead of an array
?>
<label>Choose the size of the element : </label>
<select name="custom_element_grid_class" id="custom_element_grid_class">
<option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
<option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
<option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
<option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
</select>
<?php
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With