个性化阅读
专注于IT技术分析

一键更改WordPress主题定制器中的所有设置?

点击下载

我一直在尝试为Wordpress主题定制器创建一个主题重置按钮, 该按钮会自动删除所有theme_mod设置。我尝试了多种方法来执行此操作, 但从未使其能够正常工作。如此处所示。

在使用remove_theme_mods方法多次尝试失败后, 我开始怀疑这是否是我的问题, 除了ajax和javascript出现故障并没有正确绑定按钮。

由于我将所有默认值保存到一个大数组中, 因此在安装主题后, 它会自动在主题定制器页面上填充值, 并且主题具有特定外观…我想我可以尝试使用另一种方法来代替删除主题设置我只是骑了他们, 也许有一个自定义控件?可能通过某种方式将这些默认值分配给所有设置?我真的希望有人可以帮助我解决这个问题。如果你有任何想法, 我将非常感谢。

这是如何为主题分配默认值的示例:

function newtheme_get_theme_mods() {

$defaults = array(

        'newtheme_responsive'                 => true, 'newtheme_hero_title'                 => 'Beautiful and Elegant', 'newtheme_hero_description'           => 'The best theme in the world', 'newtheme_header_logo'                => '/wp-content/themes/newtheme/img/logo.png', 'newtheme_header_background_color'    => 'rgba(35, 35, 35, 0.9)'


    return $defaults;

}

#1


创建一个Class Theme, 并将主题设置与类主题的序列化对象一起存储在数据库中。在更改主题或还原主题时, 请在Theme类构造函数中加载该序列化的对象, 然后该类将相应地使用设置。到目前为止, 这是我在项目中所做的事情。


#2


如在堆栈交换上回答

使用remove_theme_mods在定制程序中显​​示默认值的问题是

  • Customizer是一个预览, 你可以不保存就退出它,
  • 个别的theme_mods被过滤, 但不是整个theme_mod数组
  • theme_mods包括菜单和小部件。

我还想要一个重置按钮, 但是我选择创建一个”预设”控件, 并且其中一个预设是”默认”。这种方式使用选择, 因此按钮不起作用没有问题(因为绑定是用于值更改, 而按钮不更改其值)。

诀窍是使用ajax检索选定的预设, 然后循环遍历javascript中的值, 将它们分配给设置, 以便这些更改将触发刷新预览。我的代码包含过滤器, 以便子主题可以添加更多选项和预设。预设可以是可用选项的子集。

这是Preset控件的PHP(仅是普通选择, 而无设置控件):

$wp_customize->add_control( 'option_presets', array(
    'label'    => __( 'Use preset theme options', 'mytheme' ), 'description' => __( 'Theme options will be set to the preset values.', 'mytheme' ), 'section'  => 'mytheme_section', 'settings' => array(), 'type'     => 'select', 'capability' => 'edit_theme_options', 'choices'  => mytheme_option_presets_choices(), ) );

这是其余的PHP函数。

/**
 * Supply list of choices for option presets.
 */
function mytheme_option_presets_choices() {
    return apply_filters( 'mytheme_option_presets_choices', array(
        'none' => __( 'Select preset', 'mytheme' ), 'defaults' => __( 'Defaults', 'mytheme' ), 'dark' => __( 'Dark', 'mytheme' ), ) );
}

/**
 * Sanitize an option preset choice.
 */
function mytheme_sanitize_option_presets_choice( $input ) {
    $valid = mytheme_option_presets_choices();
    return array_key_exists( $input, $valid ) ? $input : 'none';
}

/**
 * Get the preset values for the chosen option preset.
 */
function mytheme_option_preset( $which ) {
    $values = array();
    if ( 'defaults' === $which ) {
        $values = mytheme_default_values();
    }
    if ( 'dark' === $which ) {
        $values = array(
            'body_textcolor' => '#f9f7f7', 'background_color' => '#444244', 'header_textcolor' => '#bf9a07', 'area_classes' => array(
                'sidebar' => 'semi-black', 'widgets' => 'box', ), );
    }
    return apply_filters( 'mytheme_option_preset', $values, $which );
}

/**
 * Add a nonce for Customizer for option presets.
 */
function mytheme_refresh_nonces( $nonces ) {
    $nonces['mytheme-customize-presets'] = wp_create_nonce( 'mytheme-customize-presets' );
    return $nonces;
}
add_filter( 'customize_refresh_nonces', 'mytheme_refresh_nonces' );

/**
 * Ajax handler for supplying option preset values.
 */
function mytheme_ajax_option_preset_values() {
    check_ajax_referer( 'mytheme-customize-presets', 'option_presets_nonce' );
    if ( ! current_user_can( 'edit_theme_options' ) ) {
        wp_die( -1 );
    }

    if ( empty( $_POST['option_preset'] ) ) {
        wp_send_json_error( 'mytheme_missing_preset_parameter' );
    }
    $preset = sanitize_text_field( wp_unslash( $_POST['option_preset'] ) );
    $values = mytheme_option_preset( $preset );
    if ( empty( $values ) ) {
        wp_send_json_error( array( 'message' => __( 'No preset found.', 'mytheme' ) ) );
    }
    else {   // Flatten the array.
        foreach ($values as $key => $avalue) {
            if ( is_array( $avalue ) ) {
                unset( $values[$key] );
                foreach ($avalue as $subkey => $subvalue) {
                    $values[$key . '[' . $subkey . ']'] = $subvalue;
                }
            }
        }
        wp_send_json_success( array( 'values' => $values ) );
    }
}
add_action( 'wp_ajax_mytheme_option_preset', 'mytheme_ajax_option_preset_values' );

然后只需要一点点Javascript即可提出ajax请求。这在’customize_controls_enqueue_scripts’操作中排队。 (我忽略了错误消息的显示。)

wp.customize.control( 'option_presets', function( control ) {
    control.element = new wp.customize.Element( control.container.find( 'select' ) );
    control.element.bind( function( preset ) {
        var request = wp.ajax.post( 'mytheme_option_preset', {
            option_presets_nonce: wp.customize.settings.nonce['mytheme-customize-presets'], wp_customize: 'on', customize_theme: wp.customize.settings.theme.stylesheet, option_preset: preset
        } );
        request.done( function( response ) {
            _.each( response.values, function( value, id ) {
                var setting = wp.customize( id );
                if ( setting ) {
                    setting.set( value );
                }
            } );
        } );
    } );
} );
赞(0)
未经允许不得转载:srcmini » 一键更改WordPress主题定制器中的所有设置?

评论 抢沙发

评论前必须登录!