Site logo

Archived topic

How To Add Post Meta Title, Meta Description and Keywords

3 replies · Started by Bozlur on April 29, 2021

Viewing posts 1–4 of 4

Hi,
I don't use any SEO Plugins like Yoast or Rankmath. So, without using seo plugins, how can I add Add custom Post Meta Title, Meta Description and Keywords for each post in GP Premium.

Hi Elvin,
Thank you for your reply. Let me clear my question so that you can understand that its GeneratePress specific. There was hook/section elements in GP. So Is it possible to add Add Post Meta Title, Meta Description and Keywords in every post with hooks? If yes then how?

Hi there,

you can convert either of the codes provided in the posts that Elvin provided.
Heres one of those codes.

function gretathemes_meta_description() {

    global $post;

    if ( is_singular() ) {

        $des_post = strip_tags( $post->post_content );

        $des_post = strip_shortcodes( $post->post_content );

        $des_post = str_replace( array("\n", "\r", "\t"), ' ', $des_post );

        $des_post = mb_substr( $des_post, 0, 300, 'utf8' );

        echo '<meta name="description" content="' . $des_post . '" />' . "\n";

    }

    if ( is_home() ) {

        echo '<meta name="description" content="' . get_bloginfo( "description" ) . '" />' . "\n";

    }

    if ( is_category() ) {

        $des_cat = strip_tags(category_description());

        echo '<meta name="description" content="' . $des_cat . '" />' . "\n";

    }

}

add_action( 'wp_head', 'gretathemes_meta_description');

The code is using the add_action function to hook in the custom function into the wp_head. thats not required as the Hook Element does that.

You would use a Hook Element > wp_head instead.

Then stripping out the code from the function.
The code uses conditonal tags such as if ( is_singular() ) { for displaying the Meta Description, this is not required as this would be the Display Rule Locations.

You would use Display Rules Location: All Singular for that condition.

This leaves you with this code to add to your Hook which will add the Meta Description:

<?php
global $post;
$des_post = strip_tags( $post->post_content );
$des_post = strip_shortcodes( $post->post_content );
$des_post = str_replace( array("\n", "\r", "\t"), ' ', $des_post );
$des_post = mb_substr( $des_post, 0, 300, 'utf8' );
echo '<meta name="description" content="' . $des_post . '" />' . "\n";
?>

Note the global $post; will be required in the subsequent meta functions. And all must be wrapped in <?php ?> tags.
And in the Hook settings you must check Execute PHP.

You would need to create a separate hook for each of the different if conditions in that code.

For Keywords - well there isn't a meta field for those in WP - so you would need to create Custom Fields for those and use the get_post_meta() to grab that data.

https://developer.wordpress.org/reference/functions/get_post_meta/

Hope that helps.

This archived topic is closed to new replies.