Skip to content
Advertisement

How to replace meta title and meta description in wordpress?

Sorry if my question was basic or stupid but please help me to solve this issue. I’m trying to change <title> and <meta name="description" > tags dynamically in wordpress. so this is what I tried in function.php file.

function changeMeta_2(){
    global $wpdb;
    $cur_url = $_SERVER['REQUEST_URI']; 
    $basename = pathinfo($cur_url);
    $ebasename = $basename['filename'];
    if(is_numeric($ebasename)) {
    $url = explode('/', $basename['dirname']);
    $basename = explode('.', $url[count($url)-2]);
    $ebasename = $basename[0];
    }
    $pageName = $ebasename;



    $arraylist_subcat  = array("car","bike","boat","xxxx","yyyy","zzz","mmmm");
    $arraylist_maincat = array("aus","ind","usa","uae");


    $category_id = get_term_by('slug',$pageName, 'category');   
    $category_parentid  = get_term_by('id', $category_id->parent, 'category');   
    $parent_slug =  $category_parentid->slug;



   if ( is_page()) {        
        if ( in_array($pageName,$arraylist_maincat) ) {         
                $metaTitle = 'Browse  '.$pageName.' | Some txt title | mysite.com';
                $metaDescription = 'some of custome blablaaaaa text description  '.$pageName.' some of custome blablaaaaa text description ';                               
                echo '<title>'.$metaTitle.'</title>';
                echo '<meta name="description" content="'.$metaDescription.'"/>';                   
        }
    }
}
add_action( 'wp_head', 'changeMeta_2' );

In the above code I’m trying to change the title tag and meta description for term id which are matching with array values (in_array condition).

Everything works fine, but problem is instead of override(replace) <title> tag is appending in head. Its not changing it appending. please someone help me to solve this issue.

Advertisement

Answer

For anybody coming to this question in the future: This functionality can be accomplished using the Yoast SEO plugin.

However, if you do want to still do this yourself….

In order to modify the title, rather than the wp_head hook, you need to be using the filters that actually allow you to modify the title: wp_title

And you can / should use the wp_head in order to add the meta description (see the docs here: http://codex.wordpress.org/Meta_Tags_in_WordPress)

Also note there’s easier ways to get the page title, mentioned below…

For the title, your code would look something like so:

function changeTitle($title, $sep, $seplocation){
    global $wpdb;

    // NOTE: This is the HARD way to get the page title, and is unreliable...
    $cur_url = $_SERVER['REQUEST_URI']; 
    $basename = pathinfo($cur_url);
    $ebasename = $basename['filename'];

    if(is_numeric($ebasename)) {
        $url = explode('/', $basename['dirname']);
        $basename = explode('.', $url[count($url)-2]);
        $ebasename = $basename[0];
    }

    $pageName = $ebasename;

    // NOTE: Why not get pagename this way?
    global $post;
    $pageName = $post->post_title;

    // or if you need the slug...
    $pageName = $post->post_slug;

    $arraylist_subcat  = array("car","bike","boat","xxxx","yyyy","zzz","mmmm");
    $arraylist_maincat = array("aus","ind","usa","uae");


    $category_id = get_term_by('slug',$pageName, 'category');   
    $category_parentid  = get_term_by('id', $category_id->parent, 'category');   
    $parent_slug =  $category_parentid->slug;



   if ( is_page()) {        
        if ( in_array($pageName,$arraylist_maincat) ) {         
                $title = 'Browse  '.$pageName.' | Some txt title | mysite.com';                 
        }
    }

    return $title;
}

add_action( 'wp_title', 'changeTitle', 10, 3 );
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement