Skip to content

Instantly share code, notes, and snippets.

@trajche
Last active December 22, 2023 10:54
Show Gist options
  • Save trajche/9305892 to your computer and use it in GitHub Desktop.
Save trajche/9305892 to your computer and use it in GitHub Desktop.
Remove Yoast SEO version from header
add_action('get_header', 'start_ob');
add_action('wp_head', 'end_ob', 999);
function start_ob() {
ob_start('remove_yoast');
}
function end_ob() {
ob_end_flush();
}
function remove_yoast($output) {
if (defined('WPSEO_VERSION')) {
$targets = array(
'<!-- This site is optimized with the Yoast WordPress SEO plugin v'.WPSEO_VERSION.' - https://yoast.com/wordpress/plugins/seo/ -->',
'<!-- / Yoast WordPress SEO plugin. -->',
'<!-- This site uses the Google Analytics by Yoast plugin v'.GAWP_VERSION.' - https://yoast.com/wordpress/plugins/google-analytics/ -->',
'<!-- / Google Analytics by Yoast -->'
);
$output = str_ireplace($targets, '', $output);
$output = trim($output);
$output = preg_replace('/^[ \t]*[\r\n]+/m', '', $output);
}
return $output;
}
@aatospaja
Copy link

aatospaja commented Apr 27, 2016

Hi all... This is how I got rid of the two html comments. Code in my functions.php: (latest WP and Yoast SEO)


if (defined('WPSEO_VERSION')){

    $instance = WPSEO_Frontend::get_instance();

    remove_action( 'wpseo_head', array( $instance, 'debug_marker' ), 2 );

    remove_action( 'wp_head', array( $instance, 'head' ), 1 );
    add_action( 'wp_head', 'custom_yoast_head', 1 );

    function custom_yoast_head() {

        global $wp_query;

        $old_wp_query = null;

        if ( ! $wp_query->is_main_query() ) {
            $old_wp_query = $wp_query;
            wp_reset_query();
        }

        do_action( 'wpseo_head' );

        if ( ! empty( $old_wp_query ) ) {
            $GLOBALS['wp_query'] = $old_wp_query;
            unset( $old_wp_query );
        }

        return;

    }

}

Original head() function in class-frontend.php of the plugin is this:

public function head() {
    global $wp_query;

    $old_wp_query = null;

    if ( ! $wp_query->is_main_query() ) {
        $old_wp_query = $wp_query;
        wp_reset_query();
    }

    /**
     * Action: 'wpseo_head' - Allow other plugins to output inside the Yoast SEO section of the head section.
     */
    do_action( 'wpseo_head' );

    echo '<!-- / ', $this->head_product_name(), ". -->\n\n";

    if ( ! empty( $old_wp_query ) ) {
        $GLOBALS['wp_query'] = $old_wp_query;
        unset( $old_wp_query );
    }

    return;
}

@jarodthornton
Copy link

This is very helpful! Thank you. It works perfectly in a theme's function.php - however, is there any way to add this to a plugin? I've tried without luck. I appreciate any insight.

@jodenis
Copy link

jodenis commented Aug 10, 2016

Fusse's example works Great. Thanks Fusse!

But keep in mind NO MATTER which method you use it will BREAK forced title rewrites! (At least in the version I am using, 3.4.2)
They use the "debug_marker" (The comments) to place a the forced title rewrite. If you are not using "force title rewrite" then you should be fine.... For now.
// Find all titles, strip them out and add the new one in within the debug marker, so it's easily identified whether a site uses force rewrite. $content = preg_replace( '/<title.*?\/title>/i', '', $content ); $content = str_replace( $this->debug_marker( false ), $this->debug_marker( false ) . "\n" . '<title>' . $title . '</title>', $content );

As you can see they use the opening comment tag as a point to insert the title tag.

@AlchemyUnited
Copy link

Or...just use another plugin :)

We’ve Migrated from Yoast SEO - https://roots.io/weve-migrated-from-yoast-seo/

@paiyasaravanan
Copy link

paiyasaravanan commented Jun 22, 2017

Hello,
How to remove this line from inspect element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment