Is there any way to get yoast title inside page using their variable ( i.e. %%title%%)

14,196

Solution 1

By Default Yoast takes a format as %%title%% %%page%% %%sep%% %%sitename%%, and stores in wp_postmeta table under _yoast_wpseo_title key.

To only get Title of the page/post:

function yoastVariableToTitle($post_id) {
    $yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    $title = strstr($yoast_title, '%%', true);
    if (empty($title)) {
        $title = get_the_title($post_id);
    }
    return $title;
}

There can be 2 possibility with SEO title

Case I: Admin enters %%title%% %%page%% %%sep%% %%sitename%% in SEO title field then the above code will return Post/Page default title.

Case II: Admin enters My Custom Title %%page%% %%sep%% %%sitename%% in SEO title field then the above code will return My Custom Title.


To get the full Meta Title of the page/post:

function yoastVariableToTitle($post_id) {

    $yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    $title = strstr($yoast_title, '%%', true);
    if (empty($title)) {
        $title = get_the_title($post_id);
    }
    $wpseo_titles = get_option('wpseo_titles');

    $sep_options = WPSEO_Option_Titles::get_instance()->get_separator_options();
    if (isset($wpseo_titles['separator']) && isset($sep_options[$wpseo_titles['separator']])) {
        $sep = $sep_options[$wpseo_titles['separator']];
    } else {
        $sep = '-'; //setting default separator if Admin didn't set it from backed
    }

    $site_title = get_bloginfo('name');

    $meta_title = $title . ' ' . $sep . ' ' . $site_title;

    return $meta_title;
}

Hope this helps!

Solution 2

You have very difficult decisions. I have a simpler solution:

function get_post_title( WP_Post $post ): string {
    $yoast_title = get_post_meta( $post->ID, '_yoast_wpseo_title', true );
    if ( empty( $yoast_title ) ) {
        $wpseo_titles = get_option( 'wpseo_titles', [] );
        $yoast_title  = isset( $wpseo_titles[ 'title-' . $post->post_type ] ) ? $wpseo_titles[ 'title-' . $post->post_type ] : get_the_title();
    }

    return wpseo_replace_vars( $yoast_title, $post );
}

And for description:

function get_post_description( WP_Post $post ): string {
    $yoast_post_description = get_post_meta( $post->ID, '_yoast_wpseo_metadesc', true );
    if ( empty( $yoast_post_description ) ) {
        $wpseo_titles           = get_option( 'wpseo_titles', [] );
        $yoast_post_description = isset( $wpseo_titles[ 'metadesc-' . $post->post_type ] ) ? $wpseo_titles[ 'metadesc-' . $post->post_type ] : '';
    }

    return wpseo_replace_vars( $yoast_post_description, $post );
}

Solution 3

Since Yoast 14.0, this is a lot easier now. You can get the title of the current page with this code:

YoastSEO()->meta->for_current_page()->title;

Source: https://developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/

Solution 4

The workaround I use in my plugin using functions from class-frontend.php (yoast's class). It works outside of the loop, just give it a post's id:

function convert_yoast_title ($post_id) {
    $string =  WPSEO_Meta::get_value( 'title', $post_id );
    if ($string !== '') {
        $replacer = new WPSEO_Replace_Vars();

        return $replacer->replace( $string, get_post($post_id) );
    } 
    return ''; // if not found - returns empty string
}

Solution 5

Simply You can do:

$title = wp_title( '-', false, '' );
Share:
14,196
Hasan Tareq
Author by

Hasan Tareq

I always was a person who felt like I can't do better for not having a glorious academic result. Later I discovered myself as a master of a particular subject. The academic results of the university were better than before. Above all, my interest and skills in computers were quite good, most of which I acquired through Google search. Later I went through web design and development with fairly success and my clients and company were very satisfied with my work. So I think of myself as a person who understands a job and delivers accordingly. While studying for my bachelor's degree, I worked part-time at some web development company as a front-end developer and WordPress developer. After completing my bachelor's degree, I worked as a WordPress theme and plugin developer at a company where on-page SEO, speed optimization, and so on. After completing my bachelor's degree, I joined a software company where my job was to develop WordPress themes and plugins and lead their junior web-development team. At this company I work on some web applications with CakePHP & Laravel. My last job at the company was to develop features, bug fixing and UI improvement for CakePHP applications. I have also worked on developing and maintaining WordPress web site at this company and custom admin theme design and development for some government projects.

Updated on June 29, 2022

Comments

  • Hasan Tareq
    Hasan Tareq almost 2 years

    I need to create a function so that I can use that inside any page which is outside the wordpress regular page. I mean wp_head() will not be placed there. I need it for a purpose.

    The purpose is for amp(ampproject.org) page where I can't use any css or js. That's why I need this. I need to place a function at wp title so that the yoast title be placed there.

    I need something like this:

    function yoastVariableToTitle($variable){
        return yoast_vaialble_to_show_title($variable);
    }
    
  • Dan Hastings
    Dan Hastings over 4 years
    seems like a bit of a clunky design from Yoasts developers but this works