Skip to content

Instantly share code, notes, and snippets.

@sybrew
Created November 23, 2019 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sybrew/d2cec0ae8e47e0251432da6f8467557f to your computer and use it in GitHub Desktop.
Save sybrew/d2cec0ae8e47e0251432da6f8467557f to your computer and use it in GitHub Desktop.
Adds sitename, month, and year generation tags for The SEO Framework. Requires PHP 7+
<?php
// Don't include the PHP tag is PHP is already running.
add_filter( 'the_seo_framework_title_from_custom_field', 'my_transform_dynamic_tags' );
add_filter( 'the_seo_framework_custom_field_description', 'my_transform_dynamic_tags' );
function my_transform_dynamic_tags( $tag ) {
// Cache keys, for this method is inefficient.
static $keys = null;
// Add more keys as needed.
$keys = $keys ?? [
'%sitename%' => get_bloginfo( 'name' ),
'%month%' => function_exists( 'wp_date' ) ? wp_date( 'F' ) : date_i18n( 'F' ),
'%year%' => function_exists( 'wp_date' ) ? wp_date( 'Y' ) : date_i18n( 'Y' ),
];
foreach ( $keys as $key => $value ) {
if ( false !== strpos( $tag, $key ) ) {
$tag = str_replace( $key, $value, $tag );
}
}
return $tag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment