最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to parse Yoast SEO Snippet Variables

programmeradmin0浏览0评论

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>

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>
Share Improve this question edited Sep 28, 2018 at 8:18 Pratik Patel 1,1111 gold badge11 silver badges23 bronze badges asked Sep 28, 2018 at 6:00 Josh SmithJosh Smith 1411 silver badge12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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;

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

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/questions/41361510/is-there-any-way-to-get-yoast-title-inside-page-using-their-variable-i-e-ti

发布评论

评论列表(0)

  1. 暂无评论