0

I am allowing Authors to delete their posts on the front end as they cant access the admin. I have this working, but what bothers me is the standard javascript onclick alert message - is there are way to style a js alert? From my understanding, no. Or to use jQuery for the message and use a modal window that I can style?

Also, after deletion the user is returned to the same page which is good, but there is no confirmation message - I'd like to have a message saying "Post successfully deleted". I just think these touches would provide a better user experience. Here's the code I am using:

function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
global $post;
if ( $post->post_type == 'post' ) {
if ( !current_user_can( 'edit_page', $post->ID ) )
  return;
} else {
if ( !current_user_can( 'edit_post', $post->ID ) )
  return;
}
$message = "Are you sure you want to delete ".get_the_title($post->ID)." ?";
$delLink = wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID);
$htmllink = "<a href='" . $delLink . "' onclick = \"if ( confirm('".$message."' ) ) { execute(); return true; } return false;\"/>".$link."</a>";
echo $before . $htmllink . $after;
$redirect = add_query_arg( 'success', 'true', $redirect );
}

<?php wp_delete_post_link('Delete your Entry', '<p><em>Delete your Entry: </em>', '</p>'); ?>

And was added to the author.php where the post delete button is

 <?php if ( ! empty( $_GET['success'] ) ) {
                            echo 'Wahey!';
                            }
                        ?>  
1
  • PS: the deleting is done on the front end by a user with the role of Author and they do not have access to wp-admin. Commented Oct 6, 2015 at 12:16

1 Answer 1

0

There are two questions here. For styling alerts/modals, that's a generic HTML/CSS question - check out these to get you started.

For the second, it depends what you're doing after the delete - are you redirecting back to the frontend, or staying in the admin? (I will update my answer following your response).

You shouldn't be reinventing the wheel - use the core function get_delete_post_link to get the delete URL. This will handle nonces and any future changes to core, if and when they occur.

Update: For your redirect, add a param that you can then check for on page load:

$redirect = add_query_arg( 'success', 'true', $redirect );

And then in your template file:

if ( ! empty( $_GET['success'] ) ) {
    echo 'Wahey!';
}
5
  • Ok so I dont have to use the standard js alert - I can use jquery for example to do this? For the second part I am just redirecting back to the same page and would like to show a confirmation message. Commented Oct 6, 2015 at 11:58
  • When you redirect back, add a query argument - see revision. Commented Oct 6, 2015 at 12:55
  • I added the redirect - see updated code above and added the if ( ! empty statement in my template at the top but nothing happens after deletion. The page reloads but nothing is there - maybe that's the issue as the page is reloading. Commented Oct 6, 2015 at 13:06
  • Update your question with the code you're using to redirect. Commented Oct 6, 2015 at 15:12
  • Ive added the last piece of your code above. This is in author.php Commented Oct 8, 2015 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.