I have some metadata on my post edit page that saves perfectly using the hook and function below:
add_action('save_post', 'save_meta_data');
function save_meta_data($post_id){
if(isset($_POST['target_url'])) {
update_post_meta($post_id, 'target_url', $_POST['target_url']);
}
};
However, the same logic fails to save the meta data before a post is trashed which is what that hook is for (so I thought).
add_action('wp_trash_post', 'save_meta_data');
function save_meta_data($post_id){
if(isset($_POST['target_url'])) {
update_post_meta($post_id, 'target_url', $_POST['target_url']);
}
};
It looks like in the second case that the whole $_POST array is empty, but I know the function is firing.
Could some kind soul point out what am I doing wrong and what is the proper way to do this?
Thanks, Ron
The Core “trash post” links in the “Quick Edit” section on
edit.php
and in the “advanced” form in the “Publish” meta box work overGET
notPOST
. Unless you have altered the form(s) somehow, there is noPOST
data. All you have is the post ID.To save data when a post is deleted, you won’t be able to use the default “delete/trash” functionality. You will need to:
description, then trash the post.
out the description or when you hit the “delete/trash” link.
form over
POST
that you can submit the form as normal and if the “delete/trash”
checkbox is checked a filter on
save_post
can do the actual delete.Off the top of my head, I can’t tell you how hard modifying the form will be, but it won’t likely be simple. The AJAX options are probably easier, but can easily be disabled by switching off Javascript in the browser.