函数文档

customize_themes_print_templates()

💡 云策文档标注

概述

customize_themes_print_templates() 是一个 WordPress 函数,用于在 Customizer 中输出主题浏览界面的 JavaScript 模板。该函数自 WordPress 4.2.0 版本引入。

关键要点

  • 函数功能:打印 Customizer 主题浏览 UI 的 JS 模板。
  • 引入版本:WordPress 4.2.0。
  • 相关函数:包括 wp_get_update_php_url()、_ex()、self_admin_url()、current_user_can()、_e()、__()、_x()、esc_url()、esc_attr() 等,用于处理 URL、权限、翻译和转义。

📄 原文内容

Prints JS templates for the theme-browsing UI in the Customizer.

Source

function customize_themes_print_templates() {
?>
<script type="text/html" id="tmpl-customize-themes-details-view">
<div class="theme-backdrop"></div>
<div class="theme-wrap wp-clearfix" role="document">
<div class="theme-header">
<button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'Show previous theme' );
?>

</span></button>
<button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'Show next theme' );
?>

</span></button>
<button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
_e( 'Close details dialog' );
?>

</span></button>
</div>
<div class="theme-about wp-clearfix">
<div class="theme-screenshots">
<# if ( data.screenshot && data.screenshot[0] ) { #>
<div class="screenshot"><img src="
{{ data.screenshot[0] }}?ver={{ data.version }}" alt="" /></div>
<# } else { #>
<div class="screenshot blank"></div>
<# } #>
</div>

<div class="theme-info">
<# if ( data.active ) { #>
<span class="current-label"><?php _e( 'Active Theme' ); ?></span>
<# } #>
<h2 class="theme-name">{{{ data.name }}}<span class="theme-version">
<?php
/* translators: %s: Theme version. */
printf( __( 'Version: %s' ), '
{{ data.version }}' );
?>

</span></h2>
<h3 class="theme-author">
<?php
/* translators: %s: Theme author link. */
printf( __( 'By %s' ), '
{{{ data.authorAndUri }}}' );
?>

</h3>

<# if ( data.stars && 0 != data.num_ratings ) { #>
<div class="theme-rating">
{{{ data.stars }}}
<a class="num-ratings" target="_blank" href="
{{ data.reviews_url }}">
<?php
printf(
'%1$s <span class="screen-reader-text">%2$s</span>',
/* translators: %s: Number of ratings. */
sprintf( __( '(%s ratings)' ), '
{{ data.num_ratings }}' ),
/* translators: Hidden accessibility text. */
__( '
(opens in a new tab)' )
);
?>

</a>
</div>
<# } #>

<# if ( data.hasUpdate ) { #>
<# if ( data.updateResponse.compatibleWP && data.updateResponse.compatiblePHP ) { #>
<div class="notice notice-warning notice-alt notice-large" data-slug="{{ data.id }}">
<h3 class="notice-title"><?php _e( 'Update Available' ); ?></h3>
{{{ data.update }}}
</div>
<# } else { #>
<div class="notice notice-error notice-alt notice-large" data-slug="
{{ data.id }}">
<h3 class="notice-title"><?php _e( 'Update Incompatible' ); ?></h3>
<p>
<# if ( ! data.updateResponse.compatibleWP && ! data.updateResponse.compatiblePHP ) { #>
<?php
printf(
/* translators: %s: Theme name. */
__( 'There is a new version of %s available, but it does not work with your versions of WordPress and PHP.' ),
'
{{{ data.name }}}'
);
if ( current_user_can( '
update_core' ) && current_user_can( 'update_php' ) ) {
printf(
/* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
'
' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
self_admin_url( '
update-core.php' ),
esc_url( wp_get_update_php_url() )
);
wp_update_php_annotation( '
</p><p><em>', '</em>' );
} elseif ( current_user_can( '
update_core' ) ) {
printf(
/* translators: %s: URL to WordPress Updates screen. */
'
' . __( '<a href="%s">Please update WordPress</a>.' ),
self_admin_url( '
update-core.php' )
);
} elseif ( current_user_can( '
update_php' ) ) {
printf(
/* translators: %s: URL to Update PHP page. */
'
' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
);
wp_update_php_annotation( '
</p><p><em>', '</em>' );
}
?>

<# } else if ( ! data.updateResponse.compatibleWP ) { #>
<?php
printf(
/* translators: %s: Theme name. */
__( 'There is a new version of %s available, but it does not work with your version of WordPress.' ),
'
{{{ data.name }}}'
);
if ( current_user_can( '
update_core' ) ) {
printf(
/* translators: %s: URL to WordPress Updates screen. */
'
' . __( '<a href="%s">Please update WordPress</a>.' ),
self_admin_url( '
update-core.php' )
);
}
?>

<# } else if ( ! data.updateResponse.compatiblePHP ) { #>
<?php
printf(
/* translators: %s: Theme name. */
__( 'There is a new version of %s available, but it does not work with your version of PHP.' ),
'
{{{ data.name }}}'
);
if ( current_user_can( '
update_php' ) ) {
printf(
/* translators: %s: URL to Update PHP page. */
'
' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
);
wp_update_php_annotation( '
</p><p><em>', '</em>' );
}
?>

<# } #>
</p>
</div>
<# } #>
<# } #>

<# if ( data.parent ) { #>
<p class="parent-theme">
<?php
printf(
/* translators: %s: Theme name. */
__( 'This is a child theme of %s.' ),
'<strong>{{{ data.parent }}}</strong>'
);
?>

</p>
<# } #>

<# if ( ! data.compatibleWP || ! data.compatiblePHP ) { #>
<div class="notice notice-error notice-alt notice-large"><p>
<# if ( ! data.compatibleWP && ! data.compatiblePHP ) { #>
<?php
_e( '
This theme does not work with your versions of WordPress and PHP.' );
if ( current_user_can( '
update_core' ) && current_user_can( 'update_php' ) ) {
printf(
/* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
'
' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
self_admin_url( '
update-core.php' ),
esc_url( wp_get_update_php_url() )
);
wp_update_php_annotation( '
</p><p><em>', '</em>' );
} elseif ( current_user_can( '
update_core' ) ) {
printf(
/* translators: %s: URL to WordPress Updates screen. */
'
' . __( '<a href="%s">Please update WordPress</a>.' ),
self_admin_url( '
update-core.php' )
);
} elseif ( current_user_can( '
update_php' ) ) {
printf(
/* translators: %s: URL to Update PHP page. */
'
' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
);
wp_update_php_annotation( '
</p><p><em>', '</em>' );
}
?>

<# } else if ( ! data.compatibleWP ) { #>
<?php
_e( '
This theme does not work with your version of WordPress.' );
if ( current_user_can( '
update_core' ) ) {
printf(
/* translators: %s: URL to WordPress Updates screen. */
'
' . __( '<a href="%s">Please update WordPress</a>.' ),
self_admin_url( '
update-core.php' )
);
}
?>

<# } else if ( ! data.compatiblePHP ) { #>
<?php
_e( '
This theme does not work with your version of PHP.' );
if ( current_user_can( '
update_php' ) ) {
printf(
/* translators: %s: URL to Update PHP page. */
'
' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
);
wp_update_php_annotation( '
</p><p><em>', '</em>' );
}
?>

<# } #>
</p></div>
<# } else if ( ! data.active && data.blockTheme ) { #>
<div class="notice notice-error notice-alt notice-large"><p>
<?php
_e( '
This theme doesn't support Customizer.' );
?>
<# if ( data.actions.activate ) { #>
<?php
printf(
/* translators: %s: URL to the themes page (also it activates the theme). */
' ' . __( 'However, you can still <a href="%s">activate this theme</a>, and use the Site Editor to customize it.' ),
'
{{{ data.actions.activate }}}'
);
?>
<# } #>
</p></div>
<# } #>

<p class="theme-description">{{{ data.description }}}</p>

<# if ( data.tags ) { #>
<p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{{ data.tags }}}</p>
<# } #>
</div>
</div>

<div class="theme-actions">
<# if ( data.active )
{ #>
<button type="button" class="button button-primary customize-theme"><?php _e( 'Customize' ); ?></button>
<# } else if ( 'installed' === data.type )
{ #>
<div class="theme-inactive-actions">
<# if ( data.blockTheme )
{ #>
<?php
/* translators: %s: Theme name. */
$aria_label = sprintf( _x( 'Activate %s', 'theme' ), '{{ data.name }}' );
?>
<# if ( data.compatibleWP && data.compatiblePHP && data.actions.activate ) { #>
<a href="
{{{ data.actions.activate }}}" class="button button-primary activate" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Activate' ); ?></a>
<# } #>
<# } else { #>
<# if ( data.compatibleWP && data.compatiblePHP ) { #>
<button type="button" class="button button-primary preview-theme" data-slug="
{{ data.id }}"><?php _e( 'Live Preview' ); ?></button>
<# } else { #>
<button class="button button-primary disabled"><?php _e( 'Live Preview' ); ?></button>
<# } #>
<# } #>
</div>
<?php if ( current_user_can( 'delete_themes' ) ) { ?>
<# if ( data.actions && data.actions['delete'] ) { #>
<a href="
{{{ data.actions['delete'] }}}" data-slug="{{ data.id }}" class="button button-secondary delete-theme"><?php _e( 'Delete' ); ?></a>
<# } #>
<?php } ?>
<# } else { #>
<# if ( data.compatibleWP && data.compatiblePHP ) { #>
<button type="button" class="button theme-install" data-slug="
{{ data.id }}"><?php _e( 'Install' ); ?></button>
<button type="button" class="button button-primary theme-install preview" data-slug="
{{ data.id }}"><?php _e( 'Install & Preview' ); ?></button>
<# } else { #>
<button type="button" class="button disabled"><?php _ex( 'Cannot Install', 'theme' ); ?></button>
<button type="button" class="button button-primary disabled"><?php _e( 'Install & Preview' ); ?></button>
<# } #>
<# } #>
</div>
</div>
</script>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/theme.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/theme.php#L826">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/theme.php#L826-L1100">View on GitHub</a></p></section>

<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_get_update_php_url/">wp_get_update_php_url()</a><code>wp-includes/functions.php

Gets the URL to learn more about updating the PHP version the site is running on.

wp_update_php_annotation()wp-includes/functions.php

Prints the default annotation for the web host altering the “Update PHP” page URL.

_ex()wp-includes/l10n.php

Displays translated string with gettext context.

self_admin_url()wp-includes/link-template.php

Retrieves the URL to the admin area for either the current site or the network depending on context.

current_user_can()wp-includes/capabilities.php

Returns whether the current user has the specified capability.

_e()wp-includes/l10n.php

Displays translated text.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_x()wp-includes/l10n.php

Retrieves translated string with gettext context.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

Show 6 moreShow less

Changelog

Version Description
4.2.0 Introduced.