0
votes

The codes below works in changing title and meta description by query strings. There was only one page with 3 query strings. The other pages do not have query strings. With the codes in functions.php, the other pages Yoast title and metadescription does not work properly. Without the codes, Yoast title and meta description works properly. Is there a way to make the codes work while still using Yoast title and meta description?

/* Changing title and meta description by query strings */
function yoast_add_title (){
    if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'medical' ) {
        $title = 'Health';
    }
    if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'wealth' ) {
        $title = 'Financial';
    }
    if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == "retirement" ) {
        $title = 'Wealth';
    }
    return $title; 
}

function yoast_add_metadesc (){
    if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'medical' ) {
        $metadesc = 'Health';
    }
    if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'wealth' ) {
        $metadesc = 'Financial';
    }
    if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == "retirement" ) {
        $metadesc = 'Wealth';
    }
    return $metadesc; 
}

add_filter( 'wpseo_title', 'yoast_add_title', 10, 1);
add_filter( 'wpseo_metadesc', 'yoast_add_metadesc', 10, 1);
1

1 Answers

0
votes

The code below works in functions.php instead. Somehow I need to target the exact page id and use template_redirect.

function yoast_add_title (){
    if ( $_GET['q'] == 'medical' ) {
        $title = 'Health';
    }
    if ( $_GET['q'] == 'wealth' ) {
        $title = 'Financial';
    }
    if ( $_GET['q'] == 'retirement' ) {
        $title = 'Wealth';
    }
    return $title;
}

function yoast_add_metadesc (){
    if ( $_GET['q'] == 'medical' ) {
        $metadesc = 'Health';
    }
    if ( $_GET['q'] == 'wealth' ) {
        $metadesc = 'Financial';
    }
    if ( $_GET['q'] == 'retirement' ) {
        $metadesc = 'Wealth';
    }
    return $metadesc; 
}

function yoast_add_all (){
    if ( is_page(50) && ( ! empty( $_GET[ 'q' ] ) ) ) { 
        add_filter( 'wpseo_title', 'yoast_add_title', 10, 1 );
        add_filter( 'wpseo_metadesc', 'yoast_add_metadesc', 10, 1 );
    }
}

add_action( 'template_redirect', "yoast_add_all" );