admin管理员组

文章数量:1122832

I am developing custom theme from scratch and creates a custom post type and getting this warning while editing custom post type. I also design custom Meta box with two input fields and using nonce in it. Any help removing these warning?

Here is code of custom metabox in functions.php

//Custom Metabox

function register_book_meta_box(){
    add_meta_box('book_meta_box_id', 'Book Detail','design_book_meta_box','books','advanced','high');
}
add_action('add_meta_boxes','register_book_meta_box');

function design_book_meta_box($post){
    wp_nonce_field(basename(__FILE__),'book_cpt_nonce')
    ?>
        <div>
            <label for="book-author">Author Name&nbsp;&nbsp;</label>
            <input type="text" name="book-author" placeholder="Author Name" value="<?php echo get_post_meta( $post->ID, 'book-author-key', true );?>">
        </div>
        <div>
            <label for="year">Published Year</label>
            <input type="number" id="year" name="year" min="1455" max="2020" value="<?php echo get_post_meta( $post->ID, 'book-year-key', true );?>">
            <span id="errorMsg" style="display:none;">Published Year Must be range from 1455 - 2020</span>
        </div>

    <?php
}
function save_book_meta_data($post_id)
{
    if(!isset($_POST['book_cpt_nonce']) || !wp_verify_nonce($_POST['book_cpt_nonce'],basename(__FILE__))){
        return $post_id;
    }
    if (array_key_exists('book-author', $_POST)) {
        update_post_meta( $post_id,'book-author-key', $_POST['book-author']
        );
    }
    if (array_key_exists('year', $_POST)) {
        update_post_meta( $post_id,'book-year-key', $_POST['year']
        );
    }
}
add_action('save_post', 'save_book_meta_data');

I am developing custom theme from scratch and creates a custom post type and getting this warning while editing custom post type. I also design custom Meta box with two input fields and using nonce in it. Any help removing these warning?

Here is code of custom metabox in functions.php

//Custom Metabox

function register_book_meta_box(){
    add_meta_box('book_meta_box_id', 'Book Detail','design_book_meta_box','books','advanced','high');
}
add_action('add_meta_boxes','register_book_meta_box');

function design_book_meta_box($post){
    wp_nonce_field(basename(__FILE__),'book_cpt_nonce')
    ?>
        <div>
            <label for="book-author">Author Name&nbsp;&nbsp;</label>
            <input type="text" name="book-author" placeholder="Author Name" value="<?php echo get_post_meta( $post->ID, 'book-author-key', true );?>">
        </div>
        <div>
            <label for="year">Published Year</label>
            <input type="number" id="year" name="year" min="1455" max="2020" value="<?php echo get_post_meta( $post->ID, 'book-year-key', true );?>">
            <span id="errorMsg" style="display:none;">Published Year Must be range from 1455 - 2020</span>
        </div>

    <?php
}
function save_book_meta_data($post_id)
{
    if(!isset($_POST['book_cpt_nonce']) || !wp_verify_nonce($_POST['book_cpt_nonce'],basename(__FILE__))){
        return $post_id;
    }
    if (array_key_exists('book-author', $_POST)) {
        update_post_meta( $post_id,'book-author-key', $_POST['book-author']
        );
    }
    if (array_key_exists('year', $_POST)) {
        update_post_meta( $post_id,'book-year-key', $_POST['year']
        );
    }
}
add_action('save_post', 'save_book_meta_data');
Share Improve this question edited Oct 13, 2020 at 16:02 Ali Mirza asked Oct 13, 2020 at 15:53 Ali MirzaAli Mirza 113 bronze badges 3
  • If they're duplicate IDs then something must be loading twice. Neither of those IDs in the screenshot are generated by your code though. Why are you checking for if( array_exists() ) instead of if( get_post_meta( 'year', $post->ID, false )? If you're checking for an array because you're generating multiple fields automatically and saving them to an array, then those fields may be the cause of duplicate IDs, although I don't see that in your code. Want me to post how I would do the save as an answer and you can see if that helps? – Tony Djukic Commented Oct 13, 2020 at 17:32
  • @TonyDjukic Sure that would help – Ali Mirza Commented Oct 13, 2020 at 17:50
  • Done. I honestly don't know if change will remove those errors and most likely won't... ...but it's worth a try. Did you check the source of the edit page and see where the duplicate IDs are sitting? Are they even in your meta boxes? – Tony Djukic Commented Oct 13, 2020 at 20:50
Add a comment  | 

1 Answer 1

Reset to default 0

Ali, as mentioned in the comments here's the method I'd use to save. As you can see, it sets out the assorted fields and then runs the save process a single time, rather than setting the logic over and over for each field.

function save_book_meta_data( $post_id ) {
    if( !current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
        if( !isset( $_POST['book_cpt_nonce'] ) || !wp_verify_nonce( $_POST['book_cpt_nonce'], basename( __FILE__ ) ) ) {
            return $post_id;
        }
        $ali_book_meta['book-author-key']       = esc_textarea( $_POST['book-author-key'] );
        $ali_book_meta['book-year-key']         = esc_textarea( $_POST['book-year-key'] );
        foreach( $ali_book_meta as $key => $value ) :
            if( 'revision' === $post->post_type ) {
                return;
            }
            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 );
            }
        endforeach;
}
add_action( 'save_post', 'save_book_meta_data', 1, 2 );

I don't know if that addresses your actual issue but you could give it a try and see what happens.

It does ensure that only users with edit capabilities can make changes and avoids updating revisions because they shouldn't be updated, otherwise they wouldn't be revisions, they'd just be clones.

本文标签: custom post typesFound 2 elements with nonunique id (ajaxnonce) and (wpnonce)