Default settings aren't used

时间:2019-04-30 作者:Ryszard Jędraszyk

我使用来自以下来源的示例#2:

https://codex.wordpress.org/Creating_Options_Pages

它使用类创建设置页面。我可以访问如下值:

$options[\'id_number\']
或者在删除插件(它是一个数组)时删除所有设置的数据库条目,如下所示:

delete_option(\'my_option_name\'); // regular site options
delete_site_option(\'my_option_name\'); // multisite options
然而,这个codex示例并没有显示如何处理默认值。我看到了多种方法,并了解到其中一些方法可能有多错误。这就是说,我想在设置页面上显示默认值,并通过插件使其可用(通过保存到数据库?)如果值尚未存储。

我更换了以下部件:

$this->options = get_option( \'my_option_name\' );
使用:

$this->defaults = array (
            \'id_number\' => 123,
            \'title\' => \'my title\',
        );

$this->options = get_option($this->\'my_option_name\', $this->defaults ); 
$this->options = wp_parse_args( $this->options, $this->defaults );
现在,如果用户保存了设置,则设置页面上的值已正确预填充,并被db中的设置覆盖,然后停用并激活插件。不幸的是,这些默认值在db中不存在,所以在引用如下值时,我不能在插件代码中使用它们:

$options[\'id_number\']
有什么好方法可以使这些默认设置生效,可能是通过register_activation_hook( __FILE__, \'myplugin_activate\' );, 在提供的Codex示例中可用?此处为完整代码:

<?php
class MySettingsPage
{
    /**
     * Holds the values to be used in the fields callbacks
     */
    private $options;

    /**
     * Start up
     */
    public function __construct()
    {
        add_action( \'admin_menu\', array( $this, \'add_plugin_page\' ) );
        add_action( \'admin_init\', array( $this, \'page_init\' ) );
    }

    /**
     * Add options page
     */
    public function add_plugin_page()
    {
        // This page will be under "Settings"
        add_options_page(
            \'Settings Admin\', 
            \'My Settings\', 
            \'manage_options\', 
            \'my-setting-admin\', 
            array( $this, \'create_admin_page\' )
        );
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        // Set class property
        $this->options = get_option( \'my_option_name\' );
        ?>
        <div class="wrap">
            <h1>My Settings</h1>
            <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( \'my_option_group\' );
                do_settings_sections( \'my-setting-admin\' );
                submit_button();
            ?>
            </form>
        </div>
        <?php
    }

    /**
     * Register and add settings
     */
    public function page_init()
    {        
        register_setting(
            \'my_option_group\', // Option group
            \'my_option_name\', // Option name
            array( $this, \'sanitize\' ) // Sanitize
        );

        add_settings_section(
            \'setting_section_id\', // ID
            \'My Custom Settings\', // Title
            array( $this, \'print_section_info\' ), // Callback
            \'my-setting-admin\' // Page
        );  

        add_settings_field(
            \'id_number\', // ID
            \'ID Number\', // Title 
            array( $this, \'id_number_callback\' ), // Callback
            \'my-setting-admin\', // Page
            \'setting_section_id\' // Section           
        );      

        add_settings_field(
            \'title\', 
            \'Title\', 
            array( $this, \'title_callback\' ), 
            \'my-setting-admin\', 
            \'setting_section_id\'
        );      
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();
        if( isset( $input[\'id_number\'] ) )
            $new_input[\'id_number\'] = absint( $input[\'id_number\'] );

        if( isset( $input[\'title\'] ) )
            $new_input[\'title\'] = sanitize_text_field( $input[\'title\'] );

        return $new_input;
    }

    /** 
     * Print the Section text
     */
    public function print_section_info()
    {
        print \'Enter your settings below:\';
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function id_number_callback()
    {
        printf(
            \'<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />\',
            isset( $this->options[\'id_number\'] ) ? esc_attr( $this->options[\'id_number\']) : \'\'
        );
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function title_callback()
    {
        printf(
            \'<input type="text" id="title" name="my_option_name[title]" value="%s" />\',
            isset( $this->options[\'title\'] ) ? esc_attr( $this->options[\'title\']) : \'\'
        );
    }
}

if( is_admin() )
    $my_settings_page = new MySettingsPage();

1 个回复
最合适的回答,由SO网友:Welcher 整理而成

您可以使用以下帮助器方法从类中检索默认值,而不是将默认值保存到数据库中:

/**
 * Get the option that is saved or the default.
 *
 * @param string $index. The option we want to get.
 */
public function get_settings( $index = false ) {
        $defaults = [ \'id_number\' => 123,\'title\' => \'my title\' ];
        $settings = get_option( \'my_option_name\', $defaults );
        $settings = wp_parse_args( $settings, $defaults );

        if ( $index && isset( $settings[ $index ] ) ) {
            return $settings[ $index ];
        }

        return $settings;
    }
希望这有帮助!

相关推荐