Add Page number to Meta Description in WordPress SEO by Yoast

The code below is from WordPress SEO plugin by Yoast. I am trying to add the page number to the Meta description on paginated posts (to avoid duplication issues with Google).

function metadesc( $echo = true ) {
        if ( get_query_var('paged') && get_query_var('paged') > 1 )
            return;

        global $post, $wp_query, $page;
        $options = get_wpseo_options();

        $metadesc = '';
        if (is_singular()) { 
            $metadesc = wpseo_get_value('metadesc');
            if ($metadesc == '' || !$metadesc) {
                if ( isset($options['metadesc-'.$post->post_type]) && $options['metadesc-'.$post->post_type] != '' )
                    $metadesc = wpseo_replace_vars($options['metadesc-'.$post->post_type], (array) $post );
            }
        } 

        $metadesc = apply_filters( 'wpseo_metadesc', trim( $metadesc ) );

        if ( $echo ) {
            if ( !empty( $metadesc ) )
                echo '<meta name="description" content="'.esc_attr( strip_tags( stripslashes( $metadesc ) ) ).''.Page .''.$page.'"/>'."n";
            else if ( current_user_can('manage_options') && is_singular() )
                echo '<!-- '.__( 'Admin only notice: this page doesn't show a meta description because it doesn't have one, either write it for this page specifically or go into the SEO -> Titles menu and set up a template.', 'wordpress-seo' ).' -->'."n";          
        } else {
            return $metadesc;
        }

    }

I have added $page as a global variable and can output the page number like so:

Read More
<meta name="description" content="Wordpress SeoPage3"/>

I would like the output to omit the page number if on Page0 (i.e. first page) then add spaces and separator (pipe or dash) so it reads

<meta name="description" content="Wordpress Seo Page | 3"/>

Thanks..

Related posts

Leave a Reply

2 comments

  1. To add a page number regardless to any plugin … use filters. Do not change the plugin file. You cannot run updates other wise.

    Example:

    <?php # -*- coding: utf-8 -*-
    /**
     * Plugin Name: T5 Add page number to title
     * Description: Adds <code> | Page $number</code> to the page title.
     * License:     MIT
     * License URI: http://www.opensource.org/licenses/mit-license.php
     */
    
    if ( ! function_exists( 't5_add_page_number' ) )
    {
        function t5_add_page_number( $s )
        {
            global $page;
            $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
            ! empty ( $page ) && 1 < $page && $paged = $page;
    
            $paged > 1 && $s .= ' | ' . sprintf( __( 'Page: %s' ), $paged );
    
            return $s;
        }
    
        add_filter( 'wp_title', 't5_add_page_number', 100, 1 );
        add_filter( 'wpseo_metadesc', 't5_add_page_number', 100, 1 );
    }