Get_canonical_url causes cornerstone error

If I change the canonical url of page, cornerstone wont work in preview mode, and I get the error “Uh oh! An unidentifiable error is preventing the live preview from loading. Switching to Skeleton Mode” .

I’m using the standard wordpress filter get_canonical_url (https://developer.wordpress.org/reference/hooks/get_canonical_url/)

Is there a way to know that cornerstone is active and then disable this filter?

I’m looking for something like If (!is_cornerstone_active() ) {get_canonical_url (...) }

Does such a function exist for cornerstone or is there some other way to know that the user is editing a page / post?

I’ve tried is_admin() but that doesn’t work when editing cornerstone.

Thanks in advance

Hello There,

Thanks for writing in! To resolve your issue, you can test out by using this condition in the example code below:

// function uses cornerstone
function do_nothing(){
  $post = get_post();

  if ( CS()->common()->uses_cornerstone( $post ) ) {
    echo "Cornerstone active!";
  } else {
    echo "Not active";
  }
}
add_action('wp_footer', 'do_nothing');

This function will display the status of the post right below in the footer. This does not do anything. It will just demonstrate the condition I used so that you can apply it to your own code.

Hope this helps.

Thanks, but the get_canonical_url hook happens in wp_head. Is there something higher up in the hook sequence where this will work ?

Also this function seems to return true if the post is being edited or not. I just need a function to indicate that the post is being edited in cornerstone. Not if the post has been edited with cornerstone.

Thanks

Hey There,

Since your hook happens in the wp_heade, then you can have your function like this:

// function uses cornerstone
function do_nothing(){
  $post = get_post();

  if ( CS()->common()->uses_cornerstone( $post ) ) {
    // do something
  } else {
    // do nothing
  }
}
add_action('wp_head', 'do_nothing');

Please keep in mind that this is just an example code. The uses_cornerstone() will return true or false whether the page is built with Cornerstone or not. Even if the page was created in Cornerstone but then edited outside of Cornerstone, it will return false.

“I just need a function to indicate that the post is being edited in cornerstone”

  • I am afraid that we do not have this feature yet. This is something we can add to our list of feature requests. This way it can be taken into consideration for future development. All of these items are discussed with our team internally and prioritized based on the amount of interest a particular feature might receive.

Thanks!

Then please look into the bug when the using get_canonical_url causes to cornerstone to fallback to preview mode.

Hi There,

Thanks for getting back to us. I will be submitting your thread to one of our developers. He should be able to answer more about this issue.

Thank you for your understanding.

Hi Blake,

Based on that error, it sounds like you’re using an older version of Cornerstone. Could you try updating and let us know if you’re experiencing the same errors? We made some changes to how the builder works, and it now loads based on the post ID, so the slugs shouldn’t matter anymore.

Thanks … I’m on pro 1.1.1

Hi there,

May I know the complete code you’re adding? I need to know how you’re implementing it so I could reproduce it on my install.

Thanks!

Here’s the code:

add_filter('get_canonical_url', static::class . '::get_canonical_url', 10, 2);


public static function get_canonical_url($canonical_url, $post)
{
	if ($post->ID == 5577) {
		return 'https://www.samedomain.com/some_other_url';
	}
	return $canonical_url;
}

Hi there,

Please try this condition,

if ( ! (  isset( $_GET['cornerstone_preview'] )  && $_GET['cornerstone_preview'] == '1'  )   ) {
      switch( $post->ID ) {
            case 5577: return 'https://www.samedomain.com/some_other_url'; break;
            case 5578: return 'https://www.samedomain.com/some_other_url'; break;
            case 5579: return 'https://www.samedomain.com/some_other_url'; break;
      }	
}
return $canonical_url;

The preview is loaded through the iframe with $_GET['cornerstone_preview'] URL parameter.

Thanks!

Perfect! Thanks this works :slight_smile:

You’re most welcome!