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
    • Tech Support

    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
    • WPMU DEV Initiate

    Hi Nahid!

    The code you provided is successful in redirecting the user, however the "delete" link no longer deletes the post.

    It just redirects the users, without actually deleting the post.

    The other code that I was using from the article DID delete the post, but it lead to a 404 error.

    I'm waiting for your advice.

    Thank you,
    Joshua

  • Joshua
    • WPMU DEV Initiate

    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

  • Joshua
    • WPMU DEV Initiate

    The code you provided successfully redirects the user to homepage, but it does not delete the post, unfortunately.

    Hoping you can provide me with a fix for this soon. Would really appreciate it.

    Thank you,
    Joshua

  • Dimitris
    • Support Star

    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

    • Joshua
      • WPMU DEV Initiate

      Hello Dimitris,

      Everything appears to be working perfectly! Thank you very much. Now I have two questions:

      1. How can I adjust the link styling? It needs to add margin-left 10px .

      2. How can I make it so that the redirection leads back to the Posts page instead of the Home page? The URL to the posts page is: https://jesusteam.com/members/blog/

      Thank you again! Been trying to figure this out for nearly a week.

      Cheers!
      Joshua

  • Dimitris
    • Support Star

    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.