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).

Read More

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.

Related posts

2 comments

  1. How document title is generated has changed since WordPress v4.4.0. Now wp_get_document_title dictates how title is generated:

    /**
     * Displays title tag with content.
     *
     * @ignore
     * @since 4.1.0
     * @since 4.4.0 Improved title output replaced `wp_title()`.
     * @access private
     */
    function _wp_render_title_tag() {
        if ( ! current_theme_supports( 'title-tag' ) ) {
            return;
        }
    
        echo '<title>' . wp_get_document_title() . '</title>' . "n";
    }
    

    Here is the code from v5.4.2. Here are the filters you can use to manipulate title tag:

    function wp_get_document_title() {
        /**
        * Filters the document title before it is generated.
        *
        * Passing a non-empty value will short-circuit wp_get_document_title(),
        * returning that value instead.
        *
        * @since 4.4.0
        *
        * @param string $title The document title. Default empty string.
        */
        $title = apply_filters( 'pre_get_document_title', '' );
        if ( ! empty( $title ) ) {
            return $title;
        }
        // --- snipped ---
        /**
        * Filters the separator for the document title.
        *
        * @since 4.4.0
        *
        * @param string $sep Document title separator. Default '-'.
        */
        $sep = apply_filters( 'document_title_separator', '-' );
    
        /**
        * Filters the parts of the document title.
        *
        * @since 4.4.0
        *
        * @param array $title {
        *     The document title parts.
        *
        *     @type string $title   Title of the viewed page.
        *     @type string $page    Optional. Page number if paginated.
        *     @type string $tagline Optional. Site description when on home page.
        *     @type string $site    Optional. Site title when not on home page.
        * }
        */
        $title = apply_filters( 'document_title_parts', $title );
        // --- snipped ---
        return $title;
    }
    

    So here are two ways you can do it.

    First one uses pre_get_document_title filter which short-circuits the title generation and hence more performant if you are not going make changes on current title:

    function custom_document_title( $title ) {
        return 'Here is the new title';
    }
    add_filter( 'pre_get_document_title', 'custom_document_title', 10 );
    

    Second way uses document_title_separator and document_title_parts hooks for the title and the title seperator that are executed later in the function, after title is generated using functions like single_term_title or post_type_archive_title depending on the page and about to be outputted:

    // Custom function should return a string
    function custom_seperator( $sep ) {
       return '>';
    }
    add_filter( 'document_title_separator', 'custom_seperator', 10 );
    
    // Custom function should return an array
    function custom_html_title( $title ) {
       return array(
         'title' => 'Custom Title',
         'site'  => 'Custom Site'
        );
    }
    add_filter( 'document_title_parts', 'custom_html_title', 10 );
    
  2. 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 );
    

Comments are closed.