WordPress Save Dropdown Metabox - php

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

Related

How to convert the WordPress meta box Page Attributes->Page Template dropdown to radio buttons?

I would like to be able to change the Page Attribute "Templates" dropdown to radio buttons, to allow me to have corresponding thumbnails next to them.
It looks like this question was already asked here - Is it possible to make WordPress page attribute meta box select option display as images? - however no code was ever supplied and looking at the comments it appears that the user answered their own question?
I have already removed and replaced the Page Attributes meta box on functions.php, as per below:
add_action( 'add_meta_boxes', 'wpse44966_add_meta_box' );
function wpse44966_add_meta_box( $post_type ){
remove_meta_box(
'pageparentdiv',
'page',
'side');
add_meta_box(
'wpse44966-meta-box',
'page' == $post_type ? __('Page Style Templates') : __('Attributes'),
'wpse44966_meta_box_cb',
'page',
'side',
'low');
}
I am then able to call back the "Templates" dropdown in my own meta box - I have also included the page_template_dropdown function (renamed 'page_template_dropdown_show_it').
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$selected = selected( $default, $templates[ $template ], false );
echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<select name="page_template" id="page_template">
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
</select>
<?php endif;
}
However, when I then amend both page_template_dropdown_show_it and wpse44966_meta_box_cb to show radio buttons, the changes are applied visually but nothing happens when you select them?
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$checked = checked( $default, $templates[ $template ], false );
echo "\n\t<input type='radio' name='page_template' value='" . esc_attr( $templates[ $template ] ) . "' $checked>" . esc_html( $template );
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<input type='radio' name='page_template' value="default"><?php echo esc_html( $default_title ); ?>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
<?php endif;
}
It's obviously not as simple as a straight swap-out (as it isn't working), but the only thing that I can see that is missing is now there is nothing carrying the id="page_template", whereas before it was included in the below:
<select name="page_template" id="page_template">
So, it appears that I was correct that the issue was that the id "page_template" was no longer being passed.
To solve this, all I have done is applied some Javascript (using the admin_enqueue_scripts function) which adds the id "page_template" to the radio button that has been selected.

wp_reset_postdata() doesn't restore the global $post variable

I'm using WP_Query to get posts from a custom post type to use the result in a metabox. Everything works great with my query. But after this query I can't get the other meta values from database.
This is my helper function to get custom field value:
function my_page_get_custom_field( $value ) {
global $post;
$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
return false;
}
Here's my query:
$sliderArgs = array(
'posts_per_page' => -1,
'post_type' => 'slider',
);
$slider = new WP_Query($sliderArgs);
if ($slider->have_posts()) {
?>
<select name="slider" id="slider">
$selectedSlide = my_page_get_custom_field('slider');
while($slider->have_posts()){
$slider->the_post();
$slideID = get_the_ID();
?><option value="<?php echo $slideID; ?>" <?php selected($selectedSlide, $slideID, true); ?>><?php the_title(); ?></option><?php
}
wp_reset_postdata(); ?>
</select>
}
And this is my other custom field which returns empty (there is a value in database and when I try to change it works great but not displaying in input value in admin):
<input type="text" name="meta_title" id="meta_title" value="<?php echo my_page_get_custom_field('meta_title'); ?>">
OK I solved it.
I used get_posts instead of WP_Query. This helped me a lot: https://core.trac.wordpress.org/ticket/18408#comment:5

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'] ) );
}
?>

Wordpress save only last custom field

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).

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

Categories