custom fields content is not saving after update WordPress 6.1
-
after I upgrade the wordpress to 6.1 all my website got the issues and you can’t see from the user url only on the edit mode, my fields that I added using meta-box are there but when I add the content and update the page the content of the fields are gone.
-
How did you create the custom fields? With the help of ACF?
I’m using meta-box creating a php file below is a sample of my code for custom fields for pages but I also use meta-box for custom post-type
<?php add_action( 'add_meta_boxes', 'add_common_meta_box' ); function add_common_meta_box() { global $post; add_meta_box( 'common_id', 'Pages Custom Meta Box', 'common_meta_box_callback', 'page', 'normal', 'high' ); } function common_meta_box_callback() { global $post; echo '<input type="hidden" name="common_noncename" id="common_noncename" value="' . wp_create_nonce( plugin_basename( __FILE__ ) ) . '" />'; $single_image = get_post_meta( $post->ID, 'single_image', true ); $header_slider = get_post_meta( $post->ID, 'header_slider', true ); $content_single_image = get_post_meta( $post->ID, 'content_single_image', true ); $maincontent_title = get_post_meta( $post->ID, 'maincontent_title', true ); $pagebuttontext = get_post_meta( $post->ID, 'pagebuttontext', true ); $pagebuttonurl = get_post_meta( $post->ID, 'pagebuttonurl', true ); $pagebuttonurltitle = get_post_meta( $post->ID, 'pagebuttonurltitle', true ); ?> <div class="cc-wrapper clear"> <div class="cc-container"> <label for="maincontent_title">H1 - Main Title</label> <input type="text" name="maincontent_title" value="<?php echo $maincontent_title; ?>" class="input widefat" /> <br><br> </div> <div class="cc-container"> <label for="single_image">Main Image - Single Image</label> <a id="single_image" class="button meta-button">Add Main Image</a> <input id="single_image_box" type="text" name="single_image" value="<?php echo $single_image; ?>" class="input widefat" /> <a class="remove-upload" href="#">Remove Image</a> <br><br> </div> <div class="cc-container"> <label for="header_slider">Header Slider - First Create the slider on Slider Revolution</label> <input type="text" name="header_slider" value="<?php echo $header_slider; ?>" class="input widefat" /> <br><br> </div> <div class="cc-container"> <label for="content_single_image">Content Image - Single Image</label> <a id="content_single_image" class="button meta-button">Add Main Image</a> <input id="content_single_image_box" type="text" name="content_single_image" value="<?php echo $content_single_image; ?>" class="input widefat" /> <a class="remove-upload" href="#">Remove Image</a> <br><br> </div> <div class="cc-container"> <label for="pagebuttontext">Type the Button Label</label> <input type="text" name="pagebuttontext" value="<?php echo $pagebuttontext; ?>" class="input widefat" /> <br><br> </div> <div class="cc-container"> <label for="pagebuttonurl">Type the Button url link</label> <input type="text" name="pagebuttonurl" value="<?php echo $pagebuttonurl; ?>" class="input widefat" /> <br><br> </div> <div class="cc-container"> <label for="pagebuttonurltitle">Type the Button url link Title Tag (ADA)</label> <input type="text" name="pagebuttonurltitle" value="<?php echo $pagebuttonurltitle; ?>" class="input widefat" /> <br><br> </div> </div> <?php wp_register_script( 'media-uploader', get_stylesheet_directory_uri() . '/includes/php/meta-boxes/meta-uploader.js', array( 'jquery' ), '5.5.0', true ); wp_enqueue_script( 'media-uploader' ); wp_enqueue_script( 'media-upload' ); wp_enqueue_script( 'thickbox' ); wp_enqueue_style( 'thickbox' ); } add_action( 'save_post', 'save_common_info', 1, 2 ); function save_common_info( $post_id, $post ) { if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if( !isset( $_POST[ 'common_noncename' ] ) ) return; if( !current_user_can( 'edit_post' ) ) return; $common_meta[ 'single_image' ] = $_POST[ 'single_image' ]; $common_meta[ 'header_slider' ] = $_POST[ 'header_slider' ]; $common_meta[ 'content_single_image' ] = $_POST[ 'content_single_image' ]; $common_meta[ 'maincontent_title' ] = $_POST[ 'maincontent_title' ]; $common_meta[ 'pagebuttontext' ] = $_POST[ 'pagebuttontext' ]; $common_meta[ 'pagebuttonurl' ] = $_POST[ 'pagebuttonurl' ]; $common_meta[ 'pagebuttonurltitle' ] = $_POST[ 'pagebuttonurltitle' ]; foreach( $common_meta as $key => $value ) { if( $post->post_type == 'revision' ) return; $value = implode( ',', (array)$value ); if( get_post_meta( $post->ID, $key, false ) ) { update_post_meta( $post->ID, $key, $value ); } else { add_post_meta( $post->ID, $key, $value ); } if( !$value ) delete_post_meta( $post->ID, $key ); } }Change
if( !current_user_can( 'edit_post') ) return;to
if( !current_user_can( 'edit_post', $post_id ) ) return;Reason for this requirement is described here in the ticket: https://core.trac.wordpress.org/ticket/56962
By the way, you can also omit the
noncecheck in your own code. WordPress already does that itself for the surrounding form.thanks @threadi for the answer, but not sure where to I change the lines I don’t have that lines on my meta-box PHP file, is this in a specific place or file that I need to make the update?
The line is in your
save_common_info()function.thanks @threadi it works…
thanks @threadi it works…
The topic ‘custom fields content is not saving after update WordPress 6.1’ is closed to new replies.