// ===== CW: Fix blog post title tags (remove trailing separator, add site name) =====
// Titles were "Post Title -" — separator showing without site name after it.
// Root cause: Yoast title template for posts has %%sep%% %%sitename%% but sitename
// in wpseo_titles was empty. Fix: set it + remove stray trailing separator via filter.
add_action( 'init', function () {
if ( get_transient( 'cw_insights_title_fix_v1' ) ) return;
$opt = get_option( 'wpseo_titles', array() );
// Ensure site name is set
if ( empty( $opt['website_name'] ) ) {
$opt['website_name'] = 'Codewave';
}
// Ensure post title template includes site name
if ( empty( $opt['title-post'] ) || strpos( $opt['title-post'], '%%sitename%%' ) === false ) {
$opt['title-post'] = '%%title%% %%sep%% %%sitename%%';
}
update_option( 'wpseo_titles', $opt );
set_transient( 'cw_insights_title_fix_v1', 1, 0 );
} );
// Belt-and-suspenders: strip any trailing " -" or " | " left by missing variable
add_filter( 'wpseo_title', function ( $title ) {
return preg_replace( '/s*[-|]+s*$/', '', $title );
} );
// ===== END CW: Fix blog post title tags =====
// ===== CW: Temp - diagnose Yoast title settings =====
add_action( 'wp_ajax_cw_insights_title_diag', function () {
$opt = get_option( 'wpseo_titles', array() );
$blog_name = get_bloginfo( 'name' );
$transient = get_transient( 'cw_insights_title_fix_v1' );
wp_send_json_success( array(
'website_name' => $opt['website_name'] ?? '(not set)',
'title-post' => $opt['title-post'] ?? '(not set)',
'blog_name' => $blog_name,
'transient_set' => $transient ? true : false,
'separator' => $opt['separator'] ?? '(not set)',
) );
} );
// ===== END CW: Temp diag =====
Software Engineer