I created a plugin that, when updated, gets a link to a published record and sends it to the API. So I get the URL, but sometimes wordpress gives me such a link: site.com/?p=123, and should site.com/url/

add_action( 'post_updated', array( $this, 'on_post_updated' ), 10, 3 );

     function on_post_updated( $post_ID, $post_after, $post_before ) {
                if ( wp_is_post_revision( $post_ID ) ) {
                    return false;

                $url = get_permalink( $post_ID );
                $url = $this->base_url . $url;
                wp_remote_get( $url );

How can I fix this problem?

  • Where does on_post_updated() run? What are your Permalink settings set to? Is $post_id() published? – Jacob Peattie Sep 25 '18 at 12:49
  • i use: add_action( 'post_updated', array( $this, 'on_post_updated' ), 10, 3 ); – Marat Petrov Sep 25 '18 at 13:04
  • What is in $this->base_url? get_permalink returns a full URL, not a relative URL. Also I see you're then retrieving that URL via wp_remote_get, but your question says you're sending it to an API. Additionally, example.com/?p=123 is still a URL for the post, it's just not a pretty one that's been rewritten, but it still works. Infact, internally, WP will match the URL on to a rule that transforms it from example.com/url into example/com/?p=123 etc. Some CPT's have rewrite rules turned off too. I also see the hook for on_post_updated is missing, can we see the add_action call? – Tom J Nowell Sep 25 '18 at 13:20
  • I corrected the code. Forgot to write – Marat Petrov Sep 25 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.