1

I see Yoast stores the snippet variables in the database. I'd like to get their values and ship them via the WP REST API. I'd also like to keep the Admin functionality for Administrators and the default settings for scalability.

I'm shipping the values off to a different application, but I can't ship the placeholders.

They can obviously get parsed because that's how they display in the frontend. I just don't see how to do it.

In the database you can see:

_yoast_wpseo_title  %%title%% %%page%% %%sep%% %%sitename%%

On the front end you can see:

<title>About - Cool Site Name</title>

3 Answers 3

2

Ok here is how I parsed the snippets in case anyone else needs to know

$id = get_the_ID();

$post         = get_post( $id, ARRAY_A );
$yoast_title = get_post_meta( $id, '_yoast_wpseo_title', true );
$yoast_desc = get_post_meta( $id, '_yoast_wpseo_metadesc', true );

$metatitle_val = wpseo_replace_vars($yoast_title, $post );
$metatitle_val = apply_filters( 'wpseo_title', $metatitle_val );

$metadesc_val = wpseo_replace_vars($yoast_desc, $post );
$metadesc_val = apply_filters( 'wpseo_metadesc', $metadesc_val );

echo $metatitle_val;
echo "<br>";
echo $metadesc_val;
2
  • Could you edit your answer and explain what this code does, where this code goes, and how it solves the original question?
    – Howdy_McGee
    Commented Mar 22, 2019 at 15:35
  • 1
    Despite lack of comment I got it to work flawlessly. The only thing is I've found the use of filters unnecessary. Thanks a lot!
    – Daniel
    Commented Apr 2, 2019 at 13:21
0

Yoast's SEO plugin has a nice API. You can probably get this with a filter like

add_filter( 'wpseo_options', function ( $arr ) {
    return $arr;
} );
1
  • I am not able to hook onto that filter from within the WP REST API callback function, unfortunately.
    – Josh Smith
    Commented Sep 28, 2018 at 15:47
0

Basically this is the answer I was looking for from this question:

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;
} 

https://stackoverflow.com/questions/41361510/is-there-any-way-to-get-yoast-title-inside-page-using-their-variable-i-e-ti

2
  • There is now a cleaner way to get the separator, via the WPSEO_Utils::get_title_separator() method. You can replace the entire separator section in the example above with: php $sep = WPSEO_Utils::get_title_separator();
    – Jesse
    Commented May 22, 2020 at 13:15
  • If your goal is to get the fully processed meta title, there is a new method as of Yoast 14.0: $meta_title = YoastSEO()->meta->for_current_page()->title;
    – Jesse
    Commented May 22, 2020 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.