Skip to content

Instantly share code, notes, and snippets.

@alexmoise
Last active January 22, 2019 03:24
Show Gist options
  • Save alexmoise/9fe4bb5014978b59f1dca0f0ef106248 to your computer and use it in GitHub Desktop.
Save alexmoise/9fe4bb5014978b59f1dca0f0ef106248 to your computer and use it in GitHub Desktop.
Few custom functions to adjust the meta title, meta description and og:url outputed by Yoast for Andy Myers Lodge
<?php
/**
* Plugin Name: AML Yoast Seo Adjustments
* Plugin URI: https://gist.github.com/alexmoise/9fe4bb5014978b59f1dca0f0ef106248
* GitHub Plugin URI: https://gist.github.com/alexmoise/9fe4bb5014978b59f1dca0f0ef106248
* Description: Few custom functions to adjust the meta title, meta description and og:url outputed by Yoast for Andy Myers Lodge
* Version: 1.0.2
* Author: Alex Moise
* Author URI: https://moise.pro
*/
// Fix canonical link, working with Yoast only
// ----------------------------------------------------------------------------
function AML_remove_canonical() {add_filter( 'wpseo_canonical', '__return_false', 10, 1 );}
add_action('wp', 'AML_remove_canonical');
function AML_design_canonical() {
$slug = strip_tags('http'.(($_SERVER['SERVER_PORT'] == 443) ? "s://" : "://").$_SERVER['SERVER_NAME'].parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
echo '<link rel="canonical" href="'.$slug.'" />'.PHP_EOL;
}
add_filter( 'wpseo_canonical', 'AML_design_canonical' );
// ....and OG:URL too, working with Yoast only
// ----------------------------------------------------------------------------
add_filter( 'wpseo_opengraph_url', 'AML_opengraph_url' );
function AML_opengraph_url( $url ) {
$ogurl = strip_tags('http'.(($_SERVER['SERVER_PORT'] == 443) ? "s://" : "://").$_SERVER['SERVER_NAME'].parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
return $ogurl;
}
// Adjust the meta title, working with Yoast only
// ----------------------------------------------------------------------------
add_filter('wpseo_title', 'AML_adjust_title');
function AML_adjust_title($title) {
// global $wp_query; // actually not needed
if ( is_page_template( 'template-photo-gallery.php' ) ) {
$titleyear = get_query_var('year'); if ($titleyear < 2000) { unset ($titleyear); }
$title = 'Photos '.$titleyear.' | Andy Myers Lodge';
}
return $title;
}
// Adjust the meta description, working with Yoast only
// ----------------------------------------------------------------------------
add_filter('wpseo_metadesc', 'AML_adjust_description');
function AML_adjust_description($description) {
// global $wp_query; // actually not needed
if ( is_page_template( 'template-photo-gallery.php' ) ) {
$titleyear = get_query_var('year');
$descpart1 = 'Check our some of the photos we\'ve gathered ';
if ($titleyear < 2000) { $descpart2 = 'over the years'; } else { $descpart2 = 'in '.$titleyear; }
$descpart3 = ' at Andy Myers Lodge!';
$description = $descpart1.$descpart2.$descpart3;
}
return $description;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment