Ask your WordPress questions! Pay money and get answers fast! (more info)

Customize Open Graph tags on paginated posts/pages WordPress

  • SOLVED

I´m using Yoast SEO plugin to print Open Graph data to the header of my theme.

The <strong>og:url</strong> always shows the current URL the user is visiting:
<meta property="og:url" content="http://www.example.com/mypost/" />

However, on paginated posts I want it to show the URL of the first/main post in the series.

So for example on page 30, instead of this URL:
<meta property="og:url" content="http://www.example.com/mypost/30/" />

You´d see this:
<meta property="og:url" content="http://www.example.com/mypost/" />


It looks like the Yoast SEO plugin has an action that we can use to accomplish this:
https://yoast.com/wordpress/plugins/seo/api/
http://hookr.io/actions/wpseo_opengraph/

The code I´m looking for does two things:

1. It checks if we´re on a paginated post or page (see resources)
2. If we´re on a paginated post/page, it replaces the og:url tag with the URL of first post/page in the series. (as shown in the example earlier in this post)

Resources:

http://wordpress.stackexchange.com/questions/21953/how-to-know-if-the-post-has-pagination-nextpage-or-not
http://wordpress.stackexchange.com/questions/31065/how-to-determine-if-im-on-the-first-page-of-pagination

Answers (2)

2015-11-21

dimadin answers:

Here is code that does this. It doesn't check if this is paginated page or not because it uses Yoast SEO method that gives URL without pagination (second parameter 'true'). It does check if we are on post or page post type, if you want this to work with any post type just remove array with post types names.


function md_no_paging_in_og( $url ) {
if ( is_singular( array( 'post', 'page' ) ) ) {
$url = WPSEO_Frontend::get_instance()->canonical( false, true );
}

return $url;
};
add_filter( 'wpseo_opengraph_url', 'md_no_paging_in_og' );


MattVic comments:

Wow thanks, it works like a charm and it´s a super elegant solution!

2015-11-19

Rempty answers:

If your url is like http://www.example.com/mypost/page/30/ (need "/page/")

add_filter( 'wpseo_opengraph_url', 'my_gv_wpseo_canonical' );
function my_gv_wpseo_canonical( $canonical ) {
global $post;
$paged = get_query_var('paged');
if(isset($paged) && $paged!='0'){
$pos=strpos($canonical,'page/');
$url=substr($canonical,0,$pos);
return $url;
}
return $canonical;
}


MattVic comments:

Thanks for your answer. That code filters the canonical URL. not the Open Graph tag.


Rempty comments:

Try this, i forgot to change the filter name


add_filter( 'wpseo_opengraph_url', 'my_gv_wpseo_canonical' );
function my_gv_wpseo_canonical( $canonical ) {
global $post;

$paged = get_query_var('paged');
if(isset($paged) && $paged!='0'){
$pos=strpos($canonical,'page/');
$url=substr($canonical,0,$pos);
return $url;
}
return $canonical;
}


MattVic comments:

That might work for pages, but it doesn´t check if we´re on a paginated post. That was one of the requirements:

<blockquote>The code I´m looking for does two things:
1. It checks if we´re on a paginated <strong>post</strong> or page (see resources)
</blockquote>


Rempty comments:

This check for pagination
first check fot the pagination variable


//Here get the value from query var ('paged'), when the query var paged exist?, only on pages with pagination
$paged = get_query_var('paged');

//now check the current variable

if(isset($paged) && $paged!='0'){


Rempty comments:

on pages and posts or everything have using the query_var('paged'), because wordpress use the query_var('paged') to paginate.