函数文档

wp_block_theme_activate_nonce()

💡 云策文档标注

概述

wp_block_theme_activate_nonce() 函数用于为站点编辑器设置一个 JavaScript 常量,包含激活主题所需的 nonce。此函数在 WordPress 6.3.0 版本中引入。

关键要点

  • 函数 wp_block_theme_activate_nonce() 设置 JavaScript 全局常量 WP_BLOCK_THEME_ACTIVATE_NONCE,用于主题激活操作。
  • 主要应用于站点编辑器环境,确保安全地处理主题切换。
  • 依赖于 wp_get_theme_preview_path() 获取主题预览路径,并调用 wp_create_nonce() 生成 nonce。
  • 相关函数包括 wp_json_encode() 用于 JSON 编码,以及 wp_get_theme_preview_path() 和 wp_create_nonce()。

代码示例

function wp_block_theme_activate_nonce() {
    $nonce_handle = 'switch-theme_' . wp_get_theme_preview_path();
    ?>
    <script type="text/javascript">
        window.WP_BLOCK_THEME_ACTIVATE_NONCE = <?php echo wp_json_encode( wp_create_nonce( $nonce_handle ) ); ?>;
    </script>
    <?php
}

📄 原文内容

Set a JavaScript constant for theme activation.

Description

Sets the JavaScript global WP_BLOCK_THEME_ACTIVATE_NONCE containing the nonce required to activate a theme. For use within the site editor.

See also

Source

function wp_block_theme_activate_nonce() {
$nonce_handle = 'switch-theme_' . wp_get_theme_preview_path();
?>
<script type="text/javascript">
window.WP_BLOCK_THEME_ACTIVATE_NONCE = <?php echo wp_json_encode( wp_create_nonce( $nonce_handle ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?>;
</script>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/theme-previews.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/theme-previews.php#L69">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/theme-previews.php#L69-L76">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_theme_preview_path/">wp_get_theme_preview_path()</a><code>wp-includes/theme-previews.php

Filters the blog option to return the path for the previewed theme.

wp_json_encode()wp-includes/functions.php

Encodes a variable into JSON, with some confidence checks.

wp_create_nonce()wp-includes/pluggable.php

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

Show 1 moreShow less

Changelog

Version Description
6.3.0 Introduced.