Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace PWCC\NoIndexPPP;
if ( ! is_admin() ) {
add_action( 'pre_get_posts', __NAMESPACE__ . '\\show_public_preview' );
}
/**
* Filter the Yoast SEO and AIOSEO robots meta tag for public previews.
*
* Helper for Public Post Preview plugin.
*
* Detects if a page is a public preview using the same logic as the PPP plugin
* and filters the AIOSEO & Yoast SEO robots meta tag to noindex if it is.
*
* This runs on the `pre_get_posts` action but makes no changes to the query.
*
* @param WP_Query $query The WP_Query instance.
*/
function show_public_preview( $query ) {
if (
class_exists( 'DS_Public_Post_Preview' ) &&
$query->is_main_query() &&
$query->is_preview() &&
$query->is_singular() &&
$query->get( '_ppp' )
) {
add_filter( 'wpseo_robots', __NAMESPACE__ . '\\return_noindex' );
add_filter( 'aioseo_robots_meta', __NAMESPACE__ . '\\aioseo_noindex' );
}
}
/**
* Set the AIOSEO robots meta tag attributes to noindex.
*
* @return array Modified AIOSEO meta tag attributes.
*/
function aioseo_noindex( $attributes ) {
$attributes['noindex'] = 'noindex';
return $attributes;
}
/**
* Return the string 'noindex'.
*
* @return string The string 'noindex'.
*/
function return_noindex() {
return 'noindex';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment