404 on delete post link

I followed this article to add a "delete" link to posts in the front end, however, after clicking on it, a 404 error is shown.

  • Nahid

    Hey there Joshua !
    Hope you are having a great day!

    The code in the article that you followed doesn't have a redirect functionality to a different page. Could you please try adding the following code to your functions.php file instead?

    function wp_delete_post_link() {
        global $post;
    
        if ( current_user_can( 'edit_post', $post->ID ) ) {
            echo '<span><a class="post-delete-link" onclick="return confirm( \'Are you sure to delete?\' )" href=" ' . get_delete_post_link( get_the_ID(), '', true ) . ' ">Delete Post</a></span>';
        }
    }
    
    add_action( 'deleted_post', 'trash_redirection_frontend' );
    function trash_redirection_frontend( $post_id ) {
        	wp_redirect( home_url() );
        	exit;
    }

    The following code should go into the single.php file instead:

    <?php wp_delete_post_link(); ?>

    This code will redirect the privileged user to the homepage after the post deletion.

    Hope this helps. Please let us know if this works for you. Thanks!

    Kind regards,
    Nahid

  • Joshua

    When I use the code that is provided in the article which is:

    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;
    
    }
    
    $link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
    
    echo $before . $link . $after;
    }

    The "delete" link is successful at deleting the post. However, it leaves the user at a delete-page, which then reads a 404 error.

    The URL where the user is left is something like this: https://jesusteam.com/example-blog/?deleted=1

  • Dimitris

    Hello there Joshua,

    hope you're doing well and don't mind me chip in here! :slight_smile:

    I tested this is a local site of mine using the default TwentySeventeen theme and it was working good. Please remove previous snippets from the functions.php file of your child-theme and use the following instead:

    // Delete post
    function delete_post(){
      global $post;
      $deletepostlink= add_query_arg( 'frontend', 'true', get_delete_post_link( get_the_ID() ) );
      if (current_user_can('edit_post', $post->ID)) {
          echo       '<span><a class="post-delete-link" onclick="return confirm(\'Are you sure to delete this post?\')" href="'.$deletepostlink.'">DELETE</a></span>';
      }
    }
    
    //Redirect after delete post in frontend
    add_action('trashed_post','trash_redirection_frontend');
    function trash_redirection_frontend($post_id) {
      if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
          wp_redirect( get_option('siteurl') );
          exit;
      }
    }

    Then in the single.php file, you should add:
    echo delete_post();

    Warm regards,
    Dimitris

  • Dimitris

    Hey Joshua,

    I'm glad this worked for you! :slight_smile:

    You can replace the snippet once again, in order to redirect to the posts page:

    // Delete post
    function delete_post(){
      global $post;
      $deletepostlink= add_query_arg( 'frontend', 'true', get_delete_post_link( get_the_ID() ) );
      if (current_user_can('edit_post', $post->ID)) {
          echo '<span><a class="post-delete-link" onclick="return confirm(\'Are you sure to delete this post?\')" href="'.$deletepostlink.'">DELETE</a></span>';
      }
    }
    
    //Redirect after delete post in frontend
    add_action('trashed_post','trash_redirection_frontend');
    function trash_redirection_frontend($post_id) {
      if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
          wp_redirect( get_option('siteurl').'/members/blog' );
          exit;
      }
    }

    As for the styling of the link, you can do that via CSS. You can insert the following snippet using the built-in WP option found in Appearance -> Customize -> Additional CSS (if there are other CSS rules in there, append the following code in a new line on the very bottom):

    .post-delete-link {
      margin-left: 10px;
    }

    Thank you,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.