I made a checkbox meta box in my functions.php file but I have two problems. I can't save if checkbox is checked or not for the next times. and I want to show something in div if it was checked.
<?php
function filmview_ext_info_meta_box() {
add_meta_box(
'filmview_ext_info_meta_box',
__( 'Extra info', 'filmview_ext_info_meta' ),
'filmview_ext_info_meta_html',
'filmview',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'filmview_ext_info_meta_box' );
function filmview_ext_info_meta_html( $post) {
wp_nonce_field( '_filmview_ext_info_meta_nonce', 'filmview_ext_info_meta_nonce' ); ?>
<p>
<input type="checkbox" id="filmview_ext_info_meta_sub" name="filmview_ext_info_meta_sub" <?php checked( $check, 'on' ); ?> />
<label for="filmview_ext_info_meta_sub">Does it have subtitle?</label>
</p>
<?php
}
function filmview_info_meta_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['filmview_info_meta_nonce'] ) || ! wp_verify_nonce( $_POST['filmview_info_meta_nonce'], '_filmview_info_meta_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
$chk = isset( $_POST['filmview_ext_info_meta_sub'] ) ? 'on' : 'off';
update_post_meta( $post_id, 'filmview_ext_info_meta_sub', $chk );
}
add_action( 'save_post', 'filmview_info_meta_save' );
?>
What I can see is that:
1) You init $chk in the function: filmview_info_meta_save
2) You are using (with your provided code, an undefined variable ) $check
Try to read it from the database instead of of $_POST which is only present in the page after you submit the form, this should Do:
function filmview_ext_info_meta_html( $post) {
wp_nonce_field( '_filmview_ext_info_meta_nonce', 'filmview_ext_info_meta_nonce' );
$chk = get_post_meta($post->ID, 'filmview_ext_info_meta_sub', true);
?>
<p>
<input type="checkbox" id="filmview_ext_info_meta_sub" name="filmview_ext_info_meta_sub" <?php checked( $chk[0], 'on' ); ?> />
<label for="filmview_ext_info_meta_sub">Does it have subtitle?</label>
</p>
<?php
}
Related
What I'm trying to do is for a dual-language website. This particular language, Papiamento, isn't supported by Wordpress. Therefore, the client had to create two separate pages, in English and Pap. What I did was to code it like that to display English or Pap menu for each page.
Like this, in header.php:
<?php
if( is_page( array('salon-and-spa-pap', 'tocante-nos', 'testimonio', 'tuma-contacto-cu-nos', 'galeria', 'tratamentonan-di-masahe', 'tratamentonan-spa-di-curpa', 'servicionan-di-boda', 'tratamentonan-spa-di-cara', 'wowo-lip-nek', 'cuido-di-man', 'tratamento-di-huna', 'tratamento-di-huna-di-pia', 'cuido-di-pia', 'salon-p', 'spa-etiquette-pap', 'wax-p', 'reserva-un-tratamento')) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}
?>
However, the problem is that the client will have to keep going back to header.php to add another page slug every time she create a new page. Therefore, I created a Metabox plugin for that. I made a Metabox checkbox so that everytime a page is intended for Papiamento language, the client can just check the box and either the page id or slug will be added to the code above.
I found another question (https://wordpress.stackexchange.com/questions/71043/listing-pages-with-checkboxes-in-a-metabox-and-saving-them) that might be similar to what I was looking for but it didn't work for me.
Here's my metabox function based on this article (http://themefoundation.com/wordpress-meta-boxes-guide/).
<?php
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Papiamento Page Box', 'prfx-textdomain' ), 'prfx_meta_callback', 'page', 'normal', 'low' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
$checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
?>
<p>
<span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="checkfield[]" id="page_<?php echo $page->ID; ?>" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/> <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?>
<?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?>
</label>
</div>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
// Checks for input and saves
if( isset( $_POST[ 'checkfield' ] ) ) {
update_post_meta($post_id, "checkfield", $_POST['checkfield'] );
}
}
add_action( 'save_post', 'prfx_meta_save' );
and calling the page id in header.php like this:
<?php
if( in_array($page->ID, (array) $checkfield) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}
?>
But it didn't work. Please help!
***************** UPDATE *****************
I've been trying to edit the function prfx_meta_callback( $post ) but with no success. Here's the latest code I'm trying to modify...
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
$page = get_pages();
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="page_<?php echo $page->ID; ?>">
<input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( isset($checkfield [ ?>'<?php echo $page->ID; ?>'<?php ] ) ) checked( $checkfield[ ?>'<?php echo $page->ID; ?>'<?php ][0], '<?php echo $page->ID; ?>'); ?>/> <?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?></label> <br>
</div>
</p>
<?php
I've been trying to make it like that, if Pap, then checked, but if not Pap, unchecked in every page.
If the metabox is saving the checkbox settings properly, you can use the following code to make the check in header.php:
if ( in_array( get_the_ID(), get_post_meta( get_the_ID(), 'checkfield', true ) ) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}
I have 2 custom post types named clients and casestudies. I'm trying to build a meta box on the clients post type that will have a drop down list featuring the titles of all posts from the casestudies post type. This will end up with a page displaying the featured image from the clients post type, then hyperlink off to the relevant casestudies post if a selection is made from the drop down list.
I have followed this tutorial to get a meta box put together: http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336
This is the meta box code I have in my functions.php file:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'clients', 'side', 'default' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post', $post_id ) ) return;
if( isset( $_POST['my_meta_box_select'] ) )
update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
}
The meta box displays correctly on the correct post type, but when I update the post it won't save the data.
Thanks.
You don't have nonce hidden field. Save function would return nothing.
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
UPDATE:
So your cd_meta_box_cb function would be
<?php
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
<?php
}
?>
I created a custom meta box (author) for a custom post type (books). The box shows up in the admin fine, but I can't figure out how to display the author in my theme.
This is the code used to create the box:
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Book Author', 'prfx-textdomain' ), 'prfx_meta_callback', 'rabe_books', 'side' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
/**
* Adds the meta box stylesheet when appropriate
*/
function prfx_admin_styles(){
global $typenow;
if( $typenow == 'post' ) {
wp_enqueue_style( 'prfx_meta_box_styles', plugin_dir_url( __FILE__ ) . 'meta-box-styles.css' );
}
}
add_action( 'admin_print_styles', 'prfx_admin_styles' ); ?>
And this is what I placed in my template:
<div class="book-author">by:
<?php $book_author = get_post_meta( get_the_ID(), 'meta-text', true );
if (!empty($book_author)) {
echo $book_author;
} elseif (empty($book_author)) {
echo "Why doesnt it work?";
} ?>
</div>
Instead of displaying the author, it displays the text "Why doesnt it work?" which I guess means that the value is empty. But it shouldn't be. What am I doing wrong?
As mevius mentioned, you're issue is here:
<?php _e( 'Example Text Input', 'prfx-textdomain' )?>
http://codex.wordpress.org/Function_Reference/_e
You'll need to assign the box to a data point.
This is a snippet from one of our functions.php file that has this:
function css_metadata_box($object, $box) { ?>
<p>
<label for="custom-css-data">Add custom CSS for this page</label>
<textarea name="custom-css-data" id="custom-css-data" cols="60" rows="25" tabindex="30" style="width: 97%;"><?php
echo esc_html( get_post_meta( $object->ID, 'custom-css', true ), 1 );
?></textarea>
<input type="hidden" name="css_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
add_meta_box('css-data-box', 'Page CSS', 'css_metadata_box', 'page', 'normal', 'high');
This is probably a bit of a basic question, but I am a bit lost.
Basically, I am trying to create a checkbox meta state within a custom post type. I then want to check if that checkbox has been selected to alter what is displayed in my theme.
So here is what I have:
my custom-post-class:
function slide_box() {
$slide_stored_meta = get_post_meta((int)$_REQUEST['post'] );
?>
<div>
<label for="slide-checkbox">
<input type="checkbox" name="slide-checkbox" id="slide-checkbox" value="yes" <?php if ( isset ( $slide_stored_meta['slide-checkbox'] ) ) checked( $slide_stored_meta['slide-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Display Title and Excerpt?', 'prfx-textdomain' )?>
</label>
</div>
<?php
}
add_action('save_post','slide_save_meta');
function slide_save_meta($postID) {
if ( is_admin() ) {
// Checks for input and saves
if( isset( $_POST[ 'slide-checkbox' ] ) ) {
update_post_meta( $postID, 'slide-checkbox', 'yes' );
} else {
update_post_meta( $postID, 'slide-checkbox', 'no' );
}
}
}
?>
It seems to save it ok, as when I update the post it stays checked on unchecked correctly.
Next my theme template file:
<?php $slider = new WP_Query(array('post_type' => 'slide', 'posts_per_page'=>20, 'suppress_filters'=>0 )); ?>
<?php if ($slider->have_posts()) : while($slider->have_posts()) : $slider->the_post(); ?>
**<?php if(get_post_meta($post->ID), 'slide-checkbox', true) { ?>**
Why did you use zero index?
Try direct variable:
checked( $slide_stored_meta['slide-checkbox'], "yes")
I seem to have a problem with my meta box saving, not totally sure what I've done wrong. Any help would be greatly appreciated!
Heres my functions file:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'golf.php')
{
add_meta_box (
'golf-times',
'Golf Opening Times & Prices',
'cd_meta_box_cb',
'page',
'normal',
'high'
);
}
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$times = isset( $values['golf_meta_box_times'] ) ? esc_attr( $values['golf_meta_box_times'][0] ) : '';
$prices = isset( $values['golf_meta_box_prices'] ) ? esc_attr( $values['golf_meta_box_prices'][0] ) : '';
wp_nonce_field( 'golf_meta_box_nonce', 'meta_box_nonce' );
?>
<div style="overflow: hidden;">
<div style="width: 45%; float: left;">
<p><label for="golf_meta_box_times">Opening Times</label></p>
<p><textarea type="text" name="golf_meta_box_times" id="golf_meta_box_times" rows="5" style="width: 90%;" value="<?php echo $times; ?>"> </textarea></p>
</div>
<div style="width: 45%; float: left;">
<p><label for="golf_meta_box_prices">Prices</label></p>
<p><textarea type="text" name="golf_meta_box_prices" id="golf_meta_box_prices" rows="5" style="width: 90%;" value="<?php echo $prices; ?>"> </textarea></p>
</div>
</div>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'golf_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array(
'href' => array()
)
);
if( isset( $_POST['golf_meta_box_times'] ) )
update_post_meta( $post_id, 'golf_meta_box_times', wp_kses( $_POST['golf_meta_box_times'], $allowed ) );
if( isset( $_POST['golf_meta_box_prices'] ) )
update_post_meta( $post_id, 'golf_meta_box_prices', wp_kses( $_POST['golf_meta_box_prices'], $allowed ) );
}
?>
If anyone has a clue how to make this save it would be a great help!
I am also having issues making the data display - but I can only assume thats where it isnt actually saving haha!
Cheers
functions.php cannot output HTML as far as i know.
wrap your html in a string and return it, your file might be returning errors.
Your add_meta_box code seems to be correct, the condition might not be met, are you sure you are sending data over get?