Wordpress 管理设置菜单不保存字段

Wordpress admin settings menu not saving fields

我在下面的 class 中设置了一个子菜单和一个选项页面,它没有保存设置字段,我不确定为什么。

class GuidedJourney_Metallic {
    private $guided_journey_options;

    public function __construct() {
        add_action( 'admin_menu', array( $this, 'guided_journey_add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'guided_journey_page_init' ) );
    }

    public function guided_journey_add_plugin_page() {

        add_submenu_page( 
            'guided-journey-home', 
            'Metallic Twill', 
            'Metallic Twill', 
            'manage_options', 
            'guided-journey-metallic', 
            array( $this, 'guided_journey_create_admin_page' )
        );
    }

    public function guided_journey_create_admin_page() {
        $this->guided_journey_options = get_option( 'guided_journey_option_name_m' ); ?>

        <div class="wrap">
            <h2>Metallic Twill Guided Journey</h2>
            <p>Please Enter Options Below</p>
            <?php settings_errors(); ?>

            <form method="post" action="options.php">
                <?php
                    settings_fields( 'guided_journey_option_group' );
                    do_settings_sections( 'guided-journey-admin' );
                    submit_button();
                ?>
            </form>
        </div>
    <?php }

    public function guided_journey_page_init() {
        register_setting(
            'guided_journey_option_group', // option_group
            'guided_journey_option_name_m', // option_name
            array( $this, 'guided_journey_sanitize' ) // sanitize_callback
        );

        add_settings_section(
            'guided_journey_setting_section', // id
            'Settings', // title
            array( $this, 'guided_journey_section_info' ), // callback
            'guided-journey-admin' // page
        );

        add_settings_field(
            '_mid_prod_img_22', // id
            'Stage 1 Middle Image', // title
            array( $this, '_mid_prod_img_22_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_prod_desc_0', // id
            '16.5" Twill Description', // title
            array( $this, '_165_prod_desc_0_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );


        add_settings_field(
            '_165_prod_img_3', // id
            '16.5" Twill Image', // title
            array( $this, '_165_prod_img_3_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_polyback_prod_desc_4', // id
            '16.5" Twill Polyback Description', // title
            array( $this, '_165_polyback_prod_desc_4_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_polyback_prod_img_5', // id
            '16.5" Twill Polyback Image', // title
            array( $this, '_165_polyback_prod_img_5_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_psa_prod_desc_6', // id
            '16.5" Twill PSA Description', // title
            array( $this, '_165_psa_prod_desc_6_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_psa_prod_img_7', // id
            '16.5" Twill PSA Image', // title
            array( $this, '_165_psa_prod_img_7_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_psaperm_prod_desc_8', // id
            '16.5" Twill PSA Perm Description', // title
            array( $this, '_165_psaperm_prod_desc_8_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_psaperm_prod_img_9', // id
            '16.5" Twill PSA Perm Image', // title
            array( $this, '_165_psaperm_prod_img_9_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_permanent_prod_desc_10', // id
            '16.5" Twill Permenant Description', // title
            array( $this, '_165_permanent_prod_desc_10_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_165_permanent_prod_img_11', // id
            '16.5" Twill Premenant Image', // title
            array( $this, '_165_permanent_prod_img_11_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_prod_desc_1', // id
            '51" Product Description', // title
            array( $this, '_51_prod_desc_1_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_prod_img_2', // id
            '51" Product Image', // title
            array( $this, '_51_prod_img_2_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );


        add_settings_field(
            '_51_polyback_prod_desc_12', // id
            '51" Polyback Description', // title
            array( $this, '_51_polyback_prod_desc_12_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_polyback_prod_img_13', // id
            '51" Polyback Image', // title
            array( $this, '_51_polyback_prod_img_13_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_psa_prod_desc_14', // id
            '51" PSA Description', // title
            array( $this, '_51_psa_prod_desc_14_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_psa_prod_img_15', // id
            '51" PSA Image', // title
            array( $this, '_51_psa_prod_img_15_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_psaperm_prod_desc_16', // id
            '51" PSA Perm Description', // title
            array( $this, '_51_psaperm_prod_desc_16_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_psaperm_prod_img_17', // id
            '51" PSA Perm Image', // title
            array( $this, '_51_psaperm_prod_img_17_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_permanent_prod_desc_18', // id
            '51" Permenant Description', // title
            array( $this, '_51_permanent_prod_desc_18_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_permanent_prod_img_19', // id
            '51" Permenant Image', // title
            array( $this, '_51_permanent_prod_img_19_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_uncoated_prod_desc_20', // id
            '51" Uncoated Description', // title
            array( $this, '_51_uncoated_prod_desc_20_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );

        add_settings_field(
            '_51_uncoated_prod_img_21', // id
            '51" Uncoated Image', // title
            array( $this, '_51_uncoated_prod_img_21_callback' ), // callback
            'guided-journey-admin', // page
            'guided_journey_setting_section' // section
        );


    }

    public function guided_journey_sanitize($input) {
        $sanitary_values = array();
        if ( isset( $input['_165_prod_desc_0'] ) ) {
            $sanitary_values['_165_prod_desc_0'] = esc_textarea( $input['_165_prod_desc_0'] );
        }

        if ( isset( $input['_51_prod_desc_1'] ) ) {
            $sanitary_values['_51_prod_desc_1'] = esc_textarea( $input['_51_prod_desc_1'] );
        }

        if ( isset( $input['_51_prod_img_2'] ) ) {
            $sanitary_values['_51_prod_img_2'] = sanitize_text_field( $input['_51_prod_img_2'] );
        }

        if ( isset( $input['_165_prod_img_3'] ) ) {
            $sanitary_values['_165_prod_img_3'] = sanitize_text_field( $input['_165_prod_img_3'] );
        }

        if ( isset( $input['_165_polyback_prod_desc_4'] ) ) {
            $sanitary_values['_165_polyback_prod_desc_4'] = esc_textarea( $input['_165_polyback_prod_desc_4'] );
        }

        if ( isset( $input['_165_polyback_prod_img_5'] ) ) {
            $sanitary_values['_165_polyback_prod_img_5'] = sanitize_text_field( $input['_165_polyback_prod_img_5'] );
        }

        if ( isset( $input['_165_psa_prod_desc_6'] ) ) {
            $sanitary_values['_165_psa_prod_desc_6'] = esc_textarea( $input['_165_psa_prod_desc_6'] );
        }

        if ( isset( $input['_165_psa_prod_img_7'] ) ) {
            $sanitary_values['_165_psa_prod_img_7'] = sanitize_text_field( $input['_165_psa_prod_img_7'] );
        }

        if ( isset( $input['_165_psaperm_prod_desc_8'] ) ) {
            $sanitary_values['_165_psaperm_prod_desc_8'] = esc_textarea( $input['_165_psaperm_prod_desc_8'] );
        }

        if ( isset( $input['_165_psaperm_prod_img_9'] ) ) {
            $sanitary_values['_165_psaperm_prod_img_9'] = sanitize_text_field( $input['_165_psaperm_prod_img_9'] );
        }

        if ( isset( $input['_165_permanent_prod_desc_10'] ) ) {
            $sanitary_values['_165_permanent_prod_desc_10'] = esc_textarea( $input['_165_permanent_prod_desc_10'] );
        }

        if ( isset( $input['_165_permanent_prod_img_11'] ) ) {
            $sanitary_values['_165_permanent_prod_img_11'] = sanitize_text_field( $input['_165_permanent_prod_img_11'] );
        }

        if ( isset( $input['_51_polyback_prod_desc_12'] ) ) {
            $sanitary_values['_51_polyback_prod_desc_12'] = esc_textarea( $input['_51_polyback_prod_desc_12'] );
        }

        if ( isset( $input['_51_polyback_prod_img_13'] ) ) {
            $sanitary_values['_51_polyback_prod_img_13'] = sanitize_text_field( $input['_51_polyback_prod_img_13'] );
        }

        if ( isset( $input['_51_psa_prod_desc_14'] ) ) {
            $sanitary_values['_51_psa_prod_desc_14'] = esc_textarea( $input['_51_psa_prod_desc_14'] );
        }

        if ( isset( $input['_51_psa_prod_img_15'] ) ) {
            $sanitary_values['_51_psa_prod_img_15'] = sanitize_text_field( $input['_51_psa_prod_img_15'] );
        }

        if ( isset( $input['_51_psaperm_prod_desc_16'] ) ) {
            $sanitary_values['_51_psaperm_prod_desc_16'] = esc_textarea( $input['_51_psaperm_prod_desc_16'] );
        }

        if ( isset( $input['_51_psaperm_prod_img_17'] ) ) {
            $sanitary_values['_51_psaperm_prod_img_17'] = sanitize_text_field( $input['_51_psaperm_prod_img_17'] );
        }

        if ( isset( $input['_51_permanent_prod_desc_18'] ) ) {
            $sanitary_values['_51_permanent_prod_desc_18'] = esc_textarea( $input['_51_permanent_prod_desc_18'] );
        }

        if ( isset( $input['_51_permanent_prod_img_19'] ) ) {
            $sanitary_values['_51_permanent_prod_img_19'] = sanitize_text_field( $input['_51_permanent_prod_img_19'] );
        }

        if ( isset( $input['_51_uncoated_prod_desc_20'] ) ) {
            $sanitary_values['_51_uncoated_prod_desc_20'] = esc_textarea( $input['_51_uncoated_prod_desc_20'] );
        }

        if ( isset( $input['_51_uncoated_prod_img_21'] ) ) {
            $sanitary_values['_51_uncoated_prod_img_21'] = sanitize_text_field( $input['_51_uncoated_prod_img_21'] );
        }

        if ( isset( $input['_mid_prod_img_22'] ) ) {
            $sanitary_values['_mid_prod_img_22'] = sanitize_text_field( $input['_mid_prod_img_22'] );
        }


        return $sanitary_values;
    }

    public function guided_journey_section_info() {

    }

    public function _165_prod_desc_0_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_165_prod_desc_0]" id="_165_prod_desc_0">%s</textarea>',
            isset( $this->guided_journey_options['_165_prod_desc_0'] ) ? esc_attr( $this->guided_journey_options['_165_prod_desc_0']) : ''
        );
    }

    public function _51_prod_desc_1_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_51_prod_desc_1]" id="_51_prod_desc_1">%s</textarea>',
            isset( $this->guided_journey_options['_51_prod_desc_1'] ) ? esc_attr( $this->guided_journey_options['_51_prod_desc_1']) : ''
        );
    }

    public function _51_prod_img_2_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_51_prod_img_2]" id="_51_prod_img_2" value="%s">',
            isset( $this->guided_journey_options['_51_prod_img_2'] ) ? esc_attr( $this->guided_journey_options['_51_prod_img_2']) : ''
        );
    }

    public function _165_prod_img_3_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_165_prod_img_3]" id="_165_prod_img_3" value="%s">',
            isset( $this->guided_journey_options['_165_prod_img_3'] ) ? esc_attr( $this->guided_journey_options['_165_prod_img_3']) : ''
        );
    }

    public function _165_polyback_prod_desc_4_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_165_polyback_prod_desc_4]" id="_165_polyback_prod_desc_4">%s</textarea>',
            isset( $this->guided_journey_options['_165_polyback_prod_desc_4'] ) ? esc_attr( $this->guided_journey_options['_165_polyback_prod_desc_4']) : ''
        );
    }

    public function _165_polyback_prod_img_5_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_165_polyback_prod_img_5]" id="_165_polyback_prod_img_5" value="%s">',
            isset( $this->guided_journey_options['_165_polyback_prod_img_5'] ) ? esc_attr( $this->guided_journey_options['_165_polyback_prod_img_5']) : ''
        );
    }

    public function _165_psa_prod_desc_6_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_165_psa_prod_desc_6]" id="_165_psa_prod_desc_6">%s</textarea>',
            isset( $this->guided_journey_options['_165_psa_prod_desc_6'] ) ? esc_attr( $this->guided_journey_options['_165_psa_prod_desc_6']) : ''
        );
    }

    public function _165_psa_prod_img_7_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_165_psa_prod_img_7]" id="_165_psa_prod_img_7" value="%s">',
            isset( $this->guided_journey_options['_165_psa_prod_img_7'] ) ? esc_attr( $this->guided_journey_options['_165_psa_prod_img_7']) : ''
        );
    }

    public function _165_psaperm_prod_desc_8_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_165_psaperm_prod_desc_8]" id="_165_psaperm_prod_desc_8">%s</textarea>',
            isset( $this->guided_journey_options['_165_psaperm_prod_desc_8'] ) ? esc_attr( $this->guided_journey_options['_165_psaperm_prod_desc_8']) : ''
        );
    }

    public function _165_psaperm_prod_img_9_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_165_psaperm_prod_img_9]" id="_165_psaperm_prod_img_9" value="%s">',
            isset( $this->guided_journey_options['_165_psaperm_prod_img_9'] ) ? esc_attr( $this->guided_journey_options['_165_psaperm_prod_img_9']) : ''
        );
    }

    public function _165_permanent_prod_desc_10_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_165_permanent_prod_desc_10]" id="_165_permanent_prod_desc_10">%s</textarea>',
            isset( $this->guided_journey_options['_165_permanent_prod_desc_10'] ) ? esc_attr( $this->guided_journey_options['_165_permanent_prod_desc_10']) : ''
        );
    }

    public function _165_permanent_prod_img_11_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_165_permanent_prod_img_11]" id="_165_permanent_prod_img_11" value="%s">',
            isset( $this->guided_journey_options['_165_permanent_prod_img_11'] ) ? esc_attr( $this->guided_journey_options['_165_permanent_prod_img_11']) : ''
        );
    }

    public function _51_polyback_prod_desc_12_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_51_polyback_prod_desc_12]" id="_51_polyback_prod_desc_12">%s</textarea>',
            isset( $this->guided_journey_options['_51_polyback_prod_desc_12'] ) ? esc_attr( $this->guided_journey_options['_51_polyback_prod_desc_12']) : ''
        );
    }

    public function _51_polyback_prod_img_13_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_51_polyback_prod_img_13]" id="_51_polyback_prod_img_13" value="%s">',
            isset( $this->guided_journey_options['_51_polyback_prod_img_13'] ) ? esc_attr( $this->guided_journey_options['_51_polyback_prod_img_13']) : ''
        );
    }

    public function _51_psa_prod_desc_14_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_51_psa_prod_desc_14]" id="_51_psa_prod_desc_14">%s</textarea>',
            isset( $this->guided_journey_options['_51_psa_prod_desc_14'] ) ? esc_attr( $this->guided_journey_options['_51_psa_prod_desc_14']) : ''
        );
    }

    public function _51_psa_prod_img_15_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_51_psa_prod_img_15]" id="_51_psa_prod_img_15" value="%s">',
            isset( $this->guided_journey_options['_51_psa_prod_img_15'] ) ? esc_attr( $this->guided_journey_options['_51_psa_prod_img_15']) : ''
        );
    }

    public function _51_psaperm_prod_desc_16_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_51_psaperm_prod_desc_16]" id="_51_psaperm_prod_desc_16">%s</textarea>',
            isset( $this->guided_journey_options['_51_psaperm_prod_desc_16'] ) ? esc_attr( $this->guided_journey_options['_51_psaperm_prod_desc_16']) : ''
        );
    }

    public function _51_psaperm_prod_img_17_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_51_psaperm_prod_img_17]" id="_51_psaperm_prod_img_17" value="%s">',
            isset( $this->guided_journey_options['_51_psaperm_prod_img_17'] ) ? esc_attr( $this->guided_journey_options['_51_psaperm_prod_img_17']) : ''
        );
    }

    public function _51_permanent_prod_desc_18_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_51_permanent_prod_desc_18]" id="_51_permanent_prod_desc_18">%s</textarea>',
            isset( $this->guided_journey_options['_51_permanent_prod_desc_18'] ) ? esc_attr( $this->guided_journey_options['_51_permanent_prod_desc_18']) : ''
        );
    }

    public function _51_permanent_prod_img_19_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_51_permanent_prod_img_19]" id="_51_permanent_prod_img_19" value="%s">',
            isset( $this->guided_journey_options['_51_permanent_prod_img_19'] ) ? esc_attr( $this->guided_journey_options['_51_permanent_prod_img_19']) : ''
        );
    }

    public function _51_uncoated_prod_desc_20_callback() {
        printf(
            '<textarea class="large-text" rows="5" name="guided_journey_option_name[_51_uncoated_prod_desc_20]" id="_51_uncoated_prod_desc_20">%s</textarea>',
            isset( $this->guided_journey_options['_51_uncoated_prod_desc_20'] ) ? esc_attr( $this->guided_journey_options['_51_uncoated_prod_desc_20']) : ''
        );
    }

    public function _51_uncoated_prod_img_21_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_51_uncoated_prod_img_21]" id="_51_uncoated_prod_img_21" value="%s">',
            isset( $this->guided_journey_options['_51_uncoated_prod_img_21'] ) ? esc_attr( $this->guided_journey_options['_51_uncoated_prod_img_21']) : ''
        );
    }

    public function _mid_prod_img_22_callback() {
        printf(
            '<input class="regular-text" type="text" name="guided_journey_option_name[_mid_prod_img_22]" id="_mid_prod_img_22" value="%s">',
            isset( $this->guided_journey_options['_mid_prod_img_22'] ) ? esc_attr( $this->guided_journey_options['_mid_prod_img_22']) : ''
        );
    }

}

我在插件中使用它,我可以看到在 wordpress 设置中创建的选项 table,但是当我单击保存时,none 的字段被添加到数据库中。

感谢任何帮助!

这是一个没有根据的猜测,但我们都是来学习的,对吗?

鼻涕虫在你的:

settings_fields( 'guided_journey_option_group' );
do_settings_sections( 'guided-journey-admin' );

正确吗?

The codex 说他们应该是 "pass slug name of page"。您的子菜单页面的 slug 看起来是 "guided-journey-metallic"?那么也许试试这个?

settings_fields( 'guided-journey-metallic' );
do_settings_sections( 'guided-journey-metallic' );