函数文档

print_embed_sharing_dialog()

💡 云策文档标注

概述

print_embed_sharing_dialog() 函数用于输出嵌入分享对话框的必要标记。该函数在非 404 页面时生成包含 WordPress 和 HTML 分享选项的对话框界面。

关键要点

  • 函数在 is_404() 返回 true 时直接返回,不输出任何内容
  • 使用 get_the_ID() 和 wp_rand() 生成唯一后缀,确保 ID 的唯一性
  • 输出包含两个标签页(WordPress 和 HTML)的对话框结构,每个标签页有对应的输入框和描述区域
  • 利用 esc_attr_e()、esc_html_e() 等函数进行安全转义,防止 XSS 攻击
  • 从 WordPress 4.4.0 版本开始引入

代码示例

function print_embed_sharing_dialog() {
    if ( is_404() ) {
        return;
    }

    $unique_suffix            = get_the_ID() . '-' . wp_rand();
    $share_tab_wordpress_id   = 'wp-embed-share-tab-wordpress-' . $unique_suffix;
    $share_tab_html_id        = 'wp-embed-share-tab-html-' . $unique_suffix;
    $description_wordpress_id = 'wp-embed-share-description-wordpress-' . $unique_suffix;
    $description_html_id      = 'wp-embed-share-description-html-' . $unique_suffix;
    ?>
    <div class="wp-embed-share-dialog" role="dialog" aria-label="">
        <div class="wp-embed-share-dialog-content">
            <div class="wp-embed-share-dialog-text">
                <ul class="wp-embed-share-tabs" role="tablist">
                    <li class="wp-embed-share-tab-button wp-embed-share-tab-button-wordpress" role="tab" aria-controls="<?php echo esc_attr( $share_tab_wordpress_id ); ?>" aria-selected="true" tabindex="0">
                        <?php _e( 'WordPress Embed', 'default' ); ?>
                    </li>
                    <li class="wp-embed-share-tab-button wp-embed-share-tab-button-html" role="tab" aria-controls="<?php echo esc_attr( $share_tab_html_id ); ?>" aria-selected="false" tabindex="-1">
                        <?php _e( 'HTML Embed', 'default' ); ?>
                    </li>
                </ul>
                <div id="<?php echo esc_attr( $share_tab_wordpress_id ); ?>" class="wp-embed-share-tab" role="tabpanel" aria-hidden="false">
                    <input type="text" id="wp-embed-share-input-wordpress-<?php echo esc_attr( $unique_suffix ); ?>" class="wp-embed-share-input" aria-label="" aria-describedby="<?php echo esc_attr( $description_wordpress_id ); ?>" tabindex="0" readonly/>
                    <p id="<?php echo esc_attr( $description_wordpress_id ); ?>" class="wp-embed-share-description">
                        <?php esc_attr_e( 'Copy and paste this URL into your WordPress site to embed', 'default' ); ?>
                    </p>
                </div>
                <div id="<?php echo esc_attr( $share_tab_html_id ); ?>" class="wp-embed-share-tab" role="tabpanel" aria-hidden="true">
                    <textarea id="wp-embed-share-input-html-<?php echo esc_attr( $unique_suffix ); ?>" class="wp-embed-share-input" aria-describedby="<?php echo esc_attr( $description_html_id ); ?>" tabindex="0" readonly><?php echo esc_textarea( get_post_embed_html( null, null, null ) ); ?></textarea>
                    <p id="<?php echo esc_attr( $description_html_id ); ?>" class="wp-embed-share-description">
                        <?php esc_html_e( 'Copy and paste this HTML into your site to embed', 'default' ); ?>
                    </p>
                </div>
            </div>
            <div class="wp-embed-share-dialog-footer">
                <button type="button" class="wp-embed-share-dialog-close" aria-label="">
                    <span class="dashicons dashicons-no"></span>
                </button>
            </div>
        </div>
    </div>
    <?php
}

注意事项

  • 该函数依赖于 WordPress 核心函数如 get_the_ID() 和 wp_rand(),确保在正确的上下文中调用
  • 输出内容包含 ARIA 属性,以增强可访问性
  • 使用 esc_attr_e() 和 esc_html_e() 进行国际化翻译和安全输出
  • 在 404 页面时函数会提前返回,避免不必要的输出

📄 原文内容

Prints the necessary markup for the embed sharing dialog.

Source

function print_embed_sharing_dialog() {
if ( is_404() ) {
return;
}

$unique_suffix = get_the_ID() . '-' . wp_rand();
$share_tab_wordpress_id = 'wp-embed-share-tab-wordpress-' . $unique_suffix;
$share_tab_html_id = 'wp-embed-share-tab-html-' . $unique_suffix;
$description_wordpress_id = 'wp-embed-share-description-wordpress-' . $unique_suffix;
$description_html_id = 'wp-embed-share-description-html-' . $unique_suffix;
?>
<div class="wp-embed-share-dialog hidden" role="dialog" aria-label="<?php esc_attr_e( 'Sharing options' ); ?>">
<div class="wp-embed-share-dialog-content">
<div class="wp-embed-share-dialog-text">
<ul class="wp-embed-share-tabs" role="tablist">
<li class="wp-embed-share-tab-button wp-embed-share-tab-button-wordpress" role="presentation">
<button type="button" role="tab" aria-controls="<?php echo $share_tab_wordpress_id; ?>" aria-selected="true" tabindex="0"></button>
</li>
<li class="wp-embed-share-tab-button wp-embed-share-tab-button-html" role="presentation">
<button type="button" role="tab" aria-controls="<?php echo $share_tab_html_id; ?>" aria-selected="false" tabindex="-1"></button>
</li>
</ul>
<div id="<?php echo $share_tab_wordpress_id; ?>" class="wp-embed-share-tab" role="tabpanel" aria-hidden="false">
<input type="text" value="<?php the_permalink(); ?>" class="wp-embed-share-input" aria-label="<?php esc_attr_e( 'URL' ); ?>" aria-describedby="<?php echo $description_wordpress_id; ?>" tabindex="0" readonly/>

<p class="wp-embed-share-description" id="<?php echo $description_wordpress_id; ?>">

</p>
</div>
<div id="<?php echo $share_tab_html_id; ?>" class="wp-embed-share-tab" role="tabpanel" aria-hidden="true">
<textarea class="wp-embed-share-input" aria-label="<?php esc_attr_e( 'HTML' ); ?>" aria-describedby="<?php echo $description_html_id; ?>" tabindex="0" readonly></textarea>

<p class="wp-embed-share-description" id="<?php echo $description_html_id; ?>">

</p>
</div>
</div>

<button type="button" class="wp-embed-share-dialog-close" aria-label="<?php esc_attr_e( 'Close sharing dialog' ); ?>">
<span class="dashicons dashicons-no"></span>
</button>
</div>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/embed.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/embed.php#L1181">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/embed.php#L1181-L1225">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/get_post_embed_html/">get_post_embed_html()</a><code>wp-includes/embed.php

Retrieves the embed code for a specific post.

esc_attr_e()wp-includes/l10n.php

Displays translated text that has been escaped for safe use in an attribute.

esc_html_e()wp-includes/l10n.php

Displays translated text that has been escaped for safe use in HTML output.

esc_textarea()wp-includes/formatting.php

Escaping for textarea values.

wp_rand()wp-includes/pluggable.php

Generates a random non-negative number.

is_404()wp-includes/query.php

Determines whether the query has resulted in a 404 (returns no results).

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

Displays the permalink for the current post.

get_the_ID()wp-includes/post-template.php

Retrieves the ID of the current item in the WordPress Loop.

_e()wp-includes/l10n.php

Displays translated text.

Show 4 moreShow less

Changelog

Version Description
4.4.0 Introduced.