Wordpress save only last custom field - php

I want to have three dropdown list that allow user select Author, Post and Testimonial. But wordpress only save last dropdown.
This is first dropdown:
//allow authors list on posts
add_action( 'add_meta_boxes', 'author_list_add' );
function author_list_add() {
add_meta_box( 'my-meta-box-id', 'Post Author', 'author_list', 'post', 'normal', 'high' );
}
function author_list( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['custom_author'] ) ? esc_attr( $values['custom_author'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce ');
?>
<p>
<label for="custom_author">Select author</label>
<br>
<select name="custom_author" id="custom_author">
<option value="0" <?php selected( $selected, '0' ); ?>>No author</option>
<?php
$args = array('post_type' => 'member');
$loop = get_posts($args);
foreach ($loop as $post) : setup_postdata($post);
$name = $post->post_title;
$link = $post->ID;
?>
<option value="<?php echo $link; ?>" <?php selected( $selected, $link ); ?>><?php echo $name; ?></option>
<?php
endforeach;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'author_list_save' );
function author_list_save( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['custom_author'] ) )
update_post_meta( $post_id, 'custom_author', esc_attr( $_POST['custom_author'] ) );
}
The second Part:
//allow select testimonial list on posts
add_action( 'add_meta_boxes', 'testimonial_select_box_add' );
$types = array( 'post', 'page' );
function testimonial_select_box_add() {
add_meta_box( 'testimonial-box', 'Testimonial in bottom row', 'testimonial_select_box', $types, 'normal', 'high' );
}
function testimonial_select_box( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['testimonial_select'] ) ? esc_attr( $values['testimonial_select'][0] ) : '';
wp_nonce_field( 'testimonial_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="testimonial_select">Select Testimonial</label>
<br>
<select name="testimonial_select" id="testimonial_select">
<option value="0" <?php selected( $selected, '0' ); ?>>None</option>
<?php
$args = array('post_type' => 'testimonial','posts_per_page' => -1);
$loop = get_posts($args);
foreach ($loop as $post) : setup_postdata($post);
$name = $post->post_title;
$link = $post->ID;
?>
<option value="<?php echo $link; ?>" <?php selected( $selected, $link ); ?>><?php echo $name; ?></option>
<?php
endforeach;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'testimonial_select_box_save' );
function testimonial_select_box_save( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'testimonial_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['testimonial_select'] ) )
update_post_meta( $post_id, 'testimonial_select', esc_attr( $_POST['testimonial_select'] ) );
}
And last one:
//allow select Post list on posts
add_action( 'add_meta_boxes', 'post_select_box_add' );
function post_select_box_add() {
add_meta_box( 'post-box', 'Post in bottom row', 'post_select_box',$types, 'normal', 'high' );
}
function post_select_box( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['post_select'] ) ? esc_attr( $values['post_select'][0] ) : '';
wp_nonce_field( 'post_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="post_select">Select Post</label>
<br>
<select name="post_select" id="post_select">
<option value="0" <?php selected( $selected, '0' ); ?>>None</option>
<?php
$args = array('post_type' => 'post','posts_per_page' => -1);
$loop = get_posts($args);
foreach ($loop as $post) : setup_postdata($post);
$name = $post->post_title;
$link = $post->ID;
?>
<option value="<?php echo $link; ?>" <?php selected( $selected, $link ); ?>><?php echo $name; ?></option>
<?php
endforeach;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'post_select_box_save' );
function post_select_box_save( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'post_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['post_select'] ) )
update_post_meta( $post_id, 'post_select', esc_attr( $_POST['post_select'] ) );
}
as you can see, function and ID names are unique, but only last one saved.
this is what happened when I save :

As per my comment, each of your nonces seems to have the same name (meta_box_nonce), so there may be a clash if all three fields are submitted in the same form. Trying giving them unique names (in both your wp_nonce_field and wp_verify_nonce calls).

Related

WordPress Save Dropdown Metabox

I have already created custom meta box at my custom post type.
so I create drop down list to choose a Teacher's name and the Class.
The value can be shown in the dropdown, but after publishing the post the value cannot save and dropdown set to default again.
can somebody know, if I am missing part or wrong code here?
Thanks if you wanna help, so here's my code:
function portfolio_student(){
add_meta_box( 'portfolio_student', 'Select The Teachers and Classes', 'meta_box_data', 'portfolio', 'normal', 'high' ); }
function meta_box_data(){
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
$teachers = isset( $values['the_teachers'] ) ? esc_attr( $values['the_teachers'] ) : '';
$classes = isset( $values['the_classes'] ) ? esc_attr( $values['the_classes'] ) : '';
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_data' );
?>
<p>
<label for="the_teachers">Teacher</label>
<select name="the_teachers" id="the_teachers">
<option value="0">-- Select Class --</option>
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'teacher'
);
$teacher_posts = get_posts($args);
foreach( $teacher_posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->ID; ?>" <?php selected($teachers, $post->ID); ?>><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="the_classes">Class</label>
<select name="the_classes" id="the_classes">
<option value="0">-- Select Class --</option>
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'class'
);
$class_posts = get_posts($args);
foreach( $class_posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->ID; ?>" <?php selected($classes, $post->ID); ?>><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php}
add_action( 'save_post', 'cd_meta_box_save' );function cd_meta_box_save( $post_id ){
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_data'] ) || !wp_verify_nonce( $_POST['meta_box_data'], 'my_meta_box_nonce' ) )
return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) )
return;
// Make sure your data is set before trying to save it
if( isset( $_POST['the_teachers'] ) )
//Update Meta Data
$teacher_meta = $_POST['the_teachers'];
//EndUpdate
update_post_meta( $post_id, 'the_teachers', $teacher_meta );
if( isset( $_POST['the_classes'] ) )
//Update Meta Data
$class_meta = $_POST['the_classes'];
//EndUpdate
update_post_meta( $post_id, 'the_classes', $class_meta ); }
Try changing these
update_post_meta( $post_id, 'the_teachers', $teacher_meta );
update_post_meta( $post_id, 'the_classes', $class_meta );
To
update_post_meta( $post->ID, 'the_teachers', $teacher_meta );
update_post_meta( $post->ID, 'the_classes', $class_meta );
Since you have declared $post as the global variable

How To Show Chechbox Result From Metabox To Single Post Wordpress?

Ask to everyone, i have problem. Here i try to use multiple chechbox to my custom post metabox.
<?php
function prodetail() {
add_meta_box('pro_metabox', 'Detail Property', 'pro_metabox', 'property', 'normal', 'default');
}
function pro_metabox() {
global $post;
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) );
$elements = array(
'pool' => 'Pool',
'garage' => 'Garage',
'balcon' => 'Balcon',
'yard' => 'Yard',
'internet' => 'Internet'
);
foreach ( $elements as $id => $element) {
if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) {
$checked = 'checked="checked"';
} else {
$checked = null;
}
?>
<div class="pro-inn">
<div class="procols">
<div class="pro-inn">
<input type="checkbox" name="multval[]" value="<?php echo $id; ?>" <?php echo $checked; ?> />
<?php echo $element;?>
</div>
</div>
</div>
<?php
}
}
function pro_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'elements', $_POST['multval'] );
} else {
delete_post_meta( $post_id, 'elements' );
}
}
add_action('save_post', 'pro_meta', 1, 2);
?>
help me to add code to show this checked result to single.php because my code use foreach just show Array text not show text like Pool Garage Balcon ect.
Thanks
Use this code in your single.php file for your custom post
$meta_value = get_post_meta( $post->ID, 'elements', true );
foreach($meta_value as $key=>$value){
echo $value . ' ';
}
It will show results same as you mentioned in the question ie:
(Pool Garage Balcon ect.)

Adding a dropdown metabox to page admin including all post categories and storing the value

I am trying to add a custom meta box to page-admin that shows all my post-categories as options. I already made the meta box appear but i can not figure out how to show the categories. The dropdown is empty.
Here is my code:
function cd_meta_box_page()
{
global $page;
$values = get_post_custom( $page->ID );
$selected = isset( $values['my_meta_box_page'] ) ? esc_attr( $values['my_meta_box_page'] [0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
Wählen Sie hier eine Kategore aus welcher die Beiträge in die Seite geladen werden sollen.
</p>
<p>
<label for="my_meta_box_page">Kategorien</label>
<select name="my_meta_box_page" id="my_meta_box_page">
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 5, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<option value="'.$cat->name; selected( $selected, $cat->name ); echo '">'.$cat->name; echo'</option>';
$args2= array("orderby"=>'name', "category" => $cat->cat_ID); // Get Post from each Sub-Category
$posts_in_category = get_posts($args2);
}
?>
</select>
</p>
<?php
}
I can not find my fault. Can anyone help, please?
Thank you!
EDIT:
I found the issue. Deleting the following line helped:
'child_of' => 5, //Child From Boxes Category
Now I have the problem that wordpress does not store my selection. Saving the value of my_meta_box_postvariante works fine. But the value for my_meta_box_page is always staying default. I can not make it work... My code:
function cd_meta_box_page()
{
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_page'] ) ? esc_attr( $values['my_meta_box_page'] [0] ) : '';
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
Wählen Sie hier eine Kategore aus welcher die Beiträge in die Seite geladen werden sollen.<br />
</p>
<p>
<label for="my_meta_box_page">Kategorien</label>
<select name="my_meta_box_page" id="my_meta_box_page">
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<option value="'.$cat->name; selected( $selected, $cat->name ); echo '">'.$cat->name; echo'</option>';
}
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchors can only have href attribute
)
);
// Make sure your data is set before trying to save it
if( isset( $_POST['my_meta_box_postvariante'] ) )
update_post_meta( $post_id, 'my_meta_box_postvariante', esc_attr( $_POST['my_meta_box_postvariante'] ) );
if( isset( $_POST['my_meta_box_page'] ) )
update_post_meta( $post_id, 'my_meta_box_page', esc_attr( $_POST['my_meta_box_page'] ) );
}
?>

cant display custom meta box data

im creating a custom metabox as such:
<?php
/**
* Plugin Name: Relaterade sidor
* Description: lägg till relaterade sidor
* Version: 1.0
* Author: test
* Author URI: test
*
*/
function relaterade_sidor_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function relaterade_sidor_add_meta_box() {
add_meta_box(
'relaterade_sidor-relaterade-sidor',
__( 'relaterade sidor', 'relaterade_sidor' ),
'relaterade_sidor_relaterade_sidor_html',
'post',
'normal',
'default'
);
add_meta_box(
'relaterade_sidor-relaterade-sidor',
__( 'relaterade sidor', 'relaterade_sidor' ),
'relaterade_sidor_relaterade_sidor_html',
'page',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'relaterade_sidor_add_meta_box' );
function relaterade_sidor_relaterade_sidor_html( $post) {
wp_nonce_field( '_relaterade_sidor_relaterade_sidor_nonce', 'relaterade_sidor_relaterade_sidor_nonce' ); ?>
<p>relaterade sidor</p>
<p>
<label for="relaterade_sidor_relaterade_sidor_url"><?php _e( 'Url', 'relaterade_sidor' ); ?></label><br>
<input type="text" name="relaterade_sidor_relaterade_sidor_url" id="relaterade_sidor_relaterade_sidor_url" value="<?php echo relaterade_sidor_get_meta( 'relaterade_sidor_relaterade_sidor_url' ); ?>">
</p><?php
}
function relaterade_sidor_relaterade_sidor_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['relaterade_sidor_relaterade_sidor_nonce'] ) || ! wp_verify_nonce( $_POST['relaterade_sidor_relaterade_sidor_nonce'], '_relaterade_sidor_relaterade_sidor_nonce' ) ) return;
if ( ! current_user_can( 'edit_post' ) ) return;
if ( isset( $_POST['relaterade_sidor_relaterade_sidor_url'] ) )
update_post_meta( $post_id, 'relaterade_sidor_relaterade_sidor_url', esc_attr( $_POST['relaterade_sidor_relaterade_sidor_url'] ) );
}
add_action( 'save_post', 'relaterade_sidor_relaterade_sidor_save' );
/*
Usage: relaterade_sidor_get_meta( 'relaterade_sidor_relaterade_sidor_url' )
*/
?>
the text remains in the textfield after saving/updating so I guess that means it is saved to the database, but when trying to display the data in a page or post I only get blank.
I tried with both these ones:
<?php if (have_posts()) : while (have_posts()) : the_post();
$intro = get_post_meta(get_the_ID(), 'relaterade_sidor-relaterade-sidor', true);
echo "content: " . $intro;
?>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'relaterade_sidor-relaterade-sidor', true);
wp_reset_query();
?>
But I only get blank, cant get it to display the content saved in the custom meta textbox.
Sorry, was trying to connect it to the metabox name when i should connect the key,
$intro = get_post_meta(get_the_ID(), 'relaterade_sidor_relaterade_sidor_url', true);
echo "content: " . $intro;
works just fine inside the loop
I suggest CMB2 library. No more headaches with metaboxes ;)
https://stackoverflow.com/a/31662024/5162081

Make Input Field Output Links

So, I added an input field to one of my post types.
What I'm trying to do is put a link (google.com, facebook.com, any link) into this field, and have it output on the front end into a button.
Below is my button. I would like it to go to the link I placed on the input field when I click the button.
<?php echo __('Purchase Now','Vierra'); ?>
Here's the code for the metabox:
<?php
add_action( 'add_meta_boxes', 'cart_meta_box_add' );
function cart_meta_box_add() {
add_meta_box( 'cart-meta-box-id', 'Put cart Here!', 'cart_meta_box_cb', 'Room', 'side', 'high' );
}
function cart_meta_box_cb() {
wp_nonce_field( basename( __FILE__ ), 'cart_meta_box_nonce' );
$value = get_post_meta(get_the_ID(), 'cart_key', true);
$html = '<label>cart: </label><input type="text" name="cart" value="'.$value.'"/>';
echo $html;
}
add_action( 'save_post', 'cart_meta_box_save' );
function cart_meta_box_save( $post_id ){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( !isset( $_POST['cart_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['cart_meta_box_nonce'], basename( __FILE__ ) ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
if( isset( $_POST['cart'] ) )
update_post_meta( $post_id, 'cart_key', esc_attr( $_POST['cart'], $allowed ) );
}
?>
Thank you for the help!
It's the same function used in the admin: get_post_meta(). Assuming the button is being printed inside the loop *:
<?php
if( $field = get_post_meta( get_the_ID(), 'cart_key', true ) ) {
?>
<a href="<?php echo $field; ?>" id="btn-book-now" class="btn-custom">
<?php echo __('Purchase Now','Vierra'); ?>
</a>
<?php
}
?>
* Otherwise, you'd use $post->ID instead of get_the_ID().

Categories