web-dev-qa-db-fra.com

Comment mettre en file d'attente le script sans coder en dur dans les fichiers de thème?

Je travaillais sur le style Disposition de galerie personnalisée et je me demandais comment puis-je mettre en file d'attente et faire en sorte que le script suivant fonctionne sans code dur dans le fichier. Ou existe-t-il un meilleur moyen de l'ajouter via le script inline wp_add_inline_script().

J'ai essayé avec wp_add_inline_script() mais cela n'a pas aidé. Peut-être que je faisais quelque chose de mal.

Comment ajouter le localiser ou mettre en file d'attente le code qui est imbriqué avec les balises <script>...</script> dans le code ci-dessous.

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
Maqk
//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);
0
Mihai Papuc