how to redirect page after delete post inside a post page?

admin2025-01-08  4

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

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

Share Improve this question asked Jan 6, 2017 at 4:45 Hend RyHend Ry 291 silver badge13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270382a1424.html

最新回复(0)