Last active
March 18, 2020 13:09
-
-
Save herbie4/319ed406c8a33ae96604a26bf5dc6da8 to your computer and use it in GitHub Desktop.
WordPress: Set metatag to no index for older posts
This file contains hidden or 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 | |
// WordPress: set no index for older posts in WordPress | |
// ----------------------------- | |
// check the post age | |
// returns true if older than... | |
function hhdev_is_old_post($days = 730) { | |
// 2 years old = 730 | |
$days = (int) $days; | |
$offset = $days*60*60*24; | |
if ( get_post_time() < date('U') - $offset ) return true; | |
return false; | |
} | |
// add Meta Tags in post when older than 2 years | |
function hhdev_add_age_meta_tags() { | |
if( is_single() && hhdev_is_old_post() ) echo '<meta name="robots" content="noindex,follow" />'; | |
} | |
add_action('wp_head', 'hhdev_add_age_meta_tags'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment