已修改Backbone JS中的wp.media.view.Settings.Gallery,但无法编辑

时间:2015-07-16 作者:Lloyd Jones

我已经添加到普通的WordPress gallery弹出窗口(在WordPress编辑器中单击“添加媒体”时),以便选择我的自定义库类型(与WordPress gallery主快捷码交互/使用)。

我可以选择我的自定义图库类型(我有两种)或“本机”(这是普通的WordPress图库)。

目前,在创建库时,一切都按预期工作(如果选择了非本机库类型,则除了我的非本机库使用的几个其他选项外,gallerytype会添加到发送给编辑器的短代码中)。

然而,问题是,当想要编辑我已经创建的图库时,我用来选择图库类型(本机或我的自定义图库)“冻结”的下拉列表,即单击它会显示所有选项,但选择不同于最初选择的选项不会起任何作用,它会捕捉回原始选项。

下面是我的代码:

 <script type="text/html" id="tmpl-mygallery-gallery-type">
<label class="setting">
  <span><?php _e(\'Gallery type\'); ?></span>
  <select data-setting="gallerytype">
    <option value="native" <# if ( \'native\' == wp.media.gallery.defaults.gallerytype ) { #>selected="selected"<# } #>> WordPress Native </option>
    <option value="mygallery_slide" <# if ( \'mygallery_slide\' == wp.media.gallery.defaults.gallerytype ) { #>selected="selected"<# } #>> On-page slide </option>        
    <option value="mygallery_fade" <# if ( \'mygallery_fade\' == wp.media.gallery.defaults.gallerytype ) { #>selected="selected"<# } #>> On-page fade </option>       
  </select>
</label>
<label class="setting" style="display: none;">
    <input data-setting="slidenumbers" value="<?php echo $this->mygallery_saved_settings[\'numslides\']; ?>" type="hidden" />
</label>
</script>
 <script type="text/javascript">
jQuery(document).ready(function() {

    var mygallery_options = {
        slidenumbers: \'<?php echo $this->mygallery_saved_settings[\'numslides\']; ?>\',
        slidespeed: \'<?php echo $this->mygallery_saved_settings[\'thespeed\']; ?>\',
        slidetitles: \'<?php echo $this->mygallery_saved_settings[\'thetitles\']; ?>\',
        slidesize: \'<?php echo $this->mygallery_saved_settings[\'thesize\']; ?>\',
    };
    var the_defaults = {
        gallerytype: \'native\'
    };

    jQuery.extend(the_defaults, mygallery_options);
    _.extend(wp.media.gallery, {
        defaults: the_defaults,
        setDefaults: function(attrs) {
            var self = this;
            // Remove default attributes from the shortcode, unless DeLight
            _.each(this.defaults, function(value, key) {
                attrs[key] = self.coerce(attrs, key);
                if (value === attrs[key]) {
                    delete attrs[key];
                }
            });
            if (\'gallerytype\' in attrs) {
                jQuery.extend(attrs, mygallery_options);
            }
            return attrs;
        },
    });

    //Extend media gallery
    wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
        events: function(args) {

            var the_events = {};

            //NEED TO GET STATE (ie, \'creating gallery for first time\' rather than \'edit gallery\'....
            var is_create_gallery = true;

            //IF WE\'RE EDITING, SET IT TO FALSE
            //if(editing??) > is_create_gallery = false;

            if (is_create_gallery) {
                _.extend( the_events, { \'change select[data-setting="gallerytype"]\' : \'gallerytypechanged\' } );
            }

            return the_events;
        },
        gallerytypechanged: function( e ){
            e.preventDefault();

            var self = this;

            var gallery_type = jQuery( e.currentTarget ).val();

            if( gallery_type != \'native\' ){

                var parent_container = jQuery(self.$el);

                var gallery_size = jQuery(parent_container).find(\'select[data-setting="size"]\');
                var gallery_link = jQuery(parent_container).find(\'select[data-setting="link"]\');

                //Set vars for DeLight selection
                jQuery( gallery_size ).val(\'thumbnail\');
                jQuery( gallery_link ).val(\'none\');

                //Update vars so the editor thinks they were clicked
                self.update.apply( self, [\'size\'] );
                self.update.apply( self, [\'link\'] );
                self.update.apply( self, [\'gallerytype\'] );
                self.update.apply( self, [\'slidenumbers\'] );
                self.update.apply( self, [\'slidespeed\'] );
                self.update.apply( self, [\'slidetitles\'] );
                self.update.apply( self, [\'slidesize\'] );
            }

            return self;
        },
        template: function(view) {
            return wp.media.template(\'gallery-settings\')(view) + wp.media.template(\'mygallery-gallery-type\')(view);
        },
    });
});
</script>
如果您有任何关于如何纠正这种行为的建议,我们将不胜感激!

当我注释掉这行的自我时。使现代化应用(self,[\'gallerytype\']);\',它在编辑时工作,但在从头开始创建时,不会将自定义设置发送到编辑器中的快捷码。当我保留该行时,创建工作正常,但随后会出现下拉菜单,返回到原始值行为。

谢谢

1 个回复
SO网友:Amol Bhandari SJ

保存选项时,是否将自定义帖子类型保存到您要分配的新帖子类型?。你可以调查一下。

结束

相关推荐

JQuery函数在开发人员控制台中工作,但在其他情况下不起作用

有人能帮我利用原生wordpress jquery来运行我的函数吗?我所做的唯一工作测试是通过chrome开发控制台加载外部jquery资源,然后将我的函数添加到inspect元素控制台。工作测试:将其添加到控制台:if(!window.jQuery||confirm(\'Overwrite\\x20current\\x20version?\\x20v\'+jQuery.fn.jquery)) (function(d,s){s=d.createElement(\'script\');&#x