Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We use a very customized version of WordPress for some of our index/root site pages where we use a custom meta box for custom permalinks. What we are looking for is a custom function that basically looks to see if the custom permalink field is being used and if it is, to grab that as the canonical url. This helps with duplicate content being indexed by search engines and we're gearing up to use Outbrain, who will be crawling our canonical urls.

I came across this post on Stack Overflow but am not sure how to edit for my needs.

function design_canonical() {
   global $post;

   if ( get_post_type( $post->ID ) == 'design' ) {
       return site_url( '/design/' . $post->post_name );
   } else {
       // Leave blank and Yoast SEO will use default canonical for posts/pages
   }
}
add_filter( 'wpseo_canonical', 'design_canonical' )

I'm thinking the post->ID doesn't need the 'design' and the return site_url should be my custom meta box. How to edit it properly?

share|improve this question
    
What have you tried or where has your research led? You are expected to have researched the problem and made an attempt at solving it before posting a question. –  s_ha_dum Mar 28 '13 at 13:11
    
I came across this post but am not sure how to customize it for my needs. –  JusticeIsCheap Mar 28 '13 at 13:25

1 Answer 1

up vote 0 down vote accepted

What you need is get_post_meta instead of get_post_type

This:

if ( get_post_type( $post->ID ) == 'design' ) {

Should be:

$meta = get_post_meta($post->ID,'your-meta-key',true);
if (!empty($meta)) {

Put altogether

function design_canonical_wpse_93717() {
   global $post;
   $meta = get_post_meta($post->ID,'your-meta-key',true);
   if (!empty($meta)) {
       return $meta; // assuming this is an absolute URL
   }
}
add_filter( 'wpseo_canonical', 'design_canonical_wpse_93717' );

The else conditional was spurious so I removed it, and I am assuming that the filter works as advertised. I don't use WP-SEO.

Also, I am a bit concerns as usually filters work more like this:

function design_canonical_wpse_93717($url) {
   global $post;
   $meta = get_post_meta($post->ID,'your-meta-key',true);
   if (!empty($meta)) {
       $url = $meta; // assuming this is an absolute URL
   }
   return $url;
}
add_filter( 'wpseo_canonical', 'design_canonical_wpse_93717' );

That is, usually filters pull in content via a parameter and pass that content back out, but again I don't use that plugin.

share|improve this answer
    
Thanks, I'll give that a try. On Edit: Works a treat. I put the else function back in for pages/posts that don't use the custom permalink. I'll just have to find one of those in our site to make sure that works. –  JusticeIsCheap Mar 28 '13 at 14:11
    
So, I just checked a page and it isn't showing the canonical link at all (the pages def use the WP generated permalink). function design_canonical_wpse_93717() { global $post; $meta = get_post_meta($post->ID,'_pbsparents_cp',true); if (!empty($meta)) { return $meta; // assuming this is an absolute URL } else { // Leave blank and Yoast SEO will use default canonical for posts/pages } } add_filter( 'wpseo_canonical', 'design_canonical_wpse_93717' ); –  JusticeIsCheap Mar 28 '13 at 14:17
    
Put return 'this-is-a-test'; immediately after the opening { and see if that prints to the page. –  s_ha_dum Mar 28 '13 at 14:19
    
Actually, I just placed your second function into the functions file and it seems to be working for both instances. I'm getting the custom meta field and when there isn't one, I'm getting the WP permalink. So, it appears as though it's working properly! Thanks so much. –  JusticeIsCheap Mar 28 '13 at 14:21
    
That is why I was worried about the format of that answer you found. –  s_ha_dum Mar 28 '13 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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