Customize-Contros.js-Expand api.previewer

时间:2014-12-25 作者:Duke TF

我想删除此支票->if ( value._dirty ) 来自下面的代码片段,但它位于Wordpress核心文件(customize-controls.js line 1919)中

我不能编辑它,我需要从我的主题的自定义js文件中完成它,但我不知道如何完成,因为我想它应该被扩展并正确排队。代码:

        // Initialize Previewer
    api.previewer = new api.Previewer({
        container:   \'#customize-preview\',
        form:        \'#customize-controls\',
        previewUrl:  api.settings.url.preview,
        allowedUrls: api.settings.url.allowed,
        signature:   \'WP_CUSTOMIZER_SIGNATURE\'
    }, {

        nonce: api.settings.nonce,

        query: function() {
            var dirtyCustomized = {};
            api.each( function ( value, key ) {
                if ( value._dirty ) {
                    dirtyCustomized[ key ] = value();
                }
            } );

            return {
                wp_customize: \'on\',
                theme:      api.settings.theme.stylesheet,
                customized: JSON.stringify( dirtyCustomized ),
                nonce:      this.nonce.preview
            };
        },

        save: function() {
            var self  = this,
                query = $.extend( this.query(), {
                    action: \'customize_save\',
                    nonce:  this.nonce.save
                } ),
                processing = api.state( \'processing\' ),
                submitWhenDoneProcessing,
                submit;

            body.addClass( \'saving\' );

            submit = function () {
                var request = $.post( api.settings.url.ajax, query );

                api.trigger( \'save\', request );

                request.always( function () {
                    body.removeClass( \'saving\' );
                } );

                request.done( function( response ) {
                    // Check if the user is logged out.
                    if ( \'0\' === response ) {
                        self.preview.iframe.hide();
                        self.login().done( function() {
                            self.save();
                            self.preview.iframe.show();
                        } );
                        return;
                    }

                    // Check for cheaters.
                    if ( \'-1\' === response ) {
                        self.cheatin();
                        return;
                    }

                    // Clear setting dirty states
                    api.each( function ( value ) {
                        value._dirty = false;
                    } );
                    api.trigger( \'saved\' );
                } );
            };

            if ( 0 === processing() ) {
                submit();
            } else {
                submitWhenDoneProcessing = function () {
                    if ( 0 === processing() ) {
                        api.state.unbind( \'change\', submitWhenDoneProcessing );
                        submit();
                    }
                };
                api.state.bind( \'change\', submitWhenDoneProcessing );
            }

        }
    });

1 个回复
SO网友:cybmeta

从你的评论来看,我认为你的概念是错误的。您想在第一次激活后保存主题选项。激活主题并不意味着用户将使用主题定制器,因此我认为与定制器javascript无关,因为只有当用户转到定制器屏幕时才会加载它,因为它是客户端代码。有更好的方法。

我的建议是使用after_switch_themeswitch_theme 操作挂钩:

  • after_switch_theme 激活主题后保存主题选项
  • switch_theme 如果主题已停用,则要删除主题选项(可选)
示例:

add_action("after_switch_theme", "cyb_save_theme_options_on_activation");
function cyb_save_theme_options_on_activation() {
    set_theme_mod(\'background-color\', \'#FFF\');
}
我认为最好检查正在添加/更新的选项是否具有用户自定义值;如果是这样,我认为不应该改变。例如:

add_action("after_switch_theme", "cyb_save_theme_options_on_activation");
function cyb_save_theme_options_on_activation() {
    if( get_theme_mod(\'background-color\') == \'\' ) {
        set_theme_mod(\'background-color\', \'#FFF\');
    }
}
如果您使用的是主题选项而不是主题mods:

add_action("after_switch_theme", "cyb_save_theme_options_on_activation");
function cyb_save_theme_options_on_activation() {
    //add_option adds the option if it doesn\'t exist in the database
    //Use update_option if you want to override existing option in the database or add it if doesn\'t exist
    add_option(\'background-color\', \'#FFF\');
}
我仍然认为你的做法是错误的。从您的评论中,您希望在使用时单击“保存并发布”而不是主题激活时,将所有选项保存到数据库中,即使是具有默认值的选项。要做到这一点,您可以使用customize_save_after 行动挂钩:

add_action( \'customize_save_after\', \'customize_save_after\' );
function customize_save_after( $wp_customize ) {
    //Save the theme options here
}

结束