Setting a custom canonical URL in Yoast Wordpress SEO Plugin

0 votes

I'm having some trouble setting a custom canonical title using the Wordpress SEO API: http://yoast.com/wordpress-seo-api-docs/

I have a custom post type called designs which uses a custom URL rewrite. It takes the base page /design/ and adds the design name to it like /design/a-design/. The canonical in Wordpress SEO by default is the /design/ page.

What I want to do is write a function which determines if it is a design page and return a different canonical. I can test whether it's a design page by doing if ($design == ""){ and I tried to use the custom permalink URL, but the function just removes the canonical completely.

Here's my basic function:

function design_canonical(){
    if ($design == "") {    
        // Leave blank and Yoast SEO will use default canonical for posts/pages
    }
    else {
        return $design['detailslink'];
    }
}
add_filter( 'wpseo_canonical', 'design_canonical' )

Quite clearly doing something wrong, but I'm not entirely sure what.

Feb 26, 2022 in Others by Kichu
• 19,040 points
1,176 views

1 answer to this question.

0 votes
function design_canonical($url) {
    global $post;

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


try this code as a solution 
answered Feb 27, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Removing canonical tag added by Yoast SEO plugin

first  Log in to your WordPress website ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,600 points
2,673 views
0 votes
1 answer

How to Access Yoast SEO tags in Wordpress API Callback

use rest api to request for the ...READ MORE

answered Feb 12, 2022 in Others by narikkadan
• 63,600 points
1,846 views
0 votes
1 answer

Yoast SEO (WordPress Plugin) - Get plugin generated data manually

get_post_meta(), use this to get the meta ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,600 points
1,516 views
0 votes
1 answer

Why is just an ID in the URL path a bad idea for SEO?

yes it affects the click through rates ...READ MORE

answered Feb 20, 2022 in Others by narikkadan
• 63,600 points
523 views
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

the way the search engine displays your ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,600 points
512 views
0 votes
0 answers

SEOPress plugin not working with Wordpress 5.7

An error of type E_ERROR was caused ...READ MORE

Feb 14, 2022 in Others by Kichu
• 19,040 points
896 views
0 votes
1 answer

Limit the size of the existing and new permalink slugs in Wordpress for SEO

?php   /**  * Trim native slugs  */ function pm_trim_native_slug($slug, $post_ID, ...READ MORE

answered Feb 25, 2022 in Others by narikkadan
• 63,600 points
1,977 views
0 votes
1 answer

Wordpress Critical Error Due to Plugin Conflict with my Functions.php

// WooCommerce Stock message add_filter( 'woocommerce_get_availability', 'mw_get_availability', ...READ MORE

answered Feb 26, 2022 in Others by narikkadan
• 63,600 points
1,124 views
0 votes
1 answer

How to add meta keyword in wordpress using yoast seo plugin.?

if you dont want to use code ...READ MORE

answered Feb 12, 2022 in Others by narikkadan
• 63,600 points
1,012 views
0 votes
1 answer

adding meta keyword in wordpress using yoast seo plugin.

You can use Advance Custom Fields plugin ...READ MORE

answered Feb 28, 2022 in Others by narikkadan
• 63,600 points
1,634 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP