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")
Related
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
}
I'm trying to save data from dynamically generated checkboxes in Wordpress meta boxes. For now it almost works - but as you can see each checkbox has the same name and ID which is being used later on, so it cannot be like that.
This is how I create checkboxes:
<?php
$args = array( 'post_type' => 'teachers');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<label for="meta-checkbox-two">
<input type="checkbox" name="meta-checkbox-two" id="meta-checkbox-two" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox-two'] ) ) checked( $prfx_stored_meta['meta-checkbox-two'][0], 'yes' ); ?> />
<?php the_title() ?>
</label>
<?php endwhile; ?>
And here's saving:
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox-two' ] ) ) {
update_post_meta( $post_id, 'meta-checkbox-two', 'yes' );
} else {
update_post_meta( $post_id, 'meta-checkbox-two', '' );
}
As I said it almost works - it saves everything called "meta-checkbox-two" - meaning everything, which is not the goal.
This is where I'm getting lost. I'm trying to make each name and ID end with the post ID that the loop is retrieving. Here's how the code looks then:
Generating checkboxes:
<?php
$args = array( 'post_type' => 'teachers');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<label for="meta-checkbox-two">
<input type="checkbox" name="meta-checkbox-<?php the_ID() ?>" id="meta-checkbox-<?php the_ID() ?>" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox-' . the_ID()] ) ) checked( $prfx_stored_meta['meta-checkbox-'] . the_ID(), 'yes' ); ?> />
<?php the_title() ?>
</label>
<?php endwhile; ?>
Saving them:
$args = array( 'post_type' => 'teachers');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox-'.the_ID()] ) ) {
update_post_meta( $post_id, 'meta-checkbox-'.the_ID(), 'yes' );
} else {
update_post_meta( $post_id, 'meta-checkbox-'.the_ID(), '' );
}
endwhile;
But in the second case the data is not saved. What am I doing wrong?
Just try code below - not tested but should work - I just changed input name on array as you can see name="meta-checkbox-two[]" and that is the point also did unique input id like you did.
<?php
$args = array( 'post_type' => 'teachers');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<label for="meta-checkbox-two">
<input type="checkbox" name="meta-checkbox-two[]" id="meta-checkbox-two-<?php the_ID() ?>" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox-two'] ) ) checked( $prfx_stored_meta['meta-checkbox-two'][0], 'yes' ); ?> />
<?php the_title() ?>
</label>
<?php
endwhile;
?>
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' ) );
}
So I created a checkbox on my wordpress theme. everything saves properly and all of that:
function member_page_featured_meta() {
add_meta_box( 'member_page_meta', __( 'Is this a Member Page?', 'member_page-textdomain' ), 'member_page_meta_callback', 'page', 'side', 'low' );
}
add_action( 'add_meta_boxes', 'member_page_featured_meta' );
/**
* Outputs the content of the meta box
*/
function member_page_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'member_page_nonce' );
$member_page_stored_meta = get_post_meta( $post->ID );
?>
<p>
<div class="member_page-row-content">
<label for="featured-checkbox">
<input type="checkbox" name="featured-checkbox" id="featured-checkbox" value="yes" <?php if ( isset ( $member_page_stored_meta['featured-checkbox'] ) ) checked( $member_page_stored_meta['featured-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Yes', 'member_page-textdomain' )?>
</label>
</div>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function member_page_meta_save( $post_id ) {
// Checks save status - overcome autosave, etc.
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'member_page_nonce' ] ) && wp_verify_nonce( $_POST[ 'member_page_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and saves - save checked as yes and unchecked at no
if( isset( $_POST[ 'featured-checkbox' ] ) ) {
update_post_meta( $post_id, 'featured-checkbox', 'yes' );
} else {
update_post_meta( $post_id, 'featured-checkbox', 'no' );
}
}
add_action( 'save_post', 'member_page_meta_save' );
What I need is to make it so that I can use this to show/hide content in Wordpress using Php, but I don't exactly know how to target it in a php if statement.
Do I use the name (featured-checkbox), id (#featured-checkbox), fuction name ($member_page_featured_meta), etc.
And then to make that a specific bit of HTML, how would I want to go about doing that?
HTML/PHP that I am trying to hide when the box is checked.
<header class="entry-header">
<div style="float:right;">
<div id="membership" "class='false' if not member"></div>
<div id="phone-answering" "class='false' if not phone client"></div>
<div id="location-icon" "class='false' if only one location"></div>
</div>
<?php the_title( '<h1 class="entry-title" style="clear:none; font-size: 3em; text-decoration: underline;">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<div id="tabs">
<ul class="tabs">
<li>Key Intake</li>
<li>Business Facts</li>
<li>Common Answers</li>
<li>Understanding</li>
<li>Current</li>
<li>Locations</li>
<li>Notify</li>
</ul>
<div id="key-intake" class="">
<h1 class="tabtitle">Key Intake</h1>
<?php
// Retrieves the stored value from the database
$key_intake_meta_value = get_post_meta( get_the_ID(), 'Key Intake', true );
// Checks and displays the retrieved value
if( !empty( $key_intake_meta_value ) ) {
echo $key_intake_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="business-facts">
<h1 class="tabtitle">Business Facts</h1>
<?php
// Retrieves the stored value from the database
$business_facts_meta_value = get_post_meta( get_the_ID(), 'Business Facts', true );
// Checks and displays the retrieved value
if( !empty( $business_facts_meta_value ) ) {
echo $business_facts_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="common-answers">
<h1 class="tabtitle">Common Answers</h1>
<?php
// Retrieves the stored value from the database
$common_answers_meta_value = get_post_meta( get_the_ID(), 'Common Answers', true );
// Checks and displays the retrieved value
if( !empty( $common_answers_meta_value ) ) {
echo $common_answers_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="understanding">
<h1 class="tabtitle">Understanding</h1>
<?php
// Retrieves the stored value from the database
$understanding_meta_value = get_post_meta( get_the_ID(), 'Understanding', true );
// Checks and displays the retrieved value
if( !empty( $understanding_meta_value ) ) {
echo $understanding_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="current">
<h1 class="tabtitle">Current</h1>
<?php
// Retrieves the stored value from the database
$current_meta_value = get_post_meta( get_the_ID(), 'Current', true );
// Checks and displays the retrieved value
if( !empty( $current_meta_value ) ) {
echo $current_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="location">
<h1 class="tabtitle">Locations</h1>
<?php
// Retrieves the stored value from the database
$locations_meta_value = get_post_meta( get_the_ID(), 'Locations', true );
// Checks and displays the retrieved value
if( !empty( $locations_meta_value ) ) {
echo $locations_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="notify">
<h1 class="tabtitle">Notify</h1>
<?php
// Retrieves the stored value from the database
$notify_meta_value = get_post_meta( get_the_ID(), 'Notify', true );
// Checks and displays the retrieved value
if( !empty( $notify_meta_value ) ) {
echo $notify_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
</div>
<?php
the_content();
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'odextranet' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
I would use an inline style to hide and show. You can't target HTML from the PHP so the basic process on the theme side would be:
Decide if it should be shown or not.
If it should be hidden use a variable to store the inline CSS used to hide the section.
Output that variable in the container tag for the section that contains the data to hide or show.
Here is an example that uses some of your code:
<?php
// Retrieves the stored value from the database
$current_meta_value = get_post_meta( get_the_ID(), 'Current', true );
//Will either be empty or contain the inline CSS to hide the content.
$hideOrShow = "";
// Checks and displays the retrieved value
if( !empty( $current_meta_value ) ) {
$hideOrShow = "display: none;";
}
?>
<div id="content_to_hide_or_show" style="<?php echo $hideOrShow; ?>">
<!--Content that will be hidden or shown -->
</div>
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
}
?>