Useful Yoast Hacks

I use Yoast SEO on all of my wordpress setup. Here are some hacks that I use for it.

Disable Yoast JSON-LD Completely

add_filter( 'wpseo_json_ld_output', '__return_false' );

Noindex paginated pages

Since the version 7.0 Yoast SEO started paginated pages indexation by default. Meaning /page/2/ or /page/3/ etc pages will be indexed by search engines and will show up on SERP by default. Previously you could disable it from the settings but that is no longer the case.

Some like it, some don’t. Personally I don’t like it. You can disable it by using the below codes.

/* noindex tag for paginated pages */
add_filter( 'wpseo_robots', function( $string ) {
    if ( is_paged() )
	    return 'noindex, follow';
    else
	    return $string;
});

Once you added them you can check if the code is working by looking into the source code of a paginated pages. ✌


Remove datePublished

/* Remove from article graph */
add_filter( 'wpseo_schema_article', function( $data ) {
	if ( isset( $data['datePublished'] ) )
		unset( $data['datePublished'] );
	return $data;
});

/* Remove from webpage graph */
add_filter( 'wpseo_schema_webpage', function( $data ) {
	if ( isset( $data['datePublished'] ) )
		unset( $data['datePublished'] );
	return $data;
});

Remove dateModified

/* Remove from article graph */
add_filter( 'wpseo_schema_article', function( $data ) {
	if ( isset( $data['dateModified'] ) )
		unset( $data['dateModified'] );
	return $data;
});

/* Remove from webpage graph */
add_filter( 'wpseo_schema_webpage', function( $data ) {
	if ( isset( $data['dateModified'] ) )
		unset( $data['dateModified'] );
	return $data;
});

Remove both datePublished & dateModified

/* Remove from article graph */
add_filter( 'wpseo_schema_article', function( $data ) {
	if ( isset( $data['datePublished'] ) )
		unset( $data['datePublished'] );
	if ( isset( $data['dateModified'] ) )
		unset( $data['dateModified'] );
	return $data;
});

/* Remove from webpage graph */
add_filter( 'wpseo_schema_webpage', function( $data ) {
	if ( isset( $data['datePublished'] ) )
		unset( $data['datePublished'] );
	if ( isset( $data['dateModified'] ) )
		unset( $data['dateModified'] );
	return $data;
});

Show dateModified as datePublished

/* For article graph */
add_filter( 'wpseo_schema_article', function( $data ) {
	if ( isset( $data['dateModified'] ) )
		$data['datePublished'] = $data['dateModified'];
	return $data; 
});

/* For webpage graph */
add_filter( 'wpseo_schema_webpage', function( $data ) {
	if ( isset( $data['dateModified'] ) )
		$data['datePublished'] = $data['dateModified'];
	return $data; 
});

Change ArticleType

add_filter( 'wpseo_schema_article', function( $data ) {
	$data['@type'] = "BlogPosting";
	return $data;
});

Remove commentCount

add_filter( 'wpseo_schema_article', function( $data ) {
	if ( isset( $data['commentCount'] ) )
		unset( $data['commentCount'] );
    return $data;
});

Remove articleSection

add_filter( 'wpseo_schema_article_sections_taxonomy', '__return_false' );

Don’t output JSON-LD to noindex pages

/* don't output json-ld to noindex pages */
add_filter( 'wpseo_robots', function( $string ) {
	if ( strpos( $string, 'noindex' ) !== false ) {
		add_filter( 'wpseo_json_ld_output', '__return_false' );
	}
	return $string;
});

Noindex paginated pages + Disable JSON-LD for noindex pages

/* noindex tag for paginated pages */
/* don't output json-ld to noindex pages */
add_filter( 'wpseo_robots', function( $string ) {
	if ( is_paged() ) {
		$string = 'noindex, follow';
	}
	if ( strpos( $string, 'noindex' ) !== false ) {
		add_filter( 'wpseo_json_ld_output', '__return_false' );
	}
	return $string;
});