skip to Main Content

I am working on a new plugin. I am dealing with a problem that I have outlined in the title. My intention is to redirect the user to the My Pages page after the user clicks the "Delete page" button.

Here is my code:

function custom_admin_bar_delete_link( $wp_admin_bar ) {
    global $post;
    if( is_admin() || ! is_object( $post ) )
        return;
    if ( ! current_user_can( 'delete_pages' ) )
        return;
    if ( $post->post_type != 'page' )
        return;
    $args = array(
        'id'    => 'delete_link',
        'title' => 'Delete this page',
        'href'  => get_delete_post_link( $post->ID ),
        'meta'  => array( 'class' => 'delete-link' )
    );
    $wp_admin_bar->add_node( $args );
}
add_action( 'admin_bar_menu', 'custom_admin_bar_delete_link', 999 );

function custom_page_delete_redirect( $location, $post_id ) {
    $post = get_post( $post_id );
    if ( 'page' === $post->post_type && 'trash' === get_post_status( $post_id ) ) {
        return admin_url( 'edit.php?post_type=page' );
    }
    return $location;
}
add_filter( 'wp_redirect', 'custom_page_delete_redirect', 10, 2 );

Thank you.

2

Answers


  1. Without thinking too hard about it, I would add an ID to your button and just write some jQuery to do the redirect.
    (The PHP: NOT tested)

    EDIT:
    Per the WordPress documentation, you should be able to add an ID:
    https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/

    $args = array(
    'id'    => 'delete_link',
    'title' => 'Delete this page',
    'href'  => get_delete_post_link( $post->ID ),
    'id'  => "someid",
    'meta'  => array( 'class' => 'delete-link' )
    

    );

    The jQuery:

    EDIT:
    How are you adding the jQuery? You should probably save it in a separate file in your plugin folder (maybe in a subfolder named "js") and enqueue it in your plugin. The 2nd answer to this question would be a good place to start:
    https://wordpress.stackexchange.com/questions/42641/how-to-include-a-simple-jquery-file-into-a-wordpress-plugin

    jQuery(document).ready(function($){
        $('body').on('click','#someid',function(event){
            // window.alert('jquery executing');
            setTimeout(function(){window.location.href = "http://example.com/page/";}, 500);
        });
    });
    

    Thoughts: I added a timeout, because WordPress loads slowly sometimes, and I’ve had to do this for other applications in WordPress, but try without and see if it performs without the timeout.

    Login or Signup to reply.
  2. Have a look at this answer:
    https://wordpress.stackexchange.com/questions/132196/get-delete-post-link-redirect

    The question is similar, and this may get you where you need to go.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search