admin管理员组

文章数量:1312662

I want to conditionally prefix the contents of the tag if the IP address matches the dev server. I cobbled something together, and it will output the title tag correctly, but the original title tag precedes it, and therefore is the content shown in the browser tab. Thoughts?

$host = $_SERVER['SERVER_ADDR'];
if ($host =='0.0.0.0') {
    remove_action( 'wp_head', '_wp_render_title_tag', 1 );
    add_action( 'wp_head', '_wp_render_title_tag_dev', 1 );

        function _wp_render_title_tag_dev() {
            if ( did_action( 'wp_head' ) || doing_action( 'wp_head' ) ) {     
                echo '<title>DEV SRV:' . wp_get_document_title() . '</title>' . "\n";
            }
        }
} 

I want to conditionally prefix the contents of the tag if the IP address matches the dev server. I cobbled something together, and it will output the title tag correctly, but the original title tag precedes it, and therefore is the content shown in the browser tab. Thoughts?

$host = $_SERVER['SERVER_ADDR'];
if ($host =='0.0.0.0') {
    remove_action( 'wp_head', '_wp_render_title_tag', 1 );
    add_action( 'wp_head', '_wp_render_title_tag_dev', 1 );

        function _wp_render_title_tag_dev() {
            if ( did_action( 'wp_head' ) || doing_action( 'wp_head' ) ) {     
                echo '<title>DEV SRV:' . wp_get_document_title() . '</title>' . "\n";
            }
        }
} 
Share Improve this question asked Dec 16, 2020 at 18:39 whakawaeherewhakawaehere 7910 bronze badges 2
  • 1 Why not use the filter? developer.wordpress/reference/hooks/wp_title – shanebp Commented Dec 16, 2020 at 19:21
  • @shanebp because when I work alone, I overthink and often overcomplicate things. Thanks. Solved. – whakawaehere Commented Dec 16, 2020 at 20:59
Add a comment  | 

1 Answer 1

Reset to default 1

Thanks to @shanebp for reminding me to simplify

add_filter('wp_title', 'dev_srv_title');
function dev_srv_title($title) {
        $host = $_SERVER['SERVER_ADDR'];
        if ($host =='0.0.0.0') {
                return 'DEV SRV: '.$title;
        }
    return $title;
}

本文标签: wp headPrefix the title tagif IP address is the dev server