Last active
January 31, 2022 23:11
-
-
Save vralle/9815e4b2db7ddc5cdfc6ebfebe2d2118 to your computer and use it in GitHub Desktop.
The SEO Framework - Exclude WooCommerce account pages from SEO and Sitemap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Stop each TSF meta output. | |
*/ | |
add_filter( 'the_seo_framework_sitemap_hpt_query_args', 'vralle_no_sitemap' ); | |
add_filter( 'the_seo_framework_articles_data', 'vralle_no_article_json' ); | |
add_filter( 'the_seo_framework_robots_meta_array', 'vralle_no_search_robots_meta' ); | |
add_filter( 'the_seo_framework_use_og_tags', 'vralle_no_meta' ); | |
add_filter( 'the_seo_framework_use_facebook_tags', 'vralle_no_meta' ); | |
add_filter( 'the_seo_framework_use_twitter_tags', 'vralle_no_meta' ); | |
add_filter( 'the_seo_framework_ldjson_scripts', 'vralle_no_ldjson', 10, 2 ); | |
/** | |
* Retrieve WooCommerce private pages | |
* | |
* @return array WooCommerce private page ids. | |
*/ | |
function vralle_get_private_page_ids() { | |
$exclude_ids = array( | |
get_option( 'woocommerce_myaccount_page_id' ), | |
get_option( 'woocommerce_cart_page_id' ), | |
get_option( 'woocommerce_checkout_page_id' ), | |
get_option( 'woocommerce_view_order_page_id' ), | |
); | |
return array_filter( $exclude_ids ); | |
} | |
/** | |
* Exclude private pages from sitemap | |
* | |
* @param array $args The sitemap query arguments. | |
* @return array | |
*/ | |
function vralle_no_sitemap( $args ) { | |
$exclude_ids = vralle_get_private_page_ids(); | |
$args['post__not_in'] = $exclude_ids; | |
$args['posts_per_page'] = $args['posts_per_page'] + count( $exclude_ids ); | |
return $args; | |
} | |
/** | |
* No Article schema on private pages | |
* | |
* @param array $data The Articles schema data. | |
* @return array | |
*/ | |
function vralle_no_article_json( $data ) { | |
if ( function_exists( 'is_woocommerce' ) ) { | |
if ( \is_account_page() || \is_cart() || \is_checkout() ) { | |
return array(); | |
} | |
} else { | |
if ( is_page() ) { | |
$exclude_ids = vralle_get_private_page_ids(); | |
// ToDo Shortcode exist? | |
foreach ( $exclude_ids as $id ) { | |
if ( \is_page( $id ) ) { | |
return array(); | |
} | |
} | |
} | |
} | |
return $data; | |
} | |
/** | |
* Stop search robots on private pages | |
* | |
* @param array $meta Robots meta. | |
* @return array | |
*/ | |
function vralle_no_search_robots_meta( $meta ) { | |
$no_seo = array( | |
'noindex' => 'noindex', | |
'noarchive' => 'noarchive', | |
'max_snippet' => 'max-snippet:0', | |
'max_image_preview' => 'max-image-preview:none', | |
'max_video_preview' => 'max-video-preview:0', | |
); | |
// ToDo is view_order? | |
if ( function_exists( 'is_woocommerce' ) ) { | |
if ( \is_account_page() || \is_cart() || \is_checkout() ) { | |
return wp_parse_args( $meta, $no_seo ); | |
} | |
} else { | |
if ( is_page() ) { | |
$exclude_ids = vralle_get_private_page_ids(); | |
// ToDo Shortcode exist? | |
foreach ( $exclude_ids as $id ) { | |
if ( is_page( $id ) ) { | |
return wp_parse_args( $meta, $no_seo ); | |
} | |
} | |
} | |
} | |
return $meta; | |
} | |
/** | |
* No meta-tags on private pages | |
* | |
* @param bool $use Open Graph tags on the front-end. | |
*/ | |
function vralle_no_meta( $use ) { | |
if ( is_page() ) { | |
$exclude_ids = vralle_get_private_page_ids(); | |
foreach ( $exclude_ids as $id ) { | |
if ( is_page( $id ) ) { | |
return false; | |
} | |
} | |
} | |
return $use; | |
} | |
/** | |
* No ld-json on private pages | |
* | |
* @param string $json The JSON output. Must be escaped. | |
* @param int $id The current page or term ID. | |
*/ | |
function vralle_no_ldjson( $json, $id ) { | |
$exclude_ids = $this->wc_no_seo_page_ids(); | |
if ( in_array( $id, $exclude_ids ) ) { | |
return ''; | |
} | |
return $json; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Alternative option. Stop TSF meta output using a function and add meta tag for robots. | |
*/ | |
add_filter( 'the_seo_framework_query_supports_seo', 'vralle_no_tsf_meta' ); | |
add_filter( 'the_seo_framework_sitemap_hpt_query_args', 'vralle_no_sitemap' ); | |
add_filter( 'wp_robots', 'vralle_no_robots' ); | |
/** | |
* Retrieve WooCommerce private pages | |
* | |
* @return array WooCommerce private page ids. | |
*/ | |
function vralle_get_private_page_ids() { | |
$exclude_ids = array( | |
get_option( 'woocommerce_myaccount_page_id' ), | |
get_option( 'woocommerce_cart_page_id' ), | |
get_option( 'woocommerce_checkout_page_id' ), | |
get_option( 'woocommerce_view_order_page_id' ), | |
); | |
return array_filter( $exclude_ids ); | |
} | |
/** | |
* Exclude private pages from sitemap | |
* | |
* @param array $args The sitemap query arguments. | |
* @return array | |
*/ | |
function vralle_no_sitemap( $args ) { | |
$exclude_ids = vralle_get_private_page_ids(); | |
$args['post__not_in'] = $exclude_ids; | |
$args['posts_per_page'] = $args['posts_per_page'] + count( $exclude_ids ); | |
return $args; | |
} | |
/** | |
* Stop TSF meta output on woocommerce private pages | |
* | |
* @param bool SEO support for current query. | |
* @return bool | |
*/ | |
function vralle_no_tsf_meta( $supported ) { | |
if ( is_page() ) { | |
$exclude_ids = vralle_get_private_page_ids(); | |
$tsf = the_seo_framework(); | |
$current_id = $tsf->get_the_real_ID(); | |
if ( in_array( $current_id, $exclude_ids ) ) { | |
$supported = false; | |
} | |
} | |
return $supported; | |
} | |
/** | |
* Output no-robot meta-tags on woocommerce private pages | |
* | |
* @param array $robots Associative array of robots directives. | |
* @return array Filtered robots directives. | |
*/ | |
function vralle_no_robots( $robots ) { | |
if ( is_page() ) { | |
$private_page_ids = $this->get_private_page_ids(); | |
foreach ( $private_page_ids as $id ) { | |
if ( is_page( $id ) ) { | |
$robots['noindex'] = true; | |
$robots['noarchive'] = true; | |
$robots['noimageindex'] = true; | |
$robots['max-image-preview'] = '0'; | |
$robots['max-video-preview'] = '0'; | |
break; | |
} | |
} | |
} | |
return $robots; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment