The-seo-framework: Twitter Card Missing

Created on 11 Aug 2017  路  14Comments  路  Source: sybrew/the-seo-framework

Output Twitter meta tags?
Output various tags targetted at Twitter.

I have checked this option - but I don't see any Twitter Meta Cards being added in the source code.

Also I am not seeing an image for the post, when sharing via twitter.

What can be wrong here?

thanks,
Andrew

All 14 comments

Posting the link in question would be beneficial, Andrew 馃槈

These two functions determine whether the Twitter Card output is available.

Simplified requirements:

  1. At least a social description must be available.
  2. At least a social title must be available.
  3. An image for summary_large_image instead of summary.

How-to spec the requirements:

  1. This can't be done automatically when you're using a page builder. Fill in a manual description.
  2. This can never fail... unless you use improper filters.
  3. An image is tried to be taken from many places. However, you can set up a social fallback image to guarantee output.

In short:

  1. Fill in a manual description 馃槃

If I fill in, manual description it works!

Is there a function I could use to automatically do this for my posts ?

Awesome! 馃槃

Is there a function I could use to automatically do this for my posts?

The plugin's slug is "autodescription", but it does have its limits. If you're using a page builder, then I'm afraid this isn't possible.
In any case, it's always best to forge manual descriptions. So, making a habit out of it isn't all too bad!

I actually want this for my posts only and i have an ACF field called "fl_description". So it should be simple enough to write a function to truncate the "fl_description" and pass this on to the SEO custom description ?

actually, from the past, someone gave me this code which does exactly that!

add_filter( 'the_seo_framework_description_output', 'my_description_excerpt', 10, 2 );
add_filter( 'the_seo_framework_ogdescription_output', 'my_description_excerpt', 10, 2 );
add_filter( 'the_seo_framework_twitterdescription_output', 'my_description_excerpt', 10, 2 );

function my_description_excerpt( $description = '', $id = 0 ) {

    $custom_desc = get_post_meta( $id, 'fl_description', true );
  $custom_desc = wp_strip_all_tags( $custom_desc, true );

    if ( $custom_desc ) {

        $custom_desc = the_seo_framework()->trim_excerpt( $custom_desc, mb_strlen( $custom_desc ), 155 );

        if ( method_exists( the_seo_framework_class(), 's_excerpt' ) ) {
            $custom_desc = the_seo_framework()->s_excerpt( $custom_desc );
        }

        $description = the_seo_framework()->escape_description( $custom_desc );
    }

    return $description;
}

However, it's only when I manually fill the description box for the post that I get the twitter cards.. strange..

Hi @neodjandre2

I believe this will do, you can add it to your theme's functions.php file:

add_filter( 'the_seo_framework_custom_field_description', function( $description, $args ) {

    // Return manual TSF description if set.
    if ( $description )
        return $description;

    /**
     * Try ACF and set if exists.
     * @link https://www.advancedcustomfields.com/resources/get_field/
     */
    if ( function_exists( 'get_field' ) ) {
        // Pass ID for SEO bar compatibility.
        $description = get_field( 'fl_description', $args['id'], false ) ?: $description;
    }

    return $description;
}, 10, 2 );

EDIT: Disabled ACF auto-format on get_field().

@neodjandre2 The filters you've used only apply to the front-end.
The example I just gave works from TSF 2.9.0 馃槃, which covers the whole spectrum.

EDIT:
There's no need to escape, as it's done at all times after the filter runs.
If your ACF descriptions tend to be very long (for any reason), then you might indeed wish to apply the trimming function.

ok in this case. I will replace with your new function but is it possible to trim_excerpt to 155 characters with the new function please.. I am not good with modifying functions!

@neodjandre2 here goes...

add_filter( 'the_seo_framework_custom_field_description', function( $description, $args ) {

    // Return manual TSF description if set.
    if ( $description )
        return $description;

    /**
     * Try ACF, trim and set if exists.
     * @link https://www.advancedcustomfields.com/resources/get_field/
     */
    if ( function_exists( 'get_field' ) ) {
        // Pass ID for SEO bar compatibility.
        $excerpt = get_field( 'fl_description', $args['id'] ) ?: '';

        // Only trim if there's actually something found.
        if ( $excerpt ) {
            $description = the_seo_framework()->trim_excerpt( $excerpt, mb_strlen( $excerpt ), 155 ); 
        }
    }

    return $description;
}, 10, 2 );

Ok perfect this works fine but at the beginning of my descriptions I always get these characters:

<p>

why is that? :)

I have added:

$excerpt = strip_tags(html_entity_decode($excerpt));

and it seemed to fix that.. :) thanks for all the help !

&lt;p&gt; === <p>... ACF might add that through formatting?
So, it might also be solved by adding false as the third parameter to the ACF function, which is a little cleaner and faster.

$excerpt = get_field( 'fl_description', $args['id'], false ) ?: '';

Nevertheless, I'm glad to see we've been able to forge a solution! Have a wonderful weekend 馃槃

yes your solution is better 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samburgers picture samburgers  路  6Comments

alpipego picture alpipego  路  4Comments

strarsis picture strarsis  路  4Comments

Hube2 picture Hube2  路  3Comments

paaljoachim picture paaljoachim  路  5Comments