函数文档

wp_body_open()

💡 云策文档标注

概述

wp_body_open() 是 WordPress 5.2.0 引入的函数,用于触发 wp_body_open 动作钩子,在 body 标签打开后执行相关代码。它为主题开发者提供了标准化的钩子位置,以增强主题结构的兼容性和可扩展性。

关键要点

  • wp_body_open() 函数触发 wp_body_open 动作钩子,允许在 body 标签打开后添加自定义代码。
  • 自 WordPress 5.2.0 起引入,建议主题开发者在主题中使用 wp_head()、wp_body_open() 和 wp_footer() 函数来构建标准结构。
  • 为确保向后兼容性,应检查函数是否存在,例如使用 if ( function_exists( 'wp_body_open' ) ) 或自定义函数定义。
  • 插件开发者可以利用此钩子,通过条件逻辑确保代码在旧主题或新主题中都能正确执行。
  • 主题自定义钩子应考虑迁移到核心的 wp_body_open 动作,以提高兼容性和维护性。

代码示例

// 基本用法
wp_body_open();

// 向后兼容性检查
if ( function_exists( 'wp_body_open' ) ) {
    wp_body_open();
} else {
    do_action( 'wp_body_open' );
}

// 自定义函数定义(如果不存在)
if ( ! function_exists( 'wp_body_open' ) ) {
    function wp_body_open() {
        do_action( 'wp_body_open' );
    }
}

// 条件钩子添加示例
function custom_code() {
    return '<!-- 自定义代码 -->';
}
if ( did_action( 'wp_body_open' ) ) {
    add_action( 'wp_body_open', 'custom_code' );
} else {
    add_action( 'custom_theme_hook', 'custom_code' );
}

注意事项

  • 在主题开发中,确保将 wp_body_open() 放置在 body 标签之后,以遵循 WordPress 最佳实践。
  • 对于插件开发,需处理主题可能未包含 wp_body_open() 的情况,例如通过 wp_footer 钩子作为备选。
  • 避免直接调用 do_action('wp_body_open') 而不检查函数存在性,以防止在旧版本 WordPress 中出错。

📄 原文内容

Fires the wp_body_open action.

Description

See ‘wp_body_open’.

Source

function wp_body_open() {
	/**
	 * Triggered after the opening body tag.
	 *
	 * @since 5.2.0
	 */
	do_action( 'wp_body_open' );
}

Hooks

do_action( ‘wp_body_open’ )

Triggered after the opening body tag.

Changelog

Version Description
5.2.0 Introduced.

User Contributed Notes

  1. Skip to note 8 content

    I Think This is the best way for add wp_body_open(); function for backwards compatibility

    Step 1 : Checked if function is exits, if not exits then make a function called wp_body_open();

    Example:

    if ( ! function_exists( 'wp_body_open' ) ) {
        function wp_body_open() {
            do_action( 'wp_body_open' );
        }
    }

    Then put the function after the body tag.

    Example:

    <html>
      <head>
     
        ..
        ..
     
        
     
      </head>
      <body>
     
        
     
        ..
        ..
     
        
     
      </body>
    </html>

  2. Skip to note 9 content

    How can I use this to my advantage as a plugin developer? How to make sure my code is included if the WP version is 5.2+ but the theme is older and/or does not include wp_body_open?

    Here is a working solution suggested by @danieliser on this ticket answer.

    </pre>
    <p>This way if you want to include code from your plugin just after the body tag opened by a theme that is up to date with good practices, and if it’s not, your code gets included the old way with <code>wp_footer</code>.</p>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-4876' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_body_open%2F%3Freplytocom%3D4876%23feedback-editor-4876" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-3423" data-comment-id="3423" class="comment byuser comment-author-hemant-ahir odd alt thread-odd thread-alt depth-1 bad-note">
    			<article id="div-comment-3423" class="comment-body">
    
    							<a href="#comment-content-3423" class="screen-reader-text">Skip to note 10 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/hemant-ahir/" rel="external nofollow" class="url">Ahir Hemant</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/wp_body_open/#comment-3423">
    							<time datetime="2019-10-19T09:08:16+00:00">
    							6 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="e68457c560" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="3423" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_body_open%2F%23comment-3423"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="17% like this"><span class="screen-reader-text">Vote results for this note: </span>-4</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="3423" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_body_open%2F%23comment-3423"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-3423">
    				<p><strong>Custom Theme Hooks</strong></p>
    <p>Many themes use their own custom actions at the beginning of body tag. They should consider migrating to the core wp_body_open action.</p>
    <p>For backwards compatibility, when injecting custom code, theme developers can use conditional logic to hook to the right action</p>
    <pre class="wp-block-code"><code lang="php" class="language-php line-numbers">function custom_code() {
        return '<!-- some code -->';
    }
    
    if ( did_action( 'wp_body_open' ) ) {
        add_action( 'wp_body_open', 'custom_code' );
    } else {
        add_action( 'custom_theme_hook', 'custom_code' );
    }