WordPress.org

Ready to get started?Download WordPress

Forums

WP Modal Login
[resolved] How to forward to certain page after login? (14 posts)

  1. mazrobby
    Member
    Posted 1 year ago #

    After login, it back to the same page. How do I forward it to another page after login successful?

    http://wordpress.org/extend/plugins/wp-modal-login/

  2. Cole Geissinger
    Member
    Plugin Author

    Posted 1 year ago #

    Currently right now the plugin doesn't support for login redirects but is on the list of future releases.

  3. Cole Geissinger
    Member
    Plugin Author

    Posted 1 year ago #

    Hi mazrobby, with the latest release I integrated a filter to allow people to change the login redirect. In a future version I'll be adding an area in the admin to adjust this.

    For now you can use create a function and add it to your functions.php in your theme. For demo purposes we'll call it my_awesome_redirect(). We'll pass this function a variable which will be the current URL set by default and inside of the function we'll over-write that URL with our own. Lastly, we'll hook it through add_filter( 'wpml_redirect_to', 'my_awesome_redirect' );

    here's some code to work with:

    function my_awesome_redirect( $redirect ) {
       $redirect = 'http://www.google.com';
    
       return $redirect;
    }
    add_filter( 'wpml_redirect_to', 'my_awesome_redirect' );
  4. therandomguy12345
    Member
    Posted 1 year ago #

    @Brainfestation
    @Cole Geissinger

    If I wanted to do something like this, I would add it to my functions.php or bp-custom.php, right? Maybe, in a future update, you should have a section in the admin area for the plugin called something like "Custom Code" and you can just add any code to be used along with the plugin there.

    Also, I have called the Modal Login through PHP using the below code and have left the $logout_url parameter at the default blank value:

    add_modal_login_button( $login_text = 'Login', $logout_text = 'Logout', $logout_url = '', $show_admin = true )

    I believe that the blank value in the $logout_url parameter redirects to the homepage (mydomain.com/), but I'm not certain.
    Will my_awesome_redirect() override the blank value in the $logout_url parameter?

    If not, can you provide instructions to setup a custom redirect — such as to redirect to BuddyPress profile after log-in — when you have called the Modal Login via PHP?

    Respond as soon as possible. Thanks in advance.

  5. therandomguy12345
    Member
    Posted 1 year ago #

    I am going to start a new thread since this one was already marked as resolved.

  6. Cole Geissinger
    Member
    Plugin Author

    Posted 1 year ago #

    The next major version is packed with more options like this and better support for custom login/user profile plugins like buddypress and some others.

    There will also be more options via the admin area for users to customize the text, redirects and other stuff. Just need to find the time to build all of this out :)

  7. therandomguy12345
    Member
    Posted 1 year ago #

    @Brainfestation
    @Cole Geissinger

    I downloaded and inspected the BuddyDev BP Redirect to Profile plugin myself. Then, I got it customized by Brajesh Singh from BuddyDev to make all users be redirected to their profile page (no matter what user role they are, or where they login from).

    You can view the code using the link below (no download + better view than default WordPress code tags):
    https://www.dropbox.com/s/rxn8n07h7p7brnp/viewcode.php

    I talked to WPMU Dev staff member and he is trying to work with me on this issue, but having lots of trouble. He asked me to try the below code in functions.php, but it didn't work:

    function custom_bpdev_redirect_to_profile( $redirect_to_calculated, $redirect_url_specified, $user ) {
    	return bp_core_get_user_domain( $user->ID );
    }
    add_filter( 'wpml_redirect_to', 'custom_bpdev_redirect_to_profile', 100, 3 );

    Not sure what's wrong with it. Do you think you could take the BuddyDev redirect code that I mentioned earlier with the dropbox link and post the code that I would need to put in functions.php to make WP Modal Login plugin follow the redirect?

    Please respond as soon as possible. All help is appreciated. Thanks in advance!

    NOTE: I am not using the BP Redirect to Profile plugin on my site anymore, because this is all the plugin is (one PHP file with the very short code from dropbox).

    BTW: the next version sounds awesome!

  8. Cole Geissinger
    Member
    Plugin Author

    Posted 1 year ago #

    So this is more complicated than how this plugin is built. Something I'm now realizing will require some restructuring to effectively make this happen. Here's what I have been able to uncover and possibly WPMU Dev can uncover some angles I'm missing.

    Over all, the issue is the redirect is set via javascript, and the proper redirect to the user profile won't be available until AFTER login occurs. So using the filter I created is set too early (on page load). Here's a snippet I put together that works... but only behind the scenes and not as we want it to.

    function my_awesome_redirect( $redirect ) {
    	$user = get_user_by( 'login', sanitize_text_field( $_REQUEST['username'] ) );
    
       wp_redirect( bp_core_get_user_domain( absint( $user->ID ) ) );
    }
    add_action('wp_login', 'my_awesome_redirect', 10, 2);

    Here's a break down. We create a function to run when the wp_signon() function is used (this is the function used for user login stuff). This is a hook created by WordPress and is reused in the WP Modal Login plugin. Inside that function we will get the username entered into the form so we can return the logged in users ID. In turn we use that ID with BuddyPress' bp_core_get_user_domain() function which will return the proper URL for us to redirect to. We then use wp_redirect() to try and redirect the page.

    This works.. but not visibly. If we open Chrome Dev tools in Google Chrome, and open the Network tab, when we hit the login button, the ajax call executes and then a redirect happens, however, in the front end all it says is "Checking credentials..." and no redirect as if it is broken. Here's a screenshot. The bottom two results is the ajax call and then the last is the redirect to /members/admin/ http://cl.ly/image/1R061a042h39

    I know this doesn't answer your question though.. and sadly, I don't have a clear one but that your needs are a little beyond this plugin at the moment without some tweaking to the code.

    Here's a hack that should work though. Since the issue is we need to dynamically get a URL that isn't possible without being logged in, we can setup a little hack to get that info after login then perform a second redirect.

    Use this code to point to the members page of buddypress.

    function trg12345_custom_login_redirect( $redirect ) {
    
    	// Set up a static URL for a secondary redirect
       $redirect = esc_url( home_url( '/members?wpml_login=true' ) );
    
       return $redirect;
    }
    add_filter( 'wpml_redirect_to', 'trg12345_custom_login_redirect' );

    What we are doing is redirecting to a non-dynamic page and appending a "query string" to the end of the URL. We can then create a second function that will listen for this exact URL and then run one more redirect to the right user profile page. This should only run right after the user logs in. Place this second function after the function above in your functions.php (FYI, these can be added anywhere in that file).

    function trg12345_finial_redirect() {
    	if ( isset( $_GET['wpml_login'] ) && ! is_admin() ) {
    		// Get the current user ID
    		$user = get_user_by( 'id', get_current_user_id() );
    
    		// Redirect our to our user profile
    	   echo '<script>window.location = "' . bp_core_get_user_domain( absint( $user->ID ) ) . '";</script>';
    	}
    }
    add_action( 'wp_head', 'trg12345_finial_redirect' );

    This last function will listen for the wpml_login query variable and that we are not in the admin area of WordPress. We'll then get the current logged in users ID and send that to the BuddyPress user domain fnction. We'll also make sure everything is sanitized to make sure everything is secure too.

    It's a little hacky, but your users shouldn't see much of a difference? Maybe a little flicker. Here's the full script, just copy all of this and paste it into the bottom of your functions.php and it should just work... if your members page lives under a different URL than 'members', make sure you update that part of the script. I couldn't find the BuddyPress function that would spit that out for us dynamically.

    /**
     * Setup a redirect to the main members page with a query string for us to hook onto when logged in
     * @param  string $redirect The URL to redirect to after logging in
     * @return array
     */
    function trg12345_custom_login_redirect( $redirect ) {
    
    	// Set up a static URL for a secondary redirect
       $redirect = esc_url( home_url( '/members?wpml_login=true' ) );
    
       return $redirect;
    }
    add_filter( 'wpml_redirect_to', 'trg12345_custom_login_redirect' );
    
    /**
     * The final redirect which will take us directly to the user profile.
     * This is done via JavaScript.
     * @return string
     */
    function trg12345_finial_redirect() {
    	if ( isset( $_GET['wpml_login'] ) && ! is_admin() ) {
    		// Get the current user ID
    		$user = get_user_by( 'id', get_current_user_id() );
    
    		// Redirect our to our user profile
    	   echo '<script>window.location = "' . bp_core_get_user_domain( absint( $user->ID ) ) . '";</script>';
    	}
    }
    add_action( 'wp_head', 'trg12345_finial_redirect' );
  9. therandomguy12345
    Member
    Posted 1 year ago #

    @Brainfestation
    @Cole Geissinger

    Thanks so much for the code snippet!

    I'll try this out after one more response from you. You said:

    It's a little hacky... Maybe a little flicker

    Are there any downsides of this method? If I use it for sometime and then remove it from functions.php later, will I face any problems — will there be any leftovers?

    Anyways, respond as soon as possible. Thanks in advance.

  10. therandomguy12345
    Member
    Posted 1 year ago #

    @Brainfestation
    @Cole Geissinger

    Your "hacky" code worked beautifully! Thanks so much, but do respond to my last question:

    Are there any downsides of this method? If I use it for sometime and then remove it from functions.php later, will I face any problems — will there be any leftovers?

    Also, when/if there is a future update to the plugin for a better solution for these kind of issues, would you please post it on this thread (and mention me) so I'll get an email and follow your new solution.

    Anyways, it's really appreciated.

  11. Cole Geissinger
    Member
    Plugin Author

    Posted 1 year ago #

    Its only hacky because we use two redirects instead of one. This shouldn't have any downsides just its not the preferred method.

    I'll try to remember to list the update here. This new solution will be version 2.1. You can just watch out for that version in the future.

  12. therandomguy12345
    Member
    Posted 1 year ago #

    Thank you so much!

  13. silverhema
    Member
    Posted 10 months ago #

    Hi Cole,

    I just have a simple site wanting to do a login redirect. I tried the snippet you gave earlier on

    function my_awesome_redirect( $redirect ) {
       $redirect = 'http://www.google.com';
    
       return $redirect;
    }
    add_filter( 'wpml_redirect_to', 'my_awesome_redirect' );

    and got it to work for all pages except for the home page (which is a Page, not a blog), any ideas why?

  14. therandomguy12345
    Member
    Posted 8 months ago #

    @silverhema
    Did you get this to work?
    If not, I suggest checking your site for any other redirect plugins first for conflicts. Deactivating and testing...
    Otherwise, that code snippet you provided seems like it should do the trick

    @Brainfestation
    @Cole Geissinger

    function trg12345_custom_login_redirect( $redirect ) {
    
    	// Set up a static URL for a secondary redirect
       $redirect = esc_url( home_url( '/members?wpml_login=true' ) );
    
       return $redirect;
    }
    add_filter( 'wpml_redirect_to', 'trg12345_custom_login_redirect' );

    In the first part of the code snippet you provided (included in above line), I'd like to change the "?wpml_login=true" end of the URL to something else... Would I just tweak the below line?
    $redirect = esc_url( home_url( '/members?wpml_login=true' ) );
    to
    $redirect = esc_url( home_url( '/members?profile_redirect=true' ) );
    or simply
    $redirect = esc_url( home_url( '/members?profile_redirect' ) );

    Respond when you can. Thanks in advance!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.