I wonder why this is happening or what I am doing wrong here
WordPress has a revisions system which stores a record of each saved draft or published update, and a post can have one of a number of statuses like auto-draft
when a new post is created and saved automatically in the background, draft
(e.g. when the post is autosaved) or publish
(e.g. when the publish button is clicked). So the save_post
hook (and other hooks like wp_after_insert_post
) can run multiple times while editing a post, and thus you should check whether WordPress is doing an autosave and whether the post being saved is a revision, before running your remote HTTP request.
Here’s an example which also checks whether WordPress is doing cron (/wp-cron.php
) or AJAX (wp-admin/admin-ajax.php
), and whether the post status is publish
or private
:
function update_front_end( $post_id, $post ) {
$doing_autosave = ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE );
if ( $doing_autosave || wp_is_post_revision( $post_id ) ||
wp_doing_cron() || wp_doing_ajax()
) {
return;
}
if ( in_array( $post->post_status, array( 'publish', 'private' ) ) ) {
// your code here; define the $url, etc.
// now run your remote HTTP request
wp_remote_post( $url );
}
}