How can I get the meta description to show in the head of the page?

7.26K viewsGeneral
1

Can anyone help me here? How can I get the excerpt of a question to show as the meta description in the page head?

1

With recent changes to Add Meta Tags, it is now completely possible to make it work good with AnsPress. I’m using it, see it in action here: http://dima.stefantsov.com/qa/

add_action('wp', 'd_anspress_amt_fix');
function d_anspress_amt_fix()
{
    // Only make changes when inside AnsPress.
    if (!function_exists('is_anspress') || !is_anspress()) {
        return;
    }


    add_filter('amt_exclude_schemaorg_metadata', '__return_true');
    add_filter('amt_exclude_dublin_core_metadata', '__return_true');

    // Feed AMT with real question-post.
    if (is_question()) {
        add_filter('amt_get_queried_object', function(){ return get_post(get_question_id(), OBJECT); });
    }
}
0

This is part of the same ANSPRESS_TITLE problem we are seeing here https://anspress.io/questions/question/anspress_title/

When that finally gets fixed the correct metadata will be shown in the page. For the moment that’s not happening on mine, your and a lot of other AnsPress sites, due to the fact they are not configured the same as Rahul configures his test site.

So, until he completes the documentation and specifically covers this setup issue, we are all in the dark.

This is a show-stopper for me as I am not going to launch my site until I can find out how to fix this metadata problem. And I imagine quite a few other people will be interested to learn how, too.

1

I was about to create a same question. Let me elaborate.

To create proper meta tags, microformats – I’m using excellent Add Meta Tags plugin. It doesn’t work well with AnsPress though:

As you can see, it thinks current page link is just dima.stefantsov.com/qa/ (really bad for SEO! equally bad for sharing questions in social networks)

Then it thinks description is [anspress] shortcode, which is content of base page. Here is a piece of code AMT uses to get description:

elseif ( is_singular() ) {

$content_desc = amt_get_content_description($post);
        if ( !empty($content_desc) ) {
            $metadata_arr[] = '<meta property="og:description" content="' . esc_attr( amt_process_paged( $content_desc ) ) . '" />';
        }
function amt_get_content_description( $post, $auto=true ) {
    if ( is_singular() || amt_is_static_front_page() || amt_is_static_home() ) {    // TODO: check if this check is needed at all!

        $desc_fld_content = amt_get_post_meta_description( $post->ID );

        if ( !empty($desc_fld_content) ) {
            // If there is a custom field, use it
            $content_description = $desc_fld_content;
        } else {
            // Else, use the post's excerpt. Valid for Pages too.
            if ($auto) {
                // The generated excerpt should already be sanitized.
                $content_description = amt_get_the_excerpt( $post );
        // When the post excerpt has been set explicitly, then it has priority.
        $amt_excerpt = sanitize_text_field( amt_sanitize_description( $post->post_excerpt ) );

Long story short, it tries all the way down to “read post content” to find proper data. But data is not there, since at all times wordpress thinks current page is /qa/ base page.

I’ve skimmed through this QA forum and found a lot of problems people had with <title> previously. That has been fixed afaik, but current issue looks like it’s of same origin. Internally, Questions are custom posts in wordpress (something like that). If it would be possible to somehow present real underlying item to wordpress when displaying page, a whole lot of problems, with a title, with meta tags, urls, and I’m sure with a lot more other things would be automatically solved. Is what I’m saying even possible?

This is really important for me, I’m planning to run from my current Q&A system for exactly this reason: if I want something there, I have to write it myself. Like microformats markup. But when we speak of WordPress, there are great plugins for everything like Add Meta Tags, they do it all automatically, magic!
It would not be much use, though, if AnsPress is functioning in a non-default way and plugins can’t work with it.

Nice snippet. Did you tried yoast seo plugin? it works well with AnsPress.

I have tried Yoast SEO, and it is not lost with what’s real content and real URL.

Though issue remain, because I want to use Add Meta Tags, it generates OpenGraph meta, twitter cards meta, etc.

Not only this, but any other addons might have same issues. I can easily see “share buttons” addons fail to work for AnsPress because internally it says “real page is the Base Page where there’s only [anspress]“.

Underlying issue should be fixed, then everything else will work fine.

Hi all, I checked the following question and noticed all the aforementioned problems in the generated metadata: http://dima.stefantsov.com/q/test-add-meta-tags-plugin/

I’d like to ask if this page is a regular wordpress post object (having a custom post type maybe) or anspress uses its own implementation? Chances are that the latter is the case, because otherwise the wordpress api would return the proper permalink for the page containing the question. Even in case Anspress uses its own custom objects, it is still possible to insert their information (url, description, terms, etc) into the metadata generated by add-meta-tags, but it will require some manual filtering.

If there is a documented Anspress api, I could definitely help with this and provide some sample code, which could be added in the theme’s functions.php or in a custom plugin, which would demonstrate how to filter the metadata and enter the correct url,description, etc.

I’ll be following this question.

Kind Regards,
George