I have developed a WordPress plugin which needs to perform additional processing when a post of a custom post type is saved.
The most logical solution was to utilise the "save_post" action. Upon being invoked the action handler either creates or updates a database record in a custom table (depending on whether "Create Post" or "Update Post" is clicked).
I have implemented this and it seemed to be working perfectly, but there is a minor issue which I would like to resolve. It seems that "save_post" is also triggered when loading the "Create Post" page for the first time (i.e. before any user inputs are entered and before the submit new/changes button is pressed).
This means that the custom database table is getting filled with one blank row for each new post that is saved. It also means that there is one blank row for each time the add post page is loaded.
Here is a simplified version of my "save_post" handler:
function do_save_post($post_id) {
if (get_post_type($post_id) !== 'mycustomtype')
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if (!current_user_can('edit_mycustomtype'))
return $post_id;
echo 'This happens when selecting "Add New" from admin sidebar.';
echo 'Even though post has not been saved or updated.';
echo 'This should only happen when button on right of editor is clicked.';
}
How can I detect whether the post is actually being saved?
$post_id
. My hook is not failing, it just wants to control when to undertake processing. The$post_id
is passed from each hook to each hook by return. – Lea Hayes