将自定义域添加到wp本地库设置

时间:2015-03-31 作者:rownn

我已经搜索了解决方案,发现了许多未解决或过时的主题。

Custom wordpress gallery option| Custom field for default gallery

但是,我想添加一些自定义字段(复选框、循环按钮等)以向gallery快捷方式添加属性。有人有飞贼吗?

编辑:我终于找到了https://wordpress.org/support/topic/how-to-add-fields-to-gallery-settings 它正在做我想要它做的一切。:)罗恩

编辑:根据上面的链接,我写了以下几行。

add_action(\'print_media_templates\', function(){
?>
<script type="text/html" id="tmpl-custom-gallery-setting">
    <h3 style="z-index: -1;">___________________________________________________________________________________________</h3>
    <h3>Custom Settings</h3>

    <label class="setting">
        <span><?php _e(\'Text\'); ?></span>
        <input type="text" value="" data-setting="ds_text" style="float:left;">
    </label>

    <label class="setting">
        <span><?php _e(\'Textarea\'); ?></span>
        <textarea value="" data-setting="ds_textarea" style="float:left;"></textarea>
    </label>

    <label class="setting">
        <span><?php _e(\'Number\'); ?></span>
        <input type="number" value="" data-setting="ds_number" style="float:left;" min="1" max="9">
    </label>

    <label class="setting">
      <span><?php _e(\'Select\'); ?></span>
      <select data-setting="ds_select">
        <option value="option1"> \'Option-1\' </option>
        <option value="option2"> \'Option-2\' </option>
      </select>
    </label>

    <label class="setting">
        <span><?php _e(\'Bool\'); ?></span>
        <input type="checkbox" data-setting="ds_bool">
    </label>

</script>

<script>

    jQuery(document).ready(function()
    {
        _.extend(wp.media.gallery.defaults, {
        ds_text: \'no text\',
        ds_textarea: \'no more text\',
        ds_number: "3",
        ds_select: \'option1\',
        ds_bool: false,
        ds_text1: \'dummdideldei\'
        });

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

    });

</script>
<?php

});
ui用户界面shortcode短代码

除此之外,一切都很好:数字字段不是由shortcode填充的。我认为这是因为HTML输入标记类型“number”只接受整数作为“value”。要将ds\\u number的字符串更改为int,我必须向代码中添加什么?

欢迎光临

1 个回复
SO网友:jmarceli

感谢您的代码。我已经进一步研究了这个问题(这不是一个整数格式问题)。我为数字字段想出的唯一解决方案是修补更多的WP-JS。以下是支持任何输入类型的修改后的完整代码:

add_action(\'print_media_templates\', function(){
?>
<script type="text/html" id="tmpl-custom-gallery-setting">
    <h3>Custom Settings</h3>

    <label class="setting">
        <span><?php _e(\'Text\'); ?></span>
        <input type="text" value="" data-setting="ds_text" style="float:left;">
    </label>

    <label class="setting">
        <span><?php _e(\'Textarea\'); ?></span>
        <textarea value="" data-setting="ds_textarea" style="float:left;"></textarea>
    </label>

    <label class="setting">
        <span><?php _e(\'Number\'); ?></span>
        <input type="number" value="" data-setting="ds_number" style="float:left;" min="1" max="9">
    </label>

    <label class="setting">
      <span><?php _e(\'Select\'); ?></span>
      <select data-setting="ds_select">
        <option value="option1"> \'Option-1\' </option>
        <option value="option2"> \'Option-2\' </option>
      </select>
    </label>

    <label class="setting">
        <span><?php _e(\'Bool\'); ?></span>
        <input type="checkbox" data-setting="ds_bool">
    </label>

</script>

<script>

    jQuery(document).ready(function()
    {
        _.extend(wp.media.gallery.defaults, {
        ds_text: \'no text\',
        ds_textarea: \'no more text\',
        ds_number: "3",
        ds_select: \'option1\',
        ds_bool: false,
        ds_text1: \'dummdideldei\'
        });

        wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
        template: function(view){
          return wp.media.template(\'gallery-settings\')(view)
               + wp.media.template(\'custom-gallery-setting\')(view);
        },
        // this is function copies from WP core /wp-includes/js/media-views.js?ver=4.6.1
        update: function( key ) {
          var value = this.model.get( key ),
            $setting = this.$(\'[data-setting="\' + key + \'"]\'),
            $buttons, $value;

          // Bail if we didn\'t find a matching setting.
          if ( ! $setting.length ) {
            return;
          }

          // Attempt to determine how the setting is rendered and update
          // the selected value.

          // Handle dropdowns.
          if ( $setting.is(\'select\') ) {
            $value = $setting.find(\'[value="\' + value + \'"]\');

            if ( $value.length ) {
              $setting.find(\'option\').prop( \'selected\', false );
              $value.prop( \'selected\', true );
            } else {
              // If we can\'t find the desired value, record what *is* selected.
              this.model.set( key, $setting.find(\':selected\').val() );
            }

          // Handle button groups.
          } else if ( $setting.hasClass(\'button-group\') ) {
            $buttons = $setting.find(\'button\').removeClass(\'active\');
            $buttons.filter( \'[value="\' + value + \'"]\' ).addClass(\'active\');

          // Handle text inputs and textareas.
          } else if ( $setting.is(\'input[type="text"], textarea\') ) {
            if ( ! $setting.is(\':focus\') ) {
              $setting.val( value );
            }
          // Handle checkboxes.
          } else if ( $setting.is(\'input[type="checkbox"]\') ) {
            $setting.prop( \'checked\', !! value && \'false\' !== value );
          }
          // HERE the only modification I made
          else {
            $setting.val( value ); // treat any other input type same as text inputs
          }
          // end of that modification
        },
        });
    });

</script>
<?php

结束

相关推荐

如何增加nggallery carousel模板的缩略图大小

我想增加nggallery carousel模板的缩略图大小。检查此链接http://arkamediaworks.com/rel_test/uncatagorize/hello-2/?pid=6是否有更好的代码来执行此操作。以下是代码:<!-- Thumbnail list --> <?php foreach ( $images as $image ) : ?> <?php if ( $image->hidden ) continue; ?>&#