• Resolved nobleknight

    (@nobleknight)


    Hello.

    I’m using the following code (from Tom I think, or maybe another member of the GeneratePress team) to display the meta description field on posts, which works great; however, I want to do the same for pages as I have a custom homepage to which I want to add meta description without the need of course of any SEO plugin.

    How can I change it to perform the same for both posts and pages?

    1- Here is the code I’m using to create a custom field for Meta Description to display below posts in the editor:

    function add_seo_custom_fields() {
    add_meta_box(
    'seo_custom_fields', // Unique ID
    'SEO', // Box title
    'render_seo_custom_fields', // Content callback, must be of type callable
    'post', // Post type
    'normal', // Context
    'high' // Priority
    );
    }
    add_action( 'add_meta_boxes', 'add_seo_custom_fields' ); function render_seo_custom_fields( $post ) {
    $meta_description = get_post_meta( $post->ID, 'meta_description', true );
    wp_nonce_field( basename( FILE ), 'seo_custom_fields_nonce' );
    ?>
    Meta Description:
    <?php echo esc_textarea( $meta_description ); ?> <?php
    } function save_seo_custom_fields( $post_id ) {
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'seo_custom_fields_nonce' ] ) && wp_verify_nonce( $_POST[ 'seo_custom_fields_nonce' ], basename( FILE ) ) ) ? 'true' : 'false'; if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
    } if ( isset( $_POST[ 'meta_description' ] ) ) {
    update_post_meta( $post_id, 'meta_description', sanitize_text_field( $_POST[ 'meta_description' ] ) );
    }
    }
    add_action( 'save_post', 'save_seo_custom_fields' );

    2- Here is the code I’m using to allow adding Meta Description content to its field:

    add_action( 'wp_head', function() {
        if ( is_singular() ) {
            $post_id = get_the_ID();
    
            // Set meta title to post title if custom meta title is not set
            $meta_title = get_post_meta( $post_id, 'meta_title', true );
            if ( ! $meta_title ) {
                $meta_title = get_the_title( $post_id );
            }
    
            // Set meta description to first 160 characters of post content, stripped of HTML tags, if custom meta description is not set
            $meta_desc = get_post_meta( $post_id, 'meta_description', true );
            if ( ! $meta_desc ) {
                $post_content = get_post_field( 'post_content', $post_id );
                $meta_desc = mb_substr( wp_strip_all_tags( $post_content ), 0, 160 );
            }
    
            // Replace newline characters with spaces
            $meta_desc = str_replace( "\n", " ", $meta_desc );
    
            // Replace multiple spaces with a single space
            $meta_desc = preg_replace( '/\s+/', ' ', $meta_desc );
    
            // Output meta title and meta description
            if ( $meta_title ) {
                printf( '<meta name="title" content="%s"/>', $meta_title );
                echo "\n";  // Add a line break
            }
    
            if ( $meta_desc ) {
                printf( '<meta name="description" content="%s"/>', $meta_desc );
    			echo "\n";  // Add a line break
            }
        }
    } );

    I guess probably that I would need to add ‘page’ as a post type in function add_seo_custom_fields(), but I’m not sure for the rest; I’m afraid to break the code as I’m not good in PHP.

    Any help from you would be greatly appreciated.
    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • ying

    (@yingscarlett)

    Hi there,

    You can include page in the add_seo_custom_fields() function, change the code to this:

    function add_seo_custom_fields() {
        add_meta_box(
            'seo_custom_fields', // Unique ID
            'SEO', // Box title
            'render_seo_custom_fields', // Content callback, must be of type callable
            array('post', 'page'), // Post types (both post and page)
            'normal', // Context
            'high' // Priority
        );
    }
    Thread Starter nobleknight

    (@nobleknight)

    Thank you very much, Ying. Your code worked very well.

    At first, I had a syntax error when I copy-pasted it to my website, probably because of spaces issue; so, I just wrote the desired line and it worked.

    Thanks again.

    ying

    (@yingscarlett)

    Glad to hear that ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add meta description field to pages (not posts)?’ is closed to new replies.