Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maheshwaghmare/2ae8ea8eabb44b834db05bfb41a248a3 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/2ae8ea8eabb44b834db05bfb41a248a3 to your computer and use it in GitHub Desktop.
WordPress SEO - Register current post updated year as a custom tag or variable in to the Yoast SEO plugin. We can use the %%post_updated_year%% into the title. Read more at https://maheshwaghmare.com/doc/yoast-seo-custom-template-variable/
<?php
if( ! function_exists( 'prefix_yoast_register_post_updated_year' ) ) :
/**
* Register Current Post Publish Year
*
* @todo Change the `prefix_` with your own custom prefix.
* @since 1.0.0
* @return void
*/
function prefix_yoast_register_post_updated_year() {
wpseo_register_var_replacement( '%%post_updated_year%%', 'prefix_get_current_post_updated_year', 'advanced', 'Post Updated Year..' );
}
add_action('wpseo_register_extra_replacements', 'prefix_yoast_register_post_updated_year');
endif;
if( ! function_exists( 'prefix_get_current_post_updated_year' ) ) :
/**
* Return current post updated Year
*
* @todo Change the `prefix_` with your own custom prefix.
* @since 1.0.0
* @return string
*/
function prefix_get_current_post_updated_year() {
if( is_singular() ) {
$post_updated_date = get_post_field( 'post_modified', get_the_ID() );
return date( 'Y', strtotime( $post_updated_date ) );
}
return date("Y");
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment