Is there any way to get Yoast title inside a page using the Yoast variable? ( i.e. %%title%%)
Asked Answered
C

6

6

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) a page where I can't use any CSS or JavaScript. 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);
}
Copybook answered 28/12, 2016 at 11:49 Comment(3)
Do you only want the title(name) which is set my Yoast? or the full phrase %%title%% %%page%% %%sep%% %%sitename%% ??Commend
Full phrase is good.Copybook
I have updated my answer, please check it, 2nd part will be needed by you (i.e. To get the full Meta Title of the page/post:).Commend
C
15

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!

Commend answered 28/12, 2016 at 13:22 Comment(0)
I
15

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/

Innocence answered 19/5, 2021 at 5:34 Comment(2)
A simple and better approach.Ovoviviparous
The YoastSEO class no longer exists (plugin version 20.12). The class name might have been changed.Fruitful
L
13

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 );
}
Logical answered 23/10, 2019 at 12:5 Comment(1)
I was looking for " wpseo_replace_vars( $yoast_post_description, $post )" for so much time ....Borne
C
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
}
Creight answered 28/5, 2019 at 10:36 Comment(1)
seems like a bit of a clunky design from Yoasts developers but this worksSmaragd
A
2

Simply You can do:

$title = wp_title( '-', false, '' );
Anyaanyah answered 2/10, 2018 at 4:24 Comment(0)
T
0

I ended up with Raunak Gupta's script, and I had to modify it in order to properly display the blog description in the main page title. Here's the modified variant:

function yoastVariableToTitle($post_id) {

    $yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    $title = strstr($yoast_title, '%%', true);
    if ( !is_front_page() ) {
        $title = strstr( $yoast_title, '%%', true );
    } else {
        $title = get_bloginfo( 'description' );
    }
    $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;

}
Tektite answered 2/4, 2020 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.