Viewing 4 replies - 1 through 4 (of 4 total)
  • C W (VYSO)

    (@cyril-washbrook)

    First, it’s not necessary to piece together the deletion URL as the tutorial does, when there’s already a built-in function: get_delete_post_link() (function reference). You could use it as follows:

    <?php
    if( is_user_logged_in() ) :
    $delete_link = get_delete_post_link(); ?>
    	<a href="<?php echo $delete_link; ?>">Delete post</a>
    <?php endif; ?>

    Then, use add_action() in your theme’s functions.php file to produce the redirection:

    add_action( 'trashed_post', 'trashed_post_redirect' );
    function trashed_post_redirect ($post_id) {
    	wp_redirect( home_url() );
    	exit;
    }

    This will redirect you to the home URL: you can change that if you wish.

    EDIT: Actually, just realised that adding this action will affect deletion on the backend as well. I have to dash, but I’ll think about it some more (or perhaps someone else will jump in with their thoughts).

    C W (VYSO)

    (@cyril-washbrook)

    Okay, modify the above slightly. In the relevant template:

    <?php
    if( is_user_logged_in() ) :
    $delete_link = add_query_arg( 'notadmin', 'true', get_delete_post_link() ); ?>
    	<a href="<?php echo $delete_link; ?>">Delete post</a>
    <?php endif; ?>

    In functions.php:

    add_action( 'trashed_post', 'trashed_post_redirect' );
    function trashed_post_redirect($post_id) {
    if ( filter_input( INPUT_GET, 'notadmin', FILTER_VALIDATE_BOOLEAN ) ) :
    	wp_redirect( home_url() );
    	exit;
    endif;
    }

    As stated above, this snippet will redirect you to the home URL.

    Thread Starter UFGrayMatter

    (@ufgraymatter)

    So – this works awesome! Thank you.

    I would like to limit this to one specific category. Can you show what I need to do add for that?

    C W (VYSO)

    (@cyril-washbrook)

    If you mean that you only want a delete post link to appear on posts which are in a certain category, then you would modify the if statement in the first snippet to something like this:

    if( is_user_logged_in() && in_category( '[some category]' ) ) :

    Replace [some category] with either (1) the name of the category or (2) the slug of the category or (3) the ID of the category.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Problem with Redirect’ is closed to new replies.