WordPress.org

Support

Support » Plugins and Hacks » Delete Post Link

Delete Post Link

  • This function which adds a Delete link to the admin should work but it doesn’t.

    function delete_post_link()
    {
    	global $post;
    	echo "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?post%3D" . $post->ID . "&action%3Ddelete&", 'delete-post_' . $post->ID) . "'>Delete</a>";
    }

    I get this:
    Are you sure you want to do this?
    Please try again.

    I think it has to do with wp_nonce_url. Any ideas?

    Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • kristarella
    Member

    @kristarella

    I second this question.

    I’ve tried
    href="' . get_bloginfo("url") . '/wp-admin/post.php?post=' . $post->ID . '&action=trash"
    and
    href="' . wp_nonce_url( get_bloginfo("url") . "/wp-admin/post.php?post=" . $post->ID . "&action=trash") . '"
    Both of which bring me to the same failure notice page with a link back to the page I was just on saying “Please try again”.

    The link structure of action=trash is slightly different to acondiff, above; I changed mine to replicate the link structure in the WP admin.

    adanahmad
    Member

    @adanahmad

    using this at functions.php

    function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
        global $post;
        if ( $post->post_type == 'page' ) {
            if ( !current_user_can( 'edit_page', $post->ID ) )
                return;
        } else {
            if ( !current_user_can( 'edit_post', $post->ID ) )
                return;
        }
        $message = "Are your sure you want to delete ".get_the_title($post->ID)." ?";
        $delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID);
        $link = "<a href='" . $delLink . "' title="Delete" />".$link."";
        echo $before . $link . $after;
    }

    and calling with this

    <?php wp_delete_post_link('Delete this', '<p>', '</p>'); ?>

    I tried this ^

    When I press the link I get a pop up asking me to confirm, I then press “Ok”. Then I get a “WordPress Failure Notice”, just a blank page with the message: “Are you sure you want to do this?” and a link beneath that says: “Please try again.” which leads back to the list post view.

    Any ideas how to fix this?

    kristarella
    Member

    @kristarella

    wob – I get the same message, don’t know how to fix it 🙁

    haveboard
    Member

    @haveboard

    Same here. wp_delete_post_link() doesn’t seem to work.

    “Are you sure you want to do this?

    Please try again.”

    No luck here.

    So the problem is with nonce..WP is checking for the nonce with param name = _wpnonce and pre-hash value to be in this format (without brackets of course ) [ ‘trash-‘ . $post->post_type . ‘_’ . $post->ID ]
    change this line of code in function provided above by adanaahmad

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

    to {Changed nonce and action=trash}

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

    So your function becomes ..

    function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
        global $post;
        if ( $post->post_type == 'page' ) {
            if ( !current_user_can( 'edit_page', $post->ID ) )
                return;
        } else {
            if ( !current_user_can( 'edit_post', $post->ID ) )
                return;
        }
        $message = "Are your sure you want to delete ".get_the_title($post->ID)." ?";
        $delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);
        $link = "<a href='" . $delLink . "' title="Delete" />".$link."";
        echo $before . $link . $after;
    }

    Heres a better and ‘tested’ version of the function..

    function wp_delete_post_link($link = 'Delete This', $before = '', $after = '', $title="Move this item to the Trash") {
        global $post;
        if ( $post->post_type == 'page' ) {
            if ( !current_user_can( 'edit_page' ) )
                return;
        } else {
            if ( !current_user_can( 'edit_post' ) )
                return;
        }
        $delLink = wp_nonce_url( site_url() . "/wp-admin/post.php?action=trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);
        $link = '<a href="' . $delLink . '" onclick="javascript:if(!confirm(\'Are you sure you want to move this item to trash?\')) return false;" title="'.$title.'" />'.$link."</a>";
        return $before . $link . $after;
    }

    Im having trouble with both these functions. When using an admin user, no problem. But when i try to delete using an author user (a post that user created), y receive a wordpress ‘error’ message

    Try changing edit_page and edit_post to edit_pages and edit_posts (plural)

    I checked wordpress documentation again just now and those are the correct capabilities. My GUESS is that function current_user_can returns true for admin without checking capability since admin has access to everything. SO give that a try.

    If that does not work, try changing the capability to role name.. so change this

    if ( $post->post_type == 'page' ) {
            if ( !current_user_can( 'edit_page' ) )
                return;
        } else {
            if ( !current_user_can( 'edit_post' ) )
                return;
        }

    TO

    if ( !current_user_can( 'administrator' ) && !current_user_can( 'editor' )){
                return;
              }

    Hope this helps..

    ok, i was able to solve this. It had nothing to do with the code here, but i had disabled user from wp-admin and thus they couldn’t execute this link. Thanks anyway.

    By the way, I wasn’t able to implement the alert messages before deleting.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Delete Post Link’ is closed to new replies.