0

so on theme single.php i want to put delete button so they can delete the post directly. After the post deleted then the page will redirect to profile page.

here's the delete button html

<a class="edit-post" onclick="redirectFunction();" href="<?php bp_loggedinuser_link(); ?>articles/"> Delete </a>

and this is the script for delete the post

function redirectFunction(){
    <?php wp_delete_post(get_the_id()); ?>
}

the problem is when i click other link that show on page, it also trigger this function to delete the post.

please help or let me know if there's any way better than this. thanks

2 Answers 2

0

There is a action hook in wp_delete_post() called after_delete_post. So hook a function in that hook and redirect your user to where ever you want. You can redirect to author's page by below code-

add_action( 'trash_post', 'the_dramatist_redirect_after_post_delete' );
function the_dramatist_redirect_after_post_delete() {
    if ( !is_admin() ) {
        wp_safe_redirect( get_author_posts_url( get_current_user_id() ) );
        exit();
    }
}

Hope that helps.

8
  • yes it works, but if i trash the post on wp-admin, it redirect also. What i want is redirect after delete inside an article page and didnt make change for wp-admin function delete.
    – Hend Ry
    Commented Jan 7, 2017 at 6:08
  • Test the updated code. Hope that will work. @HendRy
    – CodeMascot
    Commented Jan 8, 2017 at 5:51
  • still not working
    – Hend Ry
    Commented Jan 8, 2017 at 7:56
  • When it is not changing? Are you moving your post to trash when you delete the post from frontend?
    – CodeMascot
    Commented Jan 8, 2017 at 8:16
  • ya, the deleted post moved to trash
    – Hend Ry
    Commented Jan 8, 2017 at 9:06
0

Another way to handle ajax requests to delete a post is to use jquery and the built-in ajax functions provided by WP.

Html

<a class="delete-post" data-postid="<?php echo get_the_id(); ?>" href="#">Delete</a>




<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

<script>
    jQuery(document).ready(function($) {
    $('.delete-post').on('click', function(e) {
        e.preventDefault();

        var postId = $(this).data('postid');

        // Send an AJAX request to delete the post
        $.ajax({
            type: 'POST',
            url: ajax_object.ajaxurl,
            data: {
                action: 'delete_post',
                post_id: postId,
            },
            success: function(response) {
                // Redirect to the profile page after successful deletion
                window.location.href = '<?php echo bp_loggedinuser_link(); ?>articles/';
            },
            error: function(error) {
                console.log(error.responseText);
            }
        });
    });
    });
</script>

functions.php

Enqueue the script.

function enqueue_custom_scripts() {
    // Enqueue jQuery
    wp_enqueue_script('jquery');

    // Enqueue your custom script
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true);

    // Localize the script with the necessary data
    wp_localize_script('custom-script', 'ajax_object', array('ajaxurl' => admin_url('admin-ajax.php')));
}

add_action('wp_enqueue_scripts', 'enqueue_custom_scripts'); 



function delete_post_callback() {
    if (isset($_POST['action']) && $_POST['action'] == 'delete_post') {
        $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;

        // Check if the user has permission to delete the post
        if (current_user_can('delete_post', $post_id)) {
            wp_delete_post($post_id);
            echo 'Post deleted successfully';
        } else {
            echo 'Permission denied';
        }
    }

    die();
}

add_action('wp_ajax_delete_post', 'delete_post_callback');
add_action('wp_ajax_nopriv_delete_post', 'delete_post_callback'); // Allow non-logged in users to delete posts

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.