最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom field - non-unique #_ajax_nonce id in browser console

programmeradmin1浏览0评论

I have added multiple custom fields through a meta-box, and i have used wp_nonce_field function that generates a nonce for each field and every thing is working for This meta box, but also WP renders those custom fields below the meta box, but without unique nonces, which displays a console error of non unique nonce id for those fields. So now i have my meta-box with it's fields and again those fields are rendered by WordPress down with same values under custom fields section, how to get rid of this or fix this problem.

Here is how i added fields.. Actually i have a class that adds metabox with the following method

/**
 * Add metaboxes.
 */
public function add_meta_box(){
        if( is_array( $this->post_type ) ){

            foreach ( $this->post_type as $post_type ) {
                add_meta_box( $this->id, $this->label, array( $this, 'meta_fields_callback' ), $post_type, $this->context, $this->priority );
            }

        }else{

            add_meta_box( $this->id, $this->label, array( $this, 'meta_fields_callback' ), $this->post_type, $this->context, $this->priority );

        }
    }

which uses the callable meta_fields_callback

/**
 * Render metabox' fields.
 */
public function meta_fields_callback(){
        global $post;

        //Array of inputs that have same HTML markup
        $mixed_types = ['text','number','email', 'password','url'];

        //Loop through inputs to render
        foreach($this->fields as $field){

            $render_field = new ANONY_Input_Field($field, 'meta', $post->ID);

            $render_field->field_init();

            if(isset($field['scripts']) && !empty($field['scripts'])){

                foreach($field['scripts'] as $script){

                    $deps = (isset($script['dependancies']) && !empty($script['dependancies'])) ? $script['dependancies'] : [];

                    $deps[] = 'anony-metaboxs';

                    if(isset($script['file_name'])){

                        $url = ANONY_MB_URI. 'assets/js/'.$script['file_name'].'.js';

                    }elseif(isset($script['url'])){

                        $url = $script['url'];
                    }

                    wp_enqueue_script($script['handle'], $url, $deps, false, true);
                }
            }



        }
        wp_referer_field();
    }

The callable will render a field depending on a method of name field_init() which is defined in a separate class on name ANONY_Input_Field the field_init is:

/**
 * Initialize options field
 */
public function field_init(){

        if(!is_null($this->field_class) && class_exists($this->field_class))
        {

            $field_class = $this->field_class;

            $field = new $field_class($this);

            if($this->context == 'meta'){
                wp_nonce_field( $this->field['id'].'_action', $this->field['id'].'_nonce', false );
            }
            $field->render();


        }
    }

In this method i use wp_nonce_field function to generate the nonce for each field.

All theses are working as expected but the problem lies in fields that are rendered automatically in custom fields section into edit post screen.

发布评论

评论列表(0)

  1. 暂无评论