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;
?>
Related
My live search field fetches the title of the product, thumbnail and add to cart button using AJAX.
It displays the output in singular columns, which is unreadable for a large product return.
How can I update my output so it displays in rows of 4 as below? (below)
Front End
<input type="text" name="keyword" id="keyword" onkeyup="fetch()">
<div id="datafetch">Your numbers will show here</div>
<script>
function fetch(){
$.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action'},
function(response){
$('#datafetch').append(response);
console.log(result);
});
}
</script>
Code in Functions.php
<?php
}// LOTTERY start the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
global $product;
$product = get_product( get_the_ID() ); //set the global product object
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {?>
<h4><?php the_title();?></h4>
<h4><?php the_post_thumbnail();?></h4>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
<?php
}
endwhile;
wp_reset_postdata();
endif;
die();
}
You can use a counter variable. try the below code.
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) : $count = 1; while( $the_query->have_posts() ): $the_query->the_post();
if ( $count %4 == 1 ) {
echo '<div class="row"">';
}
global $product;
$product = get_product( get_the_ID() ); //set the global product object
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {?>
<h4><?php the_title();?></h4>
<h4><?php the_post_thumbnail();?></h4>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
<?php }
if ( $count %4 == 0 ) {
echo "</div>";
}
$count++;
endwhile;
if ( $count %4 != 1 ) echo "</div>";
wp_reset_postdata();
endif;
die();
}
<?php $loop = new WP_Query( array( 'post_type' => 'gallery',
'posts_per_page' => 100 )
);
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if ( get_post_gallery() ) :
/* Loop through all the image and output them one by one */
foreach( $gallery['src'] as $src ) : ?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
endforeach;
endif;
endwhile; wp_reset_query(); ?>
The above code pulls a WordPress gallery from a custom post named "gallery." Then it stores and displays the image. Is there a way to also store the caption of the gallery into a variable?
You can get image captions from WordPress gallery using following code.
<?php $loop = new WP_Query( array( 'post_type' => 'gallery',
'posts_per_page' => 100 )
);
while ( $loop->have_posts() ) : $loop->the_post();
if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :
$img_ids = explode( ',', $gallery['ids'] );
/* Loop through all the image and output them one by one */
foreach( $gallery['src'] as $key => $src ) : ?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
$image_post = get_post( $img_ids[ $key ] ); ?>
<p class="wp-caption-text"><?php echo $image_post->post_excerpt; ?></p>
<?php endforeach;
endif;
endwhile; wp_reset_postdata(); ?>
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
}
?>
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")