• Resolved Bhanu Singh

    (@bhanusingh)


    Hey,

    I have seen many implementation of this filter “the_seo_framework_rel_canonical_output”

    I need the post ID of the page in order to change the canonical link which I am unable to get.
    I could try “url_to_postid()” function but the problem is that I have changed the permalink of my website using a plugin which makes the link of my product pages link.

    example.com/category/product-slug

    So I am unable to get ID from there. A little bit of digging I found,

    
    
    		/**
    		 * @since 2.6.5
    		 * @param string $url The canonical URL. Must be escaped.
    		 * @param int    $id  The current page or term ID.
    		 */
    		$url = (string) \apply_filters_ref_array(
    			'the_seo_framework_rel_canonical_output',
    			[
    				$_url,
    				$this->get_the_real_ID(),
    			]
    		);
    

    I can see the URL being pased to the filter, but when I try to get it like this.

    
    
    add_filter( 'the_seo_framework_rel_canonical_output', function( $url , $id ) {
    
    	var_dump($url, $id);
    
    	return $url;
    } );
    

    I get the following error

    
    Too few arguments to function App\{closure}(), 1 passed in C:\laragon\www\cfi\wp-includes\class-wp-hook.php on line 289 and exactly 2 expected
    

    How can I get the ID of product here ?

    Thanks.

    • This topic was modified 3 years, 2 months ago by Bhanu Singh.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    You can set a custom canonical URL for every page under the Visibility tab of the Page SEO Settings (you can also access this via quick-edit). You should be able to use that if a page has an ID. Nevertheless, if you have special requirements, then filters do come in handy.

    WordPress’s custom PHP filters have four parameters: $tag, $function_to_add, $priority, and $accepted_args.
    You need to add the $accepted_args when you need to add more than one parameter.

    See: https://tsf.fyi/docs/filters

    add_filter( 'the_seo_framework_rel_canonical_output', function( $url , $id ) {
    
    	var_dump($url, $id);
    
    	return $url;
    }, 10, 2 ); // Added '10, 2'

    I hope this helps! Cheers 🙂

    Thread Starter Bhanu Singh

    (@bhanusingh)

    Hey @cybr,

    This worked. Thanks a lot.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get ID of the post when using ‘the_seo_framework_rel_canonical_output’’ is closed to new replies.