0

in my WP MultiUser project i am designing a template for deleting a page from the front-end (only the super admin can access the back-end admin panel)

I have almost achieved the desired result, but when I press "Delete," the function deletes all pages. I just want to delete only the selected page. Here is the code that I created:

<?php  $pages = get_pages(); 
  foreach ( $pages as $page ) {
 $option = '<li>';
$option .= '<a href="' . wp_delete_post( $page->ID ) . '">';
$option .= $page->post_title;
$option .= '</a>';
$option .= '</li>';
echo $option; }   ?>   

edit 2 Sorry i miss few changes

     <?php   $pages = get_pages();foreach ( $pages as $page ) {
 $option = '<li>';
$option .= $page->post_title;
// $option .= '<a href="' . wp_delete_post( $page->ID ) . '">';
$option .= 'Delete</a>';
$option .= '</li>';
echo $option;  }   ?>   
  • How is the User supposed to select the ID of the page they want deleted? Your loop needs to display all pages, let the user select the page to delete and then delete the page. See Answer – eyoung100 Sep 25 '14 at 15:49
  • @eyoung100 code updated – sunil Sep 25 '14 at 15:53
  • Diogo beat me to it. I was gong to use list_pages to list every page in the same way he built $delLink, which brings up a great point. Just because only the Super Admin can use the backend, shouldn't mean that you are precluded from using it. Notice how Diogo used an admin page to build the link. – eyoung100 Sep 25 '14 at 16:00
3

You are deleting all pages with that code because you are triggering the function wp_delete_post().

You should build the url to delete the page(or post) like this:

$delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=delete&post=" . $page->ID, 'delete-post_' . $page->ID);

So your code should be:

$pages = get_pages(); 
foreach ( $pages as $page ) {
    $delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=delete&post=" . $page->ID, 'delete-post_' . $page->ID);
    $option = '<li>';
    $option .= '<a href="' . $delLink . '">';
    $option .= $page->post_title;
    $option .= '</a>';
    $option .= '</li>';
    echo $option;
}

Cheers

  • exactly. I'd just replaced the custom building of the delete link by get_delete_post_link – david.binda Sep 25 '14 at 16:02
  • HI @diogogomes as mentioned earlier the above code works perfectly now i am looking to drag and drop rearrange code for pages just like wp menu page. dose any body have idea to call wordpress inbuilt drag and drop to work with this or any other option. – sunil Nov 3 '14 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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