Search code examples
phpwordpresswordpress-plugin-creation

how to redirect non logged users to login page and redirect to another page with saved data?


im developing a page and using this free simple Favorite Post plugin to add favorites to users. currently its working for logged in users only. i need to show it to non logged users and when click on its button redirect to login or register page and after logged in redirect to same page or custom page.

this is for show favorite button code on front end : or short code :[favorite-post-btn]

this is for short for code display favorited posts :[favorite-post]

if anyone can help me big it would me grateful..! thank you i will put the button code here :

   /**
 * Favorite post link button
 *
 * @param int $post_id
 * @return void
 */
function link_button( $post_id ) {

    if ( !is_user_logged_in() ) {
        return;
    }

    $status = $this->get_post_status( $post_id, get_current_user_id() );
    ?>

    <a class="wpf-favorite-link" href="#" data-id="<?php echo $post_id; ?>">
        <?php if ( $status ) { ?>
            <span class="wpf-favorite">&nbsp;</span> <?php _e( 'Remove from favorite', 'wfp' ); ?>
        <?php } else { ?>
            <span class="wpf-not-favorite">&nbsp;</span> <?php _e( 'Add to favorite', 'wfp' ); ?>
        <?php } ?>
    </a>

    <?php
}

 

Solution

  • You are looking for https://developer.wordpress.org/reference/functions/wp_redirect/

    Checking: Redirect after Login on WordPress

    This has been answered here: https://stackoverflow.com/a/65380999/5214689