Archived topic
Editing meta title and meta description in each page
5 replies · Started by Ricardo on May 14, 2020
Hi,
I'm sorry if this out of your scope but could you give me some orientation on how to edit meta title and meta description (or how to edit <head>) on each page and post, without installing heavy plugins like yoast SEO?
Thanks in advance,
Ricardo
Hi there,
You would need to add custom fields to your posts/pages. A plugin like ACF might help with that.
Then you can do this:
add_action( 'wp_head', function() {
if ( is_singular() ) {
$meta_title = get_post_meta( get_the_ID(), 'your_meta_title_custom_field', true );
$meta_desc = get_post_meta( get_the_ID(), 'your_meta_description_custom_field', true );
if ( $meta_title ) {
printf( '<meta name="title" content="%s">', $meta_title );
}
if ( $meta_desc ) {
printf( '<meta name="description" content="%s">', $meta_desc );
}
}
} );
Thanks a lot!
You and your team are very kind. If you ever need guitar lessons, I'll be glad to offer you.
May I assume that in terms of performance and stability (no need to update plugin) this can be better than a SEO plugin?
Best regards,
Ricardo
No problem!
Performance-wise, it's way better than an SEO plugin. However, those plugins do a lot more than just add meta titles/descriptions to your site, so functionality-wise it's not even close.
We use Yoast here on this website. Rank Math is also worth checking out if you want to do some performance tests.
Hi,
I know those plugins, but for a specific project I just need to edit the meta description and title, so why using them?
once again, thanks a lot!
That's totally fair! In that case, custom fields are the way to go :)