如何在主题文件中不硬编码的情况下将脚本入队?

时间:2017-09-08 作者:Maqk

我一直在研究自定义库布局样式,想知道如何在文件中没有硬编码的情况下排队并使以下脚本工作。或者有没有更好的方法通过内联脚本添加它wp_add_inline_script().

我试过了wp_add_inline_script() 但这没有帮助。可能是我做错了什么。

如何添加本地化或将嵌套的代码排队<script>...</script> 以下代码中的标记。

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(
        \'justified\' => esc_html__( \'Justified\',\'textdomain\' ),
        \'grid\'      => esc_html__( \'Grid\',\'textdomain\' ),
        \'slider\'    => esc_html__( \'Slider\',\'textdomain\' ),
        \'default_val\'    => esc_html__( \'Default\',\'textdomain\' ),
    )
);
?>
<script type="text/html" id="tmpl-custom-gallery-type-setting">
    <label class="setting">
        <span><?php esc_html_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\');

    // merge default gallery settings template with yours
    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;
        }
    });
});

1 个回复
最合适的回答,由SO网友:Mihai Papuc 整理而成
//add the JS code in a variable - without <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\');

// merge default gallery settings template with yours
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;
    }
});
});";
//add the code to the script that is already enqueued with wp_enqueue_script() 
//use the same handle
wp_add_inline_script(\'handle_of_script_you_want_modified\', $script);
结束

相关推荐