【问题标题】:Add a button to reset WordPress theme options添加一个按钮来重置 WordPress 主题选项
【发布时间】:2016-02-21 05:20:10
【问题描述】:

我为 WordPress 主题创建了一个主题选项页面,我需要添加一个重置按钮来清除所有用户定义的主题选项设置。

有人告诉我这个函数可以完成这项工作,

function reset_mytheme_options() { 
    remove_theme_mods();
}
add_action( 'after_switch_theme', 'reset_mytheme_options' );

但是,不知道如何在单击按钮时运行此功能。

那么,这个函数能满足我的需要吗?如果是这样,如何在单击按钮时运行它?

【问题讨论】:

  • 您是否使用 theme_mods 或 Settings API 作为您的主题选项?

标签: php wordpress wordpress-theming


【解决方案1】:

正如您提到的,您正在使用设置 API,那么您可以通过这种方式实现。

  1. 在提交按钮后的主题选项页面中添加按钮。

    submit_button(__('Reset'), 'secondary', 'reset', false);

  2. 当你注册设置时通过验证回调

    register_setting('option_group', 'options_name', 'save_theme_option');

  3. 当通过reset按钮提交表单时,检查重置请求并返回默认设置。

例子

function save_theme_option($input) {
    if (isset($_POST['reset'])) {
        add_settings_error('settingName', 'SettingSlug', __('Your settings has been changed defualt setting.', 'text-domain'), 'updated');
        return array('a' => 1, 'b' => 2); //Default settings
    }

    return $input;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多