I have a problem with updating my custom meta box in wordpress for a custom post news. I have made a meta box checkbox so I could decide if the post is featured or not. If the checkbox is checked then isFeatured = 1. Until this part it works, the problem is when after sometime I wan't to edit the post and deselect the checkbox. After updating the post the checkbox is checked all the time.
The database: meta_key | meta_value ---- > news_details | a:1:{s:10:"isFeatured";s:1:"1";}
This is how my code looks:
function add_news_details_meta_box() {
add_meta_box(
'news_details_meta_box', // $id
'Checkbox', // $title
'show_news_details_meta_box', // $callback
'news', // $screen
'normal', // $context
'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_news_details_meta_box' );
function show_news_details_meta_box() {
global $post;
$meta = get_post_meta( $post->ID, 'news_details', true ); ?>
<input type="hidden" name="news_details_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
<p>
<label for="news_details[isFeatured]">Is this post featured?
<input type="checkbox" name="news_details[isFeatured]" value="1" <?php if (is_array($meta) && $meta['isFeatured'] === '1' ) { echo 'checked';} else { echo 'Unchecked';}?>>
</label>
</p>
<?php
}
function save_news_details_meta( $post_id ) {
// verify nonce
if ( !wp_verify_nonce( $_POST['news_details_meta_box_nonce'], basename(__FILE__) ) ) {
return $post_id;
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// check permissions
if ( 'page' === $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
$old = get_post_meta( $post_id, 'news_details', true );
$new = $_POST['news_details'];
if ( $new && $new !== $old ) {
update_post_meta( $post_id, 'news_details', $new );
} elseif ( '' === $new && $old ) {
delete_post_meta( $post_id, 'news_details', $old );
}
}
add_action( 'save_post', 'save_news_details_meta' );
Could you explain to me what's wrong with updating the checkbox after sometime?
I think you should check the if condition. The way I got your description, the $meta['isFeatured'] === '1' always becomes true. You set the value to 1 in the input tag. So on save the value of 1 gets saved to the field I guess. On next load you are checking with an if, if the $meta['isFeatured'] is 1, so this is always true.
You could check if the checkbox in the form is checked with:
isset($_POST['name_of_your_checkbox_input'])
If it is set, you save the value of 1. If not, you stay at the pre defined value of 0.
What worked for me in this case was changing } elseif ( '' === $new && $old ) { to } else ( '' === $new && $old ) {
Related
Been banging my head against the wall trying to figure out the solution to my current issue. Simply put, I've created a 'page-home.php' template page, can get a meta box to call on the page template, but trying to update the page with data in the meta box makes the data disappear.
Code below:
function page_add_meta_boxes( $post ) {
global $post;
if(!empty($post))
// get the page template post meta
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
// if the current page uses our specific template, then output our custom metabox
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
// looks for page-home.php file to add our meta box
if($pageTemplate == 'page-home.php' )
{
add_meta_box(
'page-custom-metabox', // $id
'Special Post Meta', // $title
'page_template_metabox', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action( 'add_meta_boxes_page', 'page_add_meta_boxes' );
function page_template_metabox() {
wp_nonce_field( basename( __FILE__ ), 'page_meta_box_nonce' );
$some_string = get_post_meta( $post->ID, '_some_string', true );
?>
<input type="text" name="some-string" value="<?php echo $some_string; ?>" />
<?php
}
function page_save_custom_post_meta() {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action( 'publish_page', 'page_save_custom_post_meta' );
add_action( 'draft_page', 'page_save_custom_post_meta' );
add_action( 'future_page', 'page_save_custom_post_meta' );
Any suggestions are appreciated.
Howdy anyone who runs into this in the future. I was able to get it to work with the code below, someone might be able to follow up with a better answer as to why it works. My thinking is that updating my code to 'save_post_page' instead of just 'save_post' makes the difference. (Note I only changed some info due to trying to test it in my theme):
// Add meta box
function frontpage_meta_boxes( $post ){
global $post;
if(!empty($post))
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-home.php' )
{
add_meta_box( 'frontpage_meta_box', __( 'Features' ), 'frontpage_meta_box', 'page', 'advanced', 'high' );
}
}
}
add_action( 'add_meta_boxes_page', 'frontpage_meta_boxes' );
// builds our meta box
function frontpage_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'frontpage_meta_box_nonce' );
// retrieve the _manuf_url current value
$manufacturer_url = get_post_meta( $post->ID, '_manuf_url', true );
?>
<h3><?php _e( 'Manufacturer URL' ); ?></h3>
<p>
<input type="text" name="manufacturer-url" value="<?php echo $manufacturer_url; ?>" />
</p>
<?php
}
// saves our data
function frontpage_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['frontpage_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['frontpage_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// manufacturer url string
if ( isset( $_REQUEST['manufacturer-url'] ) ) {
update_post_meta( $post_id, '_manuf_url', sanitize_text_field( $_POST['manufacturer-url'] ) );
}
// store custom fields values
}
add_action( 'save_post_page', 'frontpage_save_meta_box_data' );
Try with below code :
function page_save_custom_post_meta($postid) {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action('save_post ','page_save_custom_post_meta');
I'm using a plugin on Woocommerce that adds a new metabox with editable quantity number fields onto the admin back-end.
My theme allows for front-end product posting and so I want to add bring some of these fields to the front-end.
I have managed to add the fields in my template file by using :
<?php
$min = get_post_meta( $post->ID, '_wpbo_minimum', true );
$max = get_post_meta( $post->ID, '_wpbo_maximum', true );
?>
<label for="_wpbo_minimum">Minimum Quantity</label>
<input type="number" name="_wpbo_minimum" value="<?php echo $min; ?>" />
<label for="_wpbo_maximum">Maximum Quantity</label>
<input type="number" name="_wpbo_maximum" value="<?php echo $max; ?>" />
Both fields are showing on my front-end product edit form and getting their values from the backend if these were previously filled in.
But now I am struggling with updating and saving the fields with updated values
from front-end or saving new values from front-end if these values were not previously filled in.
I spent hours finding some tutorial on this, I think I should be concentrating on the following code :
update_post_meta($post->ID, '_wpbo_minimum', $_POST['_wpbo_minimum']);
update_post_meta($post->ID, '_wpbo_maximum', $_POST['_wpbo_maximum']);
But I can't figure out if this is correct and where should this code be placed in order to save or update my fields.
I tried placing a function into my functions.php file that looks like this :
/* Update Minimum Qty field */
add_action('save_post', 'save_min_qty');
function save_min_qty($post_id)
{
if(get_post_type($post_id) != "VA_LISTING_PTYPE")
return;
$min = get_post_meta( $post->ID, '_wpbo_minimum', true );
if ( isset( $_POST['_wpbo_minimum'] )) {
$min = $_POST['_wpbo_minimum'];
}
if( isset( $_POST['_wpbo_minimum'] )) {
if ( $min != 0 ) {
$min = wpbo_validate_number( $min );
}
update_post_meta(
$post_id,
'_wpbo_minimum',
strip_tags( $min )
);
}
But this just doesn't seem to be the winner.
Would you be please able to point me to the right direction ?
Below is the code from the actual original plugin that creates metabox in the back-end, I am not sure if it's any relevant to what I need to do in front-end and what part of this and where should I be using it :
<?php
if ( ! class_exists( 'IPQ_Quantity_Meta_Boxes' ) ) :
class IPQ_Quantity_Meta_Boxes {
public function __construct() {
add_action( 'save_post', array( $this, 'save_quantity_meta_data' ) );
}
/* Handle Saving Meta Box Data */
public function save_quantity_meta_data( $post_id ) {
// Validate Post Type
if ( ! isset( $_POST['post_type'] ) or $_POST['post_type'] !== 'product' ) {
return;
}
// Validate User
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Verify Nonce
if ( ! isset( $_POST["_wpbo_product_rule_nonce"] ) or ! wp_verify_nonce( $_POST["_wpbo_product_rule_nonce"], plugin_basename( __FILE__ ) ) ) {
return;
}
// Update Rule Meta Values
if ( isset( $_POST['_wpbo_minimum'] )) {
$min = $_POST['_wpbo_minimum'];
}
if( isset( $_POST['_wpbo_minimum'] )) {
if ( $min != 0 ) {
$min = wpbo_validate_number( $min );
}
update_post_meta(
$post_id,
'_wpbo_minimum',
strip_tags( $min )
);
}
/* Make sure Max > Min */
if( isset( $_POST['_wpbo_maximum'] )) {
$max = $_POST['_wpbo_maximum'];
if ( isset( $min ) and $max < $min and $max != 0 ) {
$max = $min;
}
update_post_meta(
$post_id,
'_wpbo_maximum',
strip_tags( wpbo_validate_number( $max ) )
);
}
}
}
endif;
Everything is correct except that you are checking for the wrong post_type - it should be "product" not "VA_LISTING_PTYPE". Your function hooked to save_post is returning early because the post type is not correct.
Incorrect
if(get_post_type($post_id) != "VA_LISTING_PTYPE")
return;
Correct
if (get_post_type($post_id) != "product"){
// bail early if we aren't updating a WooCommerce Product
return;
}
You can see the same check in the plugin you are extending here:
https://github.com/wpbackoffice/woocommerce-incremental-product-quantities/blob/master/includes/class-ipq-product-meta-box.php#L222
I am creating a metabox that loops through all the posts of another CPT: 'product'. The metabox lists all the published products in that CPT, puts them in a table row along with an input box. Each input box has a unique ID based off of the CPT product id.
Most of this code I have added based on other posts, but I am having a problem saving the data. I know that it has to do with my value="", my nonce, and $meta_value, but I am not sure where to go next. Below is the code I have so far:
<?php
add_action('admin_init', 'my_theme_on_admin_init');
function my_theme_on_admin_init() {
add_meta_box('my_metabox',
__('Daily Inventory Levels', 'textdomain'),
'my_metabox_render',
'location_inventory', 'normal', 'high'
);
}
function my_metabox_render($post) {
// using an underscore, prevents the meta variable
// from showing up in the custom fields section
global $woocommerce, $post;
$productsList = new WP_Query( 'post_type=product&post_status=publish');
?>
<div class="inside">
<table class="form-table">
<input type="hidden" name="inventory_noncename" id="inventory_noncename" value="<?php wp_create_nonce( 'inventory-nonce' ); ?>" />
<?php
while ( $productsList->have_posts() ) {
$productsList->the_post();
?>
<tr><td><label for="location_inventory_product-<?php the_ID(); ?>"><?php the_title(); ?></label></td><td><input id="location_inventory_product-<?php the_ID(); ?>" name="location_inventory_product-<?php the_ID(); ?>" cols="40" rows="5" value="<?php echo $meta_key; ?>" /></td></tr>
<?php }
wp_reset_postdata();
?>
</table>
</div>
<?php
}
/*update on save*/
add_action('save_post', 'save_postdata_dynamic_inventory_metabox' );
function save_postdata_dynamic_inventory_metabox( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
// Check permissions
if ( 'location_inventory' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id )) {
return $post_id;
}
}
elseif ( !current_user_can( 'edit_post', $post_id )) {
return $post_id;
}
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (isset($_POST['inventory_noncename'])){
if ( !wp_verify_nonce( $_POST['inventory_noncename'], 'inventory-nonce' ) )
return;
} else {
return;
}
// Get the posted data and sanitize it for use as an HTML class.
$new_meta_value = ( isset( $_POST['location_inventory_product-'.the_ID().''] ) ? sanitize_html_class( $_POST['location_inventory_product-'.the_ID().''] ) : '' );
// Get the meta key.
$meta_key = 'location_inventory_product-'.the_ID().'';
// Get the meta value of the custom field key.
$meta_value = get_post_meta( $post_id, $meta_key, true );
// If a new meta value was added and there was no previous value, add it.
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
// If the new meta value does not match the old value, update it.
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
// If there is no new meta value but an old value exists, delete it.
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
} // ends function save_postdata_dynamic_reviews_metabox
Some observations:
1) The hook save_post takes 2 arguments:
add_action( 'save_post', 'save_postdata_dynamic_inventory_metabox', 10, 2 );
function save_postdata_dynamic_inventory_metabox( $post_id, $post_object ) { }
2) I don't think checking for permissions is necessary (at least, never seen it). Just in case, this is an example of the post_object use:
if ( 'location_inventory' == $post_object->post_type )
3) Short this check:
if ( !isset($_POST['inventory_noncename']) || !wp_verify_nonce( $_POST['inventory_noncename'], 'inventory-nonce' ) )
return;
4) The use of the_ID() inside the function save_postdata_dynamic_inventory_metabox is wrong. This is only available inside a Loop and you already have $post_id at hand.
5) Finally, and the most important, the way you're handling the $meta_key is wrong. For one, it's not defined inside the function my_metabox_render. Check this Answer for a working example.
Research in this search query to find more examples.
I want to change the slug of a post with a custom field.
In example, if the custom field is "keyword" my post link will become: mysite.com/keyword.
I wrote this script in fonction.php:
function change_default_slug($id) {
// get part number
$partno = get_post_meta( $id, 'partno', true );
$post_to_update = get_post( $id );
// prevent empty slug, running at every post_type and infinite loop
if ( $partno == '' )
return;
$updated_post = array();
$updated_post['ID'] = $id;
$updated_post['post_name'] = $partno;
wp_update_post( $updated_post ); // update newly created post
}
add_action('save_post', 'change_default_slug');
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', 'post', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</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 anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
$partno = get_post_meta($post->ID,'my_meta_box_text',true);
echo $partno;
This script return me "Fatal error: Maximum execution time of 30 seconds exceeded". But it seems it work because my slug change. Any idea about this issue?
The 'save_post' action gets called by wp_update_post(), so your change_default_slug() function causes an infinite loop. You need to perform a check within change_default_slug() and bail out if the function has already been called:
function change_default_slug($id) {
static $beentheredonethat = false;
if ($beentheredonethat) return;
$beentheredonethat = true;
//do your stuff and save the post...
}
I'm trying to add a checkbox into my custom meta box in WordPress and I ran into a problem with saving it - whenever I check the checkbox and update the post/page, it comes back unchecked again.
Here's the code I'm using:
add_meta_box(
'sl-meta-box-sidebar', // id
'Sidebar On/Off', // title
'sl_meta_box_sidebar', // callback function
'page', // type of write screen
'side', // context
'low' // priority
);
function sl_meta_box_sidebar() {
global $meta; sl_post_meta( $post->ID ); ?>
<input type="checkbox" name="sl_meta[sidebar]" value="<?php echo htmlspecialchars ($meta['sidebar']); ?>" />Check to turn the sidebar <strong>off</strong> on this page.
}
This creates the checkbox in the sidebar of the "Edit Page" screen, as it should, no problem there. I'm not sure what should I enter in the value of the checkbox, with text fields it obviously returns whatever was saved as meta information... I tried just using "checked" instead cause that would be my first guess (then simply check for the value when using this meta data), but it didn't save the checkbox either.
Here's the function that saves all the meta data, which I assume causes this problem:
function sl_save_meta_box( $post_id, $post ) {
global $post, $type;
$post = get_post( $post_id );
if( !isset( $_POST[ "sl_meta" ] ) )
return;
if( $post->post_type == 'revision' )
return;
if( !current_user_can( 'edit_post', $post_id ))
return;
$meta = apply_filters( 'sl_post_meta', $_POST[ "sl_meta" ] );
foreach( $meta as $key => $meta_box ) {
$key = 'meta_' . $key;
$curdata = $meta_box;
$olddata = get_post_meta( $post_id, $key, true );
if( $olddata == "" && $curdata != "" )
add_post_meta( $post_id, $key, $curdata );
elseif( $curdata != $olddata )
update_post_meta( $post_id, $key, $curdata, $olddata );
elseif( $curdata == "" )
delete_post_meta( $post_id, $key );
}
do_action( 'sl_saved_meta', $post );
}
add_action( 'save_post', 'sl_save_meta_box', 1, 2 );
It works perfectly for text fields, but the checkbox just won't save. I'm not sure if the saving function is wrong, or am I missing something about the value of the checkbox.
Any help appreciated!
I had trouble with this previously and here is how I solved it.
First, creating the Checkbox.
<?php
function sl_meta_box_sidebar(){
global $post;
$custom = get_post_custom($post->ID);
$sl_meta_box_sidebar = $custom["sl-meta-box-sidebar"][0];
?>
<input type="checkbox" name="sl-meta-box-sidebar" <?php if( $sl_meta_box_sidebar == true ) { ?>checked="checked"<?php } ?> /> Check the Box.
<?php } ?>
Next, saving.
<?php
add_action('save_post', 'save_details');
function save_details($post_ID = 0) {
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );
if ($post_type) {
update_post_meta($post_ID, "sl-meta-box-sidebar", $_POST["sl-meta-box-sidebar"]);
}
return $post_ID;
} ?>