• Resolved Jim Reekes

    (@reekes)


    I’ve searched for a couple hours, and tried several coding ideas in PHP. I’m stuck. I need the default description created by yoast for the schema code I’m adding. I’ve ended up writing a complicated function to create a proper excerpt but I can’t match the one I get yoast creating.

    I would like to get a copy of the default yoast auto-generated meta-description. I’m not referring to the excerpts I wrote and entered into yoast. Those I can get in my PHP code (see below). I’m asking about the default excerpt yoast creates when there isn’t one manually added to the yoast panel with the post editor.

    This call returns null when there is no manually entered desc. It works only if there was one written manually.

    get_post_meta($post->ID, '_yoast_wpseo_metadesc', true)

    These WordPress functions also do not produce the default yoast excerpt: get_the_excerpt() and the_excerpt()

    Yet there *is* a nicely filtered description created and outputted as meta name=”description” and og:description.

    My question, how to I get that same default except for use in my PHP template?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,

    While we are not sure what version of Yoast you are using the current versions of Yoast do not automatically generate a meta description if none has been added.

    Google, in this instance will see no meta has been outputted and create one automatically for you. Google has gotten very good at scanning the site, interpreting what is on it, and then creating a meta description it thinks works.

    However, Facebook is not. This is why the og:description takes the first couple sentences of the post/page as the description. Facebook needs the tag in order to show the right content.

    While we are not familiar with the PHP template you are using if it will have words and content on it, you can expect Google to create a meta description for it based on what it finds.

    Thread Starter Jim Reekes

    (@reekes)

    I’m using the latest versions: WordPress 5.0.3 and Yoast 9.5

    Your plugin does create an auto-generated expert. To enable this feature, you add the %%excerpt%% variable to the Excerpt field in Search Appearance template.

    The description for this is in your documentation. It reads: “Replaced with the post/page excerpt (or auto-generated if it does not exist)”

    This auto-generated except appears on the front-end html page within the meta-description tag, as well as og:description and twitter:description.

    I’m building the schema for my pages, and I must add a description. It’s not an option to leave it out. In the case there wasn’t an except manually created, I want to use the auto-generated one created by your plugin.

    Back to my question…How can I access the Yoast auto-generated excerpt from PHP?

    • This reply was modified 5 years, 9 months ago by Jim Reekes.

    Hi Jim,

    That’s not easily possible. If I understood you correctly, the following piece of code should work though:

    $content = strip_shortcodes( $this->args->post_content );
                    $content = wp_strip_all_tags( $content );
    
                    if ( strlen( utf8_decode( $content ) ) <= 156 ) {
                        return $content;
                    }
    
                    $replacement = wp_html_excerpt( $content, 156 );
    
                    // Trim the auto-generated string to a word boundary.
                    $replacement = substr( $replacement, 0, strrpos( $replacement, ' ' ) );
    Plugin Support devnihil

    (@devnihil)

    We are going ahead and marking this issue as resolved due to inactivity. If you require any further assistance please create a new issue.

    Thread Starter Jim Reekes

    (@reekes)

    The inactivity is that I haven’t gotten an answer. The sample code above has problems. Does Yoast have an API for getting the auto-generated excerpt?

    Thread Starter Jim Reekes

    (@reekes)

    This issue was not resolved.

    I’m still looking for the code to get the auto-generated except created by Yoast.

    It seems that guy didn’t understand the question or just doesn’t understand what you’re talking about… bad boyoast!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘API for auto-generated description’ is closed to new replies.