2

How can I add post content in meta description via Title & Metas?

I have seen excerpt variable %%excerpt%% but it will give only excerpt but I want whole content in meta description.

Can any one help me for this because I can not found variable for content?

Thankns

2
  • You cannot find is because it's not there and for good reason. You are not suppose to add whole content in meta description tag. It's useless since search engines will only show limited chars. Regardless of that if you show content in meta description tag then it will defeat the whole purpose of having meta description in the first place.
    – Robert hue
    Commented Jun 19, 2015 at 8:01
  • I am using custom post type and in my custom post type does not have much content. Its have only one small question in content. Commented Jun 19, 2015 at 8:11

2 Answers 2

3

Here is a safe and Yoast preferred method

add_action('wp_head','add_custom_meta_description_box');

function retrieve_var1_replacement( $var1 ) {
        global $post;
       return strip_tags($post->post_content);
}
function register_my_plugin_extra_replacements() {
       wpseo_register_var_replacement( '%%mycustomdesc%%', 'retrieve_var1_replacement', 'advanced', 'this is a help text for myvar1' );
}
add_action( 'wpseo_register_extra_replacements', 'register_my_plugin_extra_replacements' );

You can now replace your %%excerpt%% with %%mycustomdesc%%

6
  • This will create 2 meta description tags.
    – Robert hue
    Commented Jun 19, 2015 at 7:55
  • I need a help to create new variable in yoast as like others and I think it is easier because I need this option only for my custom post type. Commented Jun 19, 2015 at 7:58
  • The best way to solve this issue, is to copy paste your content in excerpt field too and use %%excerpt%% in meta description.
    – Robert hue
    Commented Jun 19, 2015 at 8:03
  • Hi @Roberthue, you are right. I have changed the old code and added new one which we can safely use and replace %%excerpt%%
    – Karun
    Commented Jun 19, 2015 at 8:34
  • Where do I have to add these code in function.php or in single.php? Commented Jun 19, 2015 at 10:48
2

In Yoast SEO there is no variable available for the content. The 2 variables that are available are %%excerpt%% and %%excerpt_only%%. The difference between these 2 is:

%%excerpt%% - Replaced with the post/page excerpt (or auto-generated if it does not exist)

%%excerpt_only%% - Replaced with the post/page excerpt (without auto-generation)

For a full list of available variables you can click on the "Help" tab (top right) when viewing the "Title and Metas" page of the Yoast SEO plugin.

If using %%excerpt_only%% and adding your post content in your post excerpt is not going to work for you then you could write your own function to hook into the wpseo_metadesc filter.

function custom_seo_description( $seo_desc ) {
  $seo_desc = "This is a custom description";
  return $seo_desc;
}

add_filter( 'wpseo_metadesc', 'custom_seo_description', 10, 1 );

I've not tested this code but something along theser lines should work. More info on the Yoast SEO API can be found here: https://yoast.com/wordpress/plugins/seo/api/

1
  • I have already mention this variable in my question. I want full content on excerpt. Commented Jun 19, 2015 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.