.
*/
class AWBSEO_Meta_Output {
public static function init() {
// Remove default WordPress title tag; we control it.
add_filter( 'pre_get_document_title', [ __CLASS__, 'document_title' ], 20 );
add_action( 'wp_head', [ __CLASS__, 'output_meta' ], 1 );
// Remove Yoast / AIOSEO / RankMath if present (avoid duplication).
add_action( 'template_redirect', [ __CLASS__, 'disable_competitors' ], 0 );
}
public static function disable_competitors() {
// Yoast SEO.
if ( class_exists( 'WPSEO_Frontend' ) ) {
$yoast = WPSEO_Frontend::get_instance();
remove_action( 'wp_head', [ $yoast, 'head' ], 1 );
}
// Rank Math.
if ( class_exists( 'RankMath\Frontend\Head' ) && method_exists( 'RankMath\Frontend\Head', 'get' ) ) {
$rm_head = \RankMath\Frontend\Head::get();
if ( $rm_head ) {
remove_action( 'wp_head', [ $rm_head, 'init' ], 20 );
}
}
// All in One SEO (AIOSEO).
if ( function_exists( 'aioseo' ) ) {
$aioseo = aioseo();
if ( isset( $aioseo->meta ) ) {
remove_action( 'wp_head', [ $aioseo->meta, 'init' ] );
}
}
// SEOPress.
if ( defined( 'SEOPRESS_VERSION' ) ) {
remove_action( 'wp_head', 'seopress_titles_meta_desc', 1 );
remove_action( 'wp_head', 'seopress_social_meta_tags', 1 );
}
}
public static function document_title( $title ) {
if ( is_singular() ) {
return AWBSEO_Meta_Manager::get_title( get_the_ID() );
}
$settings = get_option( 'awbseo_settings', AWBSEO_Core::default_settings() );
if ( is_home() || is_front_page() ) {
return $settings['homepage_title'] ?: get_bloginfo( 'name' ) . ' ' . $settings['separator'] . ' ' . get_bloginfo( 'description' );
}
return $title;
}
public static function output_meta() {
if ( is_singular() ) {
$post_id = get_the_ID();
self::output_singular( $post_id );
} elseif ( is_home() || is_front_page() ) {
self::output_homepage();
} else {
self::output_archive();
}
self::output_global();
}
private static function output_singular( $post_id ) {
$desc = AWBSEO_Meta_Manager::get_description( $post_id );
$robots = AWBSEO_Meta_Manager::get_robots( $post_id );
$canonical = AWBSEO_Meta_Manager::get( $post_id, 'canonical' ) ?: get_permalink( $post_id );
// Basic.
if ( $desc ) {
echo '' . "\n";
}
if ( $robots ) {
echo '' . "\n";
}
echo '' . "\n";
// Open Graph.
$og_title = AWBSEO_Meta_Manager::get( $post_id, 'og_title' ) ?: AWBSEO_Meta_Manager::get_title( $post_id );
$og_desc = AWBSEO_Meta_Manager::get( $post_id, 'og_description' ) ?: $desc;
$og_image = AWBSEO_Meta_Manager::get( $post_id, 'og_image' ) ?: get_the_post_thumbnail_url( $post_id, 'large' );
$og_type = get_post_type( $post_id ) === 'post' ? 'article' : 'website';
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
if ( $og_desc ) echo '' . "\n";
if ( $og_image ) echo '' . "\n";
if ( $og_type === 'article' ) {
echo '' . "\n";
echo '' . "\n";
}
// Twitter Card.
$settings = get_option( 'awbseo_settings', AWBSEO_Core::default_settings() );
$twitter_card = $settings['twitter_card'] ?? 'summary_large_image';
$tw_title = AWBSEO_Meta_Manager::get( $post_id, 'twitter_title' ) ?: $og_title;
$tw_desc = AWBSEO_Meta_Manager::get( $post_id, 'twitter_desc' ) ?: $og_desc;
$tw_image = AWBSEO_Meta_Manager::get( $post_id, 'twitter_image' ) ?: $og_image;
echo '' . "\n";
echo '' . "\n";
if ( $tw_desc ) echo '' . "\n";
if ( $tw_image ) echo '' . "\n";
if ( ! empty( $settings['twitter_site'] ) ) {
echo '' . "\n";
}
}
private static function output_homepage() {
$settings = get_option( 'awbseo_settings', AWBSEO_Core::default_settings() );
$desc = $settings['homepage_desc'] ?: get_bloginfo( 'description' );
if ( $desc ) {
echo '' . "\n";
}
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
if ( $desc ) echo '' . "\n";
$og_image = $settings['og_default_image'] ?? '';
if ( $og_image ) echo '' . "\n";
}
private static function output_archive() {
echo '' . "\n";
// Paginated archives: noindex after page 2 is a common SEO practice.
$page = get_query_var( 'paged' );
if ( $page > 1 ) {
echo '' . "\n";
}
}
private static function output_global() {
// Sitemap link.
echo '' . "\n";
}
}