Skip to content

Instantly share code, notes, and snippets.

@trajche
Last active December 22, 2023 10:54
Show Gist options
  • Select an option

  • Save trajche/9305892 to your computer and use it in GitHub Desktop.

Select an option

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;
}
@garysassano

Copy link
Copy Markdown

Hi and thank you for the code.

Would be possible to make a similar one for the Google Analytics by Yoast plugin?

@trajche

trajche commented Apr 29, 2015

Copy link
Copy Markdown
Author

Hi domegang & Bersekz, the code has been updated!

ghost commented May 1, 2015

Copy link
Copy Markdown

hi guys using this code deletes all seo breadcumbs on all pages. i used the other way. I found all these things in plugins/wordpress-seo/frontend/class-frontend.php and only changed them (with the help of ctr+f) to smting like that .
old code 'Yoast WordPress SEO plugin.'
new code 'done by Fred'
old code 'link of yoast'
new code 'link of ur site'

@StaggerLeee

Copy link
Copy Markdown

Doesnt work. Seems they changed something.

@proovit09, change or delete credits from source code, but dont take credits for yourself. It is not nice, despite you wont hurt Yoast developers much.

@crewstyle

Copy link
Copy Markdown

Thanks @trajche ;)
https://github.com/crewstyle/clean-wordpress/blob/master/_inc/frontend.php#L228-L266

I've added 2 targets which are presents since the new YOAST version:

'<!-- This site is optimized with the Yoast SEO plugin v'.WPSEO_VERSION.' - https://yoast.com/wordpress/plugins/seo/ -->',
'<!-- / Yoast SEO plugin. -->',

See ya :)

@chrisdietr

Copy link
Copy Markdown

thanks a lot! works like a charm with crewstyle's extension.

ghost commented Dec 26, 2015

Copy link
Copy Markdown

Not working in new Version...
this works:
if (defined('WPSEO_VERSION')){
add_action('get_header',function (){ ob_start(function ($o){
return preg_replace('/\n?<.?yoast.?>/mi','',$o); }); });
add_action('wp_head',function (){ ob_end_flush(); }, 999);
}

@aatospaja

aatospaja commented Apr 27, 2016

Copy link
Copy Markdown

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
Copy Markdown

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

jodenis commented Aug 10, 2016

Copy link
Copy Markdown

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
Copy Markdown

Or...just use another plugin :)

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

@paiyasaravanan

paiyasaravanan commented Jun 22, 2017

Copy link
Copy Markdown

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