0

My goal is to allow users to delete posts they own from the front end. I have an API that returns some JSON along with a link to delete their post.

$delLink = wp_nonce_url( site_url() . "/wp-admin/post.php?action=trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);

This does return a url with a nonce

https://example.com/wp-admin/post.php?post=338&action=trash&_wpnonce=983403d085

If I click this link it brings me to the 'posts' section of my admin panel without deleting the custom post in question.

But if I compare that to the link in my admin panel, the URL should be

https://example.com/wp-admin/post.php?post=338&action=trash&_wpnonce=7337993b16

What am I doing wrong here?

  • try this : <?php if (current_user_can('edit_post', $post->ID)) echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "'>Delete post</a>" ?> – vikrant zilpe Dec 22 '18 at 10:54
  • Try: You can decide when to empty the WordPress trash by adding this code to the wp-config.php file in your WordPress root directory. define('EMPTY_TRASH_DAYS', 1 ); – vikrant zilpe Dec 22 '18 at 10:54
  • That worked. It seems like my problem was that I was returning the URL (and using that to set the href on the frontend) rather than the entire HTML link. I can't imagine why it would matter, but that seems to make the difference. – Vedgy Dec 22 '18 at 18:36

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.