adding wordpress yoast SEO canonical url from "transition_post_status" hook
I am using transition_post_status
hook to add get some data at the time of post is publish but i can access any variables but when i am trying to set canonical url from add_filter
it is return 500 error
// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);
// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
if('publish' === $new_status $post-post_type === 'post') {
print_r('this is done');
$post_meta = get_post_meta($post-ID, '', true);
$object_id = $post_meta['mydo_post_id'][0];
$canonical_url = 'https://mylofamily.com/article/'.$object_id;
add_filter( 'wpseo_canonical', 'add_canonical' );
function add_canonical($url){
return $canonical_url;
}
}
}
Above is my code i am using can anyone help?
Topic rel-canonical hooks Wordpress
Category Web