Last active
March 8, 2022 23:19
-
-
Save donohoe/a6cfcf5e920e4e3ee29f2bedc1847a53 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/* | |
In Wordpress, using the "The SEO Framework" plugin, we want to break the inheritance | |
between the Meta Title and that of OpenGraph/Twitter Title | |
(we also do this for Description but the approach is the same) | |
Relates to: OpenGraph Title | |
We do not want the OG Title to inherit from the Meta Title (as is default behavior) | |
If no OG Title is given, it should default to the article headline. | |
Else use the custom OG Title. | |
Companion JS for WP Admin front-end: | |
https://gist.github.com/donohoe/393a795b764a4adefeaa5b8960c23908 | |
Related discussion: | |
https://wordpress.org/support/topic/adjust-title-and-description-default-value-behaviors/ | |
Updated (and tested) based on comments from @sybrew | |
*/ | |
function plugin_seo_get_true_ogtitle ( $title, $id ) { | |
if (tsf()->is_singular()) { | |
$og_title = tsf()->get_post_meta_item( '_open_graph_title', $id ); | |
if (empty($og_title)) { | |
if ( $id && get_post_type($id) === 'post' ) { | |
$title = get_the_title( $id ); | |
} | |
} | |
} | |
return $title; | |
} | |
add_filter('the_seo_framework_ogtitle_output', 'plugin_seo_get_true_ogtitle', 1, 2); | |
/* | |
Relates to: Twitter Title | |
Similiar approach to OG Title, except for Twitter. | |
It no Twitter Title, it will default to the OG Title. | |
If no OG Title, then it will default to the Headline, and not the Meta Title. | |
*/ | |
function plugin_seo_get_true_twitter_title ( $title, $id ) { | |
if (tsf()->is_singular()) { | |
if ( get_post_type($id) === 'post' ) { | |
$title = tsf()->get_post_meta_item( '_twitter_title', $id ) | |
?: tsf()->get_post_meta_item( '_open_graph_title', $id ) | |
?: get_the_title( $id ); | |
} | |
} | |
return $title; | |
} | |
add_filter('the_seo_framework_twittertitle_output', 'plugin_seo_get_true_twitter_title', 1, 2); | |
The filters above do not discriminate between singular and archival displays. Sometimes, term IDs and post IDs may collide when a database transaction error occurs, this will cause unexplainable issues down the line. Moreover, get_the_ID()
takes the post ID of the first post displayed. This makes blog-as-page-pages take the metadata from the most recent post.
My tips:
- Change
$args
for$id
at line 33 and 15. - Remove line 34 and 16.
- Test for
tsf()->is_singular()
first (without parameter) before reading$id
and changing the filter value ($title
). - On line 36 you can chain using
?:
:
$title = tsf()->get_post_meta_item( '_twitter_title', $id )
?: tsf()->get_post_meta_item( '_open_graph_title', $id );
?: get_the_title( $id );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add
<?php
at line 1 to enable syntax highlighting.