Using WordPress ‘print_media_templates’ PHP action

The print_media_templates WordPress PHP action is used to execute custom code when the custom Backbone media templates are printed.

Usage

add_action('print_media_templates', 'your_custom_function');
function your_custom_function() {
// your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: print_media_templates

Examples

Adding a custom media template

Add a custom media template for a new media type.

add_action('print_media_templates', 'add_custom_media_template');
function add_custom_media_template() {
// Add your custom media template
?>
<script type="text/html" id="tmpl-custom-media">
<div class="custom-media-container">
<h3>{{ data.title }}</h3>
<img src="{{ data.url }}" alt="{{ data.alt }}">
</div>
</script>
<?php
}

Modifying the image media template

Customize the appearance of the image media template.

add_action('print_media_templates', 'modify_image_media_template');
function modify_image_media_template() {
// Modify the image media template
?>
<script type="text/html" id="tmpl-modified-image-media">
<div class="modified-image-media-container">
<h2>{{ data.title }}</h2>
<img src="{{ data.url }}" alt="{{ data.alt }}" class="rounded">
</div>
</script>
<?php
}

Add a custom media template for gallery thumbnail display.

add_action('print_media_templates', 'add_gallery_thumbnail_template');
function add_gallery_thumbnail_template() {
// Add a custom media template for gallery thumbnails
?>
<script type="text/html" id="tmpl-gallery-thumbnail">
<div class="gallery-thumbnail">
<img src="{{ data.thumbnail }}" alt="{{ data.alt }}">
</div>
</script>
<?php
}

Adding a custom media button

Add a custom button to the media modal.

add_action('print_media_templates', 'add_custom_media_button');

function add_custom_media_button() {
  // Add a custom media button
  ?>
  <script type="text/html" id="tmpl-custom-media-button">
    <button class="button button-primary custom-media-button">{{ data.text }}</button>
  </script>
  <?php
}

Adding a custom media modal section

Create a custom section in the media modal.

add_action('print_media_templates', 'add_custom_media_section');
function add_custom_media_section() {
// Add a custom media section
?>
<script type="text/html" id="tmpl-custom-media-section">
<div class="custom-media-section">
<h2>{{ data.title }}</h2>
<p>{{ data.description }}</p>
</div>
</script>
<?php
}