Replies: 0
Question summary:
In my theme’s functions.php, can I hook in a filter function to change the contents of a <meta> tag in the document’s <head> section? If yes, what would that function and hook look like?
Full question:
I migrated a WordPress website from HTTP to HTTPS (SSL) and ‘lost’ all Facebook likes for its pages. Even though Google+ and Twitter follow the 301 redirect and transfer the social signals to the new URLs, Facebook doesn’t. Facebook are aware of that issue and recommend to (1) set the canonical <meta> tag to the old URL without HTTPS and to (2) set the og:url Open Graph <meta> tag to the old URL without HTTPS.
The problem is that I don’t want to set the canonical and Open Graph tags for new posts with the old URLs (which have 301 redirects to the new URLs). So I am trying to come up with a fix – create a filter function that modifies the content of these tags and hooks it in WordPress for all posts before Date X.
So far, I have only figured out how to change the data-href attribute of the social buttons that a plugin adds to the content and excerpt:
function replace_text($text) {
if (strtotime(get_the_date("Y-m-d"))<strtotime("2017-01-16")) {
$text = str_replace('data-href="https://simpatichno', 'data-href="http://simpatichno', $text);
}
return $text;
}
add_filter('the_content', 'replace_text');
add_filter('the_excerpt', 'replace_text');
I am a little lost trying to see how this can be applied to (hooked in) the header? Can someone please help?