You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress的<body>标签后立即添加自定义代码?

Perfect, let's build this simple WordPress plugin exactly how you need it—your custom code will sit right after the <body> tag, after the admin bar (if an admin is logged in) so it doesn't get blocked. Here's a step-by-step solution:

Step 1: Create the Plugin File

Create a new file called custom-body-code-injector.php in your WordPress wp-content/plugins/ directory, and paste this complete code:

<?php
/**
 * Plugin Name: Custom Body Code Injector
 * Description: Adds custom code right after the <body> tag, after the admin bar (if logged in as admin).
 * Version:     1.0.0
 * Author:      Frank
 * License:     GPL2
 */

// Prevent direct access to the file (security best practice)
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly via URL
}

// Hook into wp_body_open with higher priority than admin bar
add_action( 'wp_body_open', 'inject_custom_body_code', 20 );

function inject_custom_body_code() {
    // Only run on public frontend pages (skip WordPress admin)
    if ( is_admin() ) {
        return;
    }

    // Your custom code goes here
    echo '<style>.test{background:#000;color:#fff}</style>';
    echo '<h1 class="test">Testing</h1>';
}

Step 2: How It Works (Breakdown)

  • Plugin Header: The top comment block is required—WordPress uses it to recognize the plugin and display it in your Plugins dashboard.
  • Direct Access Protection: The ABSPATH check stops unauthorized direct access to the plugin file, which is a standard security measure.
  • wp_body_open Hook: This is WordPress's official, recommended hook for adding content immediately after the <body> tag (introduced in WordPress 5.2). We set the priority to 20 because the admin bar uses priority 10 for its output here—this ensures our code runs after the admin bar loads, so it won't be blocked.
  • Frontend-Only Check: The is_admin() condition makes sure your code only loads on public-facing website pages, not in the WordPress admin area.
  • Custom Code Output: Your style and HTML are echoed exactly where you specified—no more overlap with the admin menu.

Step 3: Ensure Your Theme Supports wp_body_open

Most modern themes include the wp_body_open() call in their header.php file, but if yours doesn't, you'll need to add it manually:

  1. Go to Appearance > Theme File Editor in your WordPress admin.
  2. Open header.php from the list of theme files.
  3. Find the <body> tag (it might look like <body <?php body_class(); ?>>).
  4. Add <?php wp_body_open(); ?> immediately after it, like this:
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
  1. Save your changes.

Final Result

Once you activate the plugin from your Plugins dashboard:

  • For logged-in admins: The admin bar loads first, then your custom code appears right after it (no more blocking!).
  • For regular visitors: Your code loads immediately after the <body> tag, as intended.

内容的提问来源于stack exchange,提问作者Frank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:15:42