Site logo

[Support request] How To Add Post Meta Title, Meta Description and Keywords

Home Forums Support [Support request] How To Add Post Meta Title, Meta Description and Keywords

Home Forums Support How To Add Post Meta Title, Meta Description and Keywords

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1756472
    Bozlur

    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.

    #1756624
    Elvin
    Staff
    Customer Support

    Hi there,

    You can try these.
    https://wpdatatables.com/how-to-add-meta-tags-in-wordpress-without-a-plugin/
    https://gretathemes.com/seo-add-meta-tags-without-plugins/

    While they’re not GeneratePress specific, it shouldn’t matter anyway as the functions are WordPress core.

    It’s a bit tedious. That’s why most users use plugins for this. 🙂

    #1756975
    Bozlur

    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?

    #1757362
    David
    Staff
    Customer Support

    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.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.