How I can setup/override meta description tag values for arbitrary pages of my site? I have a lot of pages, generated dynamically by script. I need to add dynamically generated meta description for all of these pages. How can I do it using standard Wordpress abilities?

I don't want to use plugins for this task.

A solution using filters is highly preferrable. Thanks.


You can use first few lines of post content and add these to meta description tags to website head section automatically.

Here is how you do that.

// add meta description tag
function wcs_add_meta_description_tag() {
    global $post;
    if ( is_single() ) {
        $meta = strip_tags( $post->post_content );
        $meta = strip_shortcodes( $post->post_content );
        $meta = str_replace( array("\n", "\r", "\t"), ' ', $meta );
        $meta = mb_substr( $meta, 0, 125, 'utf8' );
        echo '<meta name="description" content="' . $meta . '" />' . "\n";
add_action( 'wp_head', 'wcs_add_meta_description_tag' , 2 );

Ofcourse You can change this code to use post excerpt for meta description tag.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.