Skip to content

Instantly share code, notes, and snippets.

@palimadra
Created June 30, 2012 14:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palimadra/3023928 to your computer and use it in GitHub Desktop.
Save palimadra/3023928 to your computer and use it in GitHub Desktop.
WordPress - Automatically generate meta description from content
function create_meta_desc() {
global $post;
if (!is_single()) { return; }
$meta = strip_tags($post->post_content);
$meta = strip_shortcodes($post->post_content);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 125);
echo "<meta name='description' content='$meta' />";
}
add_action('wp_head', 'create_meta_desc');
@jphilippe2017
Copy link

Hi, I am new at this and I need your help please. I would like all my WordPress posts and pages to have individual meta titles and descriptions, from what the page or post says on it, so where can I input this code to make sure that all my posts and pages in my site has Automatically generate meta description from content and if possible Automatically generate meta Titles from content.

I just don't know where I need to install this code and if it will work for meta titles also?

Thank you

@jphilippe2017
Copy link

Also, Can I use your code with this plugin to allow me to have automatically generated meta description from content?

Please see this plugin: https://en-ca.wordpress.org/plugins/head-meta-data/

Can I use your code in this plugin you think?

@jphilippe2017
Copy link

What would be the exact markup code for this and do I put this in the head:

function create_meta_desc() {
global $post;
if (!is_single()) { return; }
$meta = strip_tags($post->post_content);
$meta = strip_shortcodes($post->post_content);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 125);
echo "";
}
add_action('wp_head', 'create_meta_desc');

@denisenadal
Copy link

This is a really handy gist! I made some modifications to allow it to use the post_excerpt for a meta description if it existed, and generate a description for posts types without an excerpt, such as pages.

I also always have problems with strip_shortcodes() stripping out all my content, so I ran the content filter before strip_tags and strip_shortcodes .

function create_meta_desc() {
    global $post;
	if (!is_singular()) {return; }
	elseif(!empty( $post->post_excerpt)) {
		echo "<meta name='description' content='$post->post_excerpt' />";
	}
	else{
		$meta = apply_filters('the_content', $post->post_content);
		$meta = strip_tags($meta);
		$meta = strip_shortcodes($meta );
		$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
		$meta = substr($meta, 0, 400);
		echo "<meta name='description' content='$meta' />";
	}
}
add_action('wp_head', 'create_meta_desc');

@onliniak
Copy link

onliniak commented Aug 24, 2018

Thanks for this gist, but it need little update for people who migrate from classic editor to Gutenberg.

You must add"<!-- wp:paragraph -->", "<p>"to str_replace, in Gutenberg all paragraphs are begining (and ending) with <!-- wp:paragraph --> also sometimes Gutenberg add <p> to html.

// $meta = str_replace( array("\n", "\r", "\t", "<!-- wp:paragraph -->", "<p>", "<!--nextpage-->"), ' ', $meta );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment