【问题标题】:One Theme Custom Options page, drops values in another Theme Custom Options Menu page一个主题自定义选项页面,在另一个主题自定义选项菜单页面中删除值
【发布时间】:2021-08-18 18:13:52
【问题描述】:

我正在为我正在构建的自定义主题创建菜单页面,并为每个选项页面创建以下类:

    <?php
/**
 * Create A Simple Theme Options Panel
 *
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
// Start Class
if ( ! class_exists('Hello_Theme_Scrolling_Settings_Options') ) {

    class Hello_Theme_Scrolling_Settings_Options {

        /**
         * Start things up
         *
         * @since 1.0.0
         */
        public function __construct() {

            // We only need to register the admin panel on the back-end
            if ( is_admin() ) {
                add_action( 'admin_menu', array('Hello_Theme_Scrolling_Settings_Options', 'add_admin_menu' ) );
                add_action( 'admin_init', array('Hello_Theme_Scrolling_Settings_Options', 'register_settings' ) );
            }

        }

        /**
         * Returns all theme options
         *
         * @since 1.0.0
         */
        public static function get_theme_options() {
            return get_option( 'theme_options' );
        }

        /**
         * Returns single theme option
         *
         * @since 1.0.0
         */
        public static function get_theme_option( $id ) {
            $options = self::get_theme_options();
            if ( isset( $options[$id] ) ) {
                return $options[$id];
            }
        }

        /**
         * Add sub menu page
         *
         * @since 1.0.0
         */
        public static function add_admin_menu() {
            add_submenu_page('theme-settings',
                esc_html__( 'Scrolling Settings', 'hello-elementor' ),
                esc_html__( 'Scrolling Settings', 'hello-elementor' ),
                'manage_options',
                'scrolling-settings',
                array('Hello_Theme_Scrolling_Settings_Options', 'create_admin_page' ),
                1
            );
        }

        /**
         * Register a setting and its sanitization callback.
         *
         * We are only registering 1 setting so we can store all options in a single option as
         * an array. You could, however, register a new setting for each option
         *
         * @since 1.0.0
         */
        public static function register_settings() {
            register_setting( 'theme_options', 'theme_options', array('Hello_Theme_Scrolling_Settings_Options', 'sanitize' ) );
        }

        /**
         * Sanitization callback
         *
         * @since 1.0.0
         */
        public static function sanitize( $options ) {

            // If we have options lets sanitize them
            if ( $options ) {
                // Enable Scrolling
                if ( ! empty( $options['scrolling_enable'] ) ) {
                    $options['scrolling_enable'] = sanitize_text_field( $options['scrolling_enable'] );
                } else {
                    unset( $options['scrolling_enable'] ); // Remove from options if empty
                }

                // Scrolling Animation Path
                if ( ! empty( $options['scrolling_speed'] ) ) {
                    $options['scrolling_speed'] = sanitize_text_field( $options['scrolling_speed'] );
                } else {
                    unset( $options['scrolling_speed'] ); // Remove from options if empty
                }

                // Scrolling Animation Step
                if ( ! empty( $options['scrolling_step'] ) ) {
                    $options['scrolling_step'] = sanitize_text_field( $options['scrolling_step'] );
                } else {
                    unset( $options['scrolling_step'] ); // Remove from options if empty
                }

                // Cursor Color
                if ( ! empty( $options['cursor_color'] ) ) {
                    $options['cursor_color'] = sanitize_text_field( $options['cursor_color'] );
                } else {
                    unset( $options['cursor_color'] ); // Remove from options if empty
                }

                // Scrollbar z-index
                if ( ! empty( $options['scrollbar_zindex'] ) ) {
                    $options['scrollbar_zindex'] = sanitize_text_field( $options['scrollbar_zindex'] );
                } else {
                    unset( $options['scrollbar_zindex'] ); // Remove from options if empty
                }

                // Scrollbar Border size
                if ( ! empty( $options['scrollbar_border_size'] ) ) {
                    $options['scrollbar_border_size'] = sanitize_text_field( $options['scrollbar_border_size'] );
                } else {
                    unset( $options['scrollbar_border_size'] ); // Remove from options if empty
                }

                // Scrollbar Border radius
                if ( ! empty( $options['scrollbar_border_radius'] ) ) {
                    $options['scrollbar_border_radius'] = sanitize_text_field( $options['scrollbar_border_radius'] );
                } else {
                    unset( $options['scrollbar_border_radius'] ); // Remove from options if empty
                }

                // Scrollbar Border color
                if ( ! empty( $options['scrollbar_border_color'] ) ) {
                    $options['scrollbar_border_color'] = sanitize_text_field( $options['scrollbar_border_color'] );
                } else {
                    unset( $options['scrollbar_border_color'] ); // Remove from options if empty
                }

                // Scrollbar Border style
                if ( ! empty( $options['scrollbar_border_type'] ) ) {
                    $options['scrollbar_border_type'] = sanitize_text_field( $options['scrollbar_border_type'] );
                } else {
                    unset( $options['scrollbar_border_type'] ); // Remove from options if empty
                }

                // Scrollbar width
                if ( ! empty( $options['scrollbar_width'] ) ) {
                    $options['scrollbar_width'] = sanitize_text_field( $options['scrollbar_width'] );
                } else {
                    unset( $options['scrollbar_width'] ); // Remove from options if empty
                }


            }

            // Return sanitized options
            return $options;

        }

        public static function scrolling_form_display(){
            ?>

            <?php settings_fields( 'theme_options' ); ?>

            <table class="form-table wpex-custom-admin-login-table">

                <?php // Text input example ?>


                <div id="edit-form-content">

                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Enable Nice Scrolling', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $excempt_admins = self::get_theme_option( 'scrolling_enable' ); ?>
                            <input type="checkbox" name="theme_options[scrolling_enable]" value="theme_options[scrolling_enable]" <?php if(esc_attr( $excempt_admins )) : ?> checked <?php endif;?>">
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Cursor Color', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $value = self::get_theme_option( 'cursor_color' ); ?>
                            <input class="my-color-field" type="text" name="theme_options[cursor_color]" value="<?php echo esc_attr( $value ); ?>" data-default-color="#effeff">
                            <br><?php esc_html_e( 'Enter the Cursor Color', 'hello-elementor' ); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scroll Speed', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $value = self::get_theme_option( 'scrolling_speed' ); ?>
                            <input type="number" min="10" step="1" name="theme_options[scrolling_speed]" value="<?php echo esc_attr( $value ); ?>">
                            <br><?php esc_html_e( 'Enter the Scrolling Speed', 'hello-elementor' ); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scroll Step', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $value = self::get_theme_option( 'scrolling_step' ); ?>
                            <input type="number" min="10" step="1" name="theme_options[scrolling_step]" value="<?php echo esc_attr( $value ); ?>">
                            <br><?php esc_html_e( 'Enter the Scrolling Step', 'hello-elementor' ); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scrollbar Width', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $value = self::get_theme_option( 'scrollbar_width' ); ?>
                            <input type="number" min="1" step="1" name="theme_options[scrollbar_width]" value="<?php echo esc_attr( $value ); ?>">
                            <br><?php esc_html_e( 'Enter the Scrollbar Width', 'hello-elementor' ); ?>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scrollbar Border Size', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $scrollbar_border_size_value = self::get_theme_option( 'scrollbar_border_size' ); ?>
                            <?php esc_html_e( 'px', 'hello-elementor' ); ?>
                            <input type="number" min="1" step="1" name="theme_options[scrollbar_border_size]" value="<?php echo esc_attr( $scrollbar_border_size_value ); ?>">
                            <br><?php esc_html_e( 'Enter the Scrollbar Border size', 'hello-elementor' ); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scrollbar Border Radius', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $scrollbar_border_radius_value = self::get_theme_option( 'scrollbar_border_radius' ); ?>
                            <?php esc_html_e( 'px', 'hello-elementor' ); ?>
                            <input type="number" min="0" step="1" name="theme_options[scrollbar_border_radius]" value="<?php echo esc_attr( $scrollbar_border_radius_value ); ?>">
                            <br><?php esc_html_e( 'Enter the Scrollbar Border Radius', 'hello-elementor' ); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scrollbar Border color', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $value = self::get_theme_option( 'scrollbar_border_color' ); ?>
                            <input class="my-color-field" type="text" name="theme_options[scrollbar_border_color]" value="<?php echo esc_attr( $value ); ?>" data-default-color="#effeff">
                            <br><?php esc_html_e( 'Enter the Scrollbar Border Color', 'hello-elementor' ); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scrollbar Border type', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $value = self::get_theme_option( 'scrollbar_border_color' );
                            $select_options = ['dotted' => __('Dotted', 'hello-elementor' ),
                                        'dashed' => __('Dashed', 'hello-elementor' ),
                                        'solid' => __('Solid', 'hello-elementor' ),
                                        'double' => __('Double', 'hello-elementor' ),
                                        'groove' => __('Groove', 'hello-elementor' ),
                                        'ridge' => __('Ridge', 'hello-elementor' ),
                                        'inset' => __('Inset', 'hello-elementor' ),
                                        'outset' => __('Outset', 'hello-elementor' ),
                                        'none' => __('None', 'hello-elementor' ),
                                        'hidden' => __('Hidden', 'hello-elementor' )];
                            ?>
                            <select name="theme_options[scrollbar_border_type]" id="scrollbar_border_type_setting">
                                <?php
                                foreach ($select_options as $option_key => $option_value){
                                    if ($option_key == self::get_theme_option('scrollbar_border_type')){
                                        echo '<option value='.$option_key.' selected>'. $option_value. '</option>';
                                        continue;
                                    }
                                    echo '<option value='.$option_key.'>'. $option_value. '</option>';
                                }
                                ?>
                            </select>
                            <br><?php esc_html_e( 'Choose the border continuity style', 'hello-elementor' ); ?>
                        </td>
                    </tr>


                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Scrollbar Z-index', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $value = self::get_theme_option( 'scrollbar_zindex' ); ?>
                            <input type="number" min="0" step="1" name="theme_options[scrollbar_zindex]" value="<?php echo esc_attr( $value ); ?>">

                            <br><?php esc_html_e( 'Enter the Scrollbar Z-index', 'hello-elementor' ); ?>
                        </td>
                    </tr>

                    <tr valign="middle"><td><h2>בקרוב</h2></td></tr>
                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Opacity Range', 'hello-elementor' ); ?> (בפיתוח)</th>
                        <td style="padding: 0 0 0 0">

                            <table style="padding: 0 0 0 0">
                                <tr style="padding: 0 0 0 0">
                                    <td style="text-align: center;padding: 0 0 0 0"><span id="slider_value2" style="color:red;font-weight:bold;"></span></td>
                                </tr>
                                <tr style="padding: 0 0 0 0">
                                    <td style="padding: 0 0 0 0">0 <input type="range" min="0" max="1" step="0.01" name="sld6" value=477" onchange="show_value2(this.value)"> 1</td>
                                </tr>
                            </table>


                        </td>
                    </tr>
                    <script>
                        function show_value2(x)
                        {
                            document.getElementById("slider_value2").innerHTML=x;
                        }

                    </script>
                </div>
            </table>

            <?php submit_button(); ?>


            <?php
        }

        /**
         * Settings page output
         *
         * @since 1.0.0
         */
        public static function create_admin_page() { ?>
            <div class="wrap">

                <h1><?php esc_html_e( 'Scrolling & Cursor', 'hello-elementor' ); ?></h1>


                <form method="post" action="options.php">
                        <h3><?php esc_html_e( 'Scrolling Bar', 'hello-elementor' ); ?></h3>
                        <?php self::scrolling_form_display(); ?>
                </form>

            </div><!-- .wrap -->

        <?php }

    }
}
# Class Driver - Initiates class on require
new Hello_Theme_Scrolling_Settings_Options();
function Hello_get_scrolling_option( $id = '' ) {
    return Hello_Theme_Scrolling_Settings_Options::get_theme_option( $id );
}

第二课

<?php
/**
 * Create A Simple Theme Options Panel
 *
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
// Start Class
if ( ! class_exists('Hello_Theme_Preloader_Settings_Options') ) {

    class Hello_Theme_Preloader_Settings_Options {

        /**
         * Start things up
         *
         * @since 1.0.0
         */
        public function __construct() {

            // We only need to register the admin panel on the back-end
            if ( is_admin() ) {
                add_action( 'admin_menu', array('Hello_Theme_Preloader_Settings_Options', 'add_admin_menu' ) );
                add_action( 'admin_init', array('Hello_Theme_Preloader_Settings_Options', 'register_settings' ) );
            }

        }

        /**
         * Returns all theme options
         *
         * @since 1.0.0
         */
        public static function get_theme_options() {
            return get_option( 'theme_options' );
        }

        /**
         * Returns single theme option
         *
         * @since 1.0.0
         */
        public static function get_theme_option( $id ) {
            $options = self::get_theme_options();
            if ( isset( $options[$id] ) ) {
                return $options[$id];
            }
        }

        /**
         * Add sub menu page
         *
         * @since 1.0.0
         */
        public static function add_admin_menu() {
            add_submenu_page('theme-settings',
                esc_html__( 'Preloader Settings', 'hello-elementor' ),
                esc_html__( 'Preloader Settings', 'hello-elementor' ),
                'manage_options',
                'preloader-settings',
                array('Hello_Theme_Preloader_Settings_Options', 'create_admin_page' ),
                1
            );
        }

        /**
         * Register a setting and its sanitization callback.
         *
         * We are only registering 1 setting so we can store all options in a single option as
         * an array. You could, however, register a new setting for each option
         *
         * @since 1.0.0
         */
        public static function register_settings() {
            register_setting( 'theme_options', 'theme_options', array('Hello_Theme_Preloader_Settings_Options', 'sanitize' ) );
        }

        /**
         * Sanitization callback
         *
         * @since 1.0.0
         */
        public static function sanitize( $options ) {

            // If we have options lets sanitize them
            if ( $options ) {
                // Enable Preloader
                if ( ! empty( $options['preloader_enable'] ) ) {
                    $options['preloader_enable'] = sanitize_text_field( $options['preloader_enable'] );
                } else {
                    unset( $options['preloader_enable'] ); // Remove from options if empty
                }

                // Preloader Aniamtion Path
                if ( ! empty( $options['preloader_delay_page'] ) ) {
                    $options['preloader_delay_page'] = sanitize_text_field( $options['preloader_delay_page'] );
                } else {
                    unset( $options['preloader_delay_page'] ); // Remove from options if empty
                }
//                // Delay Loading
//                if ( ! empty( $options['preloader_animation_path'] ) ) {
//                    $options['preloader_animation_path'] = sanitize_text_field( $options['preloader_animation_path'] );
//                } else {
//                    unset( $options['preloader_animation_path'] ); // Remove from options if empty
//                }

            }

            // Return sanitized options
            return $options;

        }

        public static function preloader_form_display(){
            ?>

            <?php settings_fields( 'theme_options' ); ?>

            <table class="form-table wpex-custom-admin-login-table">

                <?php // Text input example ?>


                <div id="edit-form-content">

                    <tr valign="top">
                        <th scope="row"><?php esc_html_e( 'Enable Preloader', 'hello-elementor' ); ?></th>
                        <td>
                            <?php $excempt_admins = self::get_theme_option( 'preloader_enable' ); ?>
                            <input type="checkbox" name="theme_options[preloader_enable]" value="theme_options[hotjar_excempt_admins]" <?php if(esc_attr( $excempt_admins )) : ?> checked <?php endif;?>">
                        </td>
                    </tr>
<!--                    <tr valign="top">-->
<!--                        <th scope="row">--><?php //esc_html_e( 'Delay Loading', 'hello-elementor' ); ?><!--</th>-->
<!--                        <td>-->
<!--                            --><?php //$value = self::get_theme_option( 'preloader_delay_page' ); ?>
<!--                            <input type="number" min="500" step="100" name="theme_options[preloader_delay_page]" value="--><?php //echo esc_attr( $value ); ?><!--">-->
<!--                            <br>--><?php //esc_html_e( 'Enter the minimal time you want the preloader to appear (in Milliseconds: 1000ms = 1s)', 'hello-elementor' ); ?>
<!--                        </td>-->
<!--                    </tr>-->
                    <tr valign="top">
                    <th scope="row"><?php esc_html_e( 'Preloader Animation', 'hello-elementor' ); ?></th>
                    <td>
                        <?php $value = self::get_theme_option( 'preloader_animation_path' ); ?>
                        <input id="upload_image" type="text" size="36" name="theme_options[preloader_animation_path]" value="<?php echo esc_attr( $value ); ?>">
                        <input id="upload_image_button" class="button" type="button" value="Upload Image">
                        <br><?php esc_html_e( 'Enter a URL or upload an image', 'hello-elementor' ); ?>

                    </td>
                    </tr>
                </div>
            </table>

            <?php submit_button(); ?>


            <?php
        }

        /**
         * Settings page output
         *
         * @since 1.0.0
         */
        public static function create_admin_page() { ?>
            <div class="wrap">

                <h1><?php esc_html_e( 'Theme Options', 'hello-elementor' ); ?></h1>


                <form method="post" action="options.php">

                    <?php self::preloader_form_display(); ?>
                </form>

            </div><!-- .wrap -->

        <?php }

    }
}
# Class Driver - Initiates class on require
new Hello_Theme_Preloader_Settings_Options();
function Hello_get_preloader_option( $id = '' ) {
    return Hello_Theme_Preloader_Settings_Options::get_theme_option( $id );
}

我从 function.php 文件中调用它们

require get_template_directory() . '/includes/admin/admin-preloader-settings.php';

require get_template_directory() . '/includes/admin/admin-scrolling-settings.php';

每个页面都能完美运行,但是当我编辑一个选项页面时,它会删除保存在第二页中的所有值,反之亦然?

我做错了什么?

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    你需要给register_setting一个唯一的名字

    register_setting( 'preloader_settings', 'preloader_settings', array('Preloader_Settings_Options', 'sanitize' ) );
    
    register_setting( 'scrolling_settings', 'scrolling_settings', array('Scrolling_Settings_Options', 'sanitize' ) );
    

    所以现在使用preloader_settingsscrolling_settings 到你使用theme_options 的任何地方。

    链接-register_setting()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2011-06-07
      • 2013-02-02
      相关资源
      最近更新 更多