使用可视编辑器时,允许在图库设置页面上显示多个模板视图

时间:2015-12-23 作者:jgraup

我想把这个扩展一下answer 可以从UI切换短代码属性,并根据这些属性更改库标记。

[gallery owl="true" link="none" size="medium" ids="378,377,376,375,374,373"]
在这种情况下,如果owl 设置为true 然后Owl Carousel 将在画廊的位置渲染-确实如此。但就像任何短代码一样,你必须记住正确的属性。鉴于我还希望能够交换旋转木马iDangerous Swiper 然后是属性owl 受支持的功能集限制且难以记住。

幸运的是,我找到了一段代码,可以使用此代码将~自定义字段~添加到库中sample.

add_action(\'print_media_templates\', function(){

  // define your backbone template;
  // the "tmpl-" prefix is required,
  // and your input field should have a data-setting attribute
  // matching the shortcode name
  ?>
  <script type="text/html" id="tmpl-my-custom-gallery-setting">
    <label class="setting">
      <span><?php _e(\'My setting\'); ?></span>
      <select data-setting="my_custom_attr">
        <option value="foo"> Foo </option>
        <option value="bar"> Bar </option>
        <option value="default_val"> Default Value </option>
      </select>
    </label>
  </script>

  <script>

    jQuery(document).ready(function(){

      // add your shortcode attribute and its default value to the
      // gallery settings list; $.extend should work as well...
      _.extend(wp.media.gallery.defaults, {
        my_custom_attr: \'default_val\'
      });

      // merge default gallery settings template with yours
      wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
        template: function(view){
          return wp.media.template(\'gallery-settings\')(view)
               + wp.media.template(\'my-custom-gallery-setting\')(view);
        }
      });

    });

  </script>
  <?php

});
不幸的是,该职位已满2年,现已关闭。

这个challenge 如果我用我的设置替换模板视图,那么就不可能从其他插件或主题扩展设置集。最后一个视图设置为template: 永远是赢家。

这个question: 我如何编写代码以允许更大的灵活性,这样不仅可以显示我的设置,而且不会阻止其他插件/主题修改的能力?

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

模板似乎以脚本形式存在

<script type="text/html" id="tmpl-my-custom-gallery-setting">
要呈现上述模板,需要

wp.media.template(\'my-custom-gallery-setting\')(view)
因为我们要更换template: 逻辑上,我们所需要做的就是存储一个模板ID列表。

if (!wp.media.gallery.templates) wp.media.gallery.templates = [\'gallery-settings\'];
wp.media.gallery.templates.push(\'my-custom-gallery-setting\');
然后遍历所有可用视图

wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
    template: function (view) {
        var output = \'\';
        for (var i in wp.media.gallery.templates) {
            output += wp.media.template(wp.media.gallery.templates[i])(view);
        }
        return output;
    }
});
<小时>

RESULT

因此,现在客户端不必记住短代码属性来修改库,因为所有选项都已作为下拉菜单添加到UI中。

[gallery link="none" type="owl" shape="square" ids="7228,7227,7226,7128,7127"]
另外,支持的类型列表将通过过滤器传递,以便您可以添加或减少来自第三方来源的选择数量。

enter image description here

LOCATION A

add_action(\'print_media_templates\', function() {

    // define your backbone template;
    // the "tmpl-" prefix is required,
    // and your input field should have a data-setting attribute
    // matching the shortcode name
    $gallery_types = apply_filters(\'print_media_templates_gallery_settings_types\',
                                   array(
                                       \'swiper\'      => \' Swiper\',
                                       \'owl\'         => \' Owl Carousel\',
                                       \'revolution\'  => \' Revolution Slider\',
                                       \'default_val\' => \' Default\'));
    ?>
    <script type="text/html" id="tmpl-custom-gallery-type-setting">
        <label class="setting">
            <span><?php _e(\'Layout Type\'); ?></span>
            <select data-setting="type"><?php
                foreach($gallery_types as $key => $value) {
                    echo "<option value=\\"$key\\">$value</option>";
                }
                ?>
            </select>
        </label>
    </script>

    <script>

        jQuery(document).ready(function () {

            // add your shortcode attribute and its default value to the
            // gallery settings list; $.extend should work as well...
            _.extend(wp.media.gallery.defaults, {
                type: \'default_val\'
            });

            // join default gallery settings template with yours -- store in list
            if (!wp.media.gallery.templates) wp.media.gallery.templates = [\'gallery-settings\'];
            wp.media.gallery.templates.push(\'custom-gallery-type-setting\');

            // loop through list -- allowing for other templates/settings
            wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
                template: function (view) {
                    var output = \'\';
                    for (var i in wp.media.gallery.templates) {
                        output += wp.media.template(wp.media.gallery.templates[i])(view);
                    }
                    return output;
                }
            });

        });

    </script>
    <?php
});
wp.media.template(\'my-custom-gallery-setting\')(view)

相关推荐

Geoip shortcodes in comments

我想知道如何从geoip插件添加国家/地区短代码(https://pl.wordpress.org/plugins/geoip-detect/) 输入注释字段。[geoip\\u detect2 property=“country”]据我所知,注释字段必须是所见即所得字段(默认情况下不是文本)。还有其他方法吗?通过自定义php函数或其他方式?你好,Michal