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

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

其他提示

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;

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;
} );
许可以下: CC-BY-SA归因
scroll top