• I have a WordPress site setup with the WP-Members plugin. I have used the filter hook before but I want to change it a little so it redirects people to this page only if they log in from the homepage.

    I think I am almost there but it is not working.. Here is what I have. Any help much appreciated.

    p.s. great plugin thanks!

    add_filter( 'wpmem_login_redirect', 'my_login_redirect' );
    
    function my_login_redirect( $redirect_to, $user_id ) {
    
    if ( is_front_page() ) {
    
      return '/dashboard/';
    }
    
    }

    https://www.ads-software.com/plugins/wp-members/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Chad Butler

    (@cbutlerjr)

    The filter as you have it will fail because you are asking for 2 arguments ($redirect_to and $user_id), but passing only 1 (the add_filter lacks the fourth possible argument that defines the number of arguments to pass, the default of which is 1).

    So you either need this:

    add_filter( 'wpmem_login_redirect', 'my_login_redirect', 10, 2 );
    function my_login_redirect( $redirect_to, $user_id ) {
    
    	// If it is the front page.
    	if ( is_front_page() ) {
    		return '/dashboard/';
    	}
    
    	// If it is anything else.
    	return $redirect_to;
    }

    Or this:

    add_filter( 'wpmem_login_redirect', 'my_login_redirect' );
    function my_login_redirect( $redirect_to ) {
    
    	// If it is the front page.
    	if ( is_front_page() ) {
    		return '/dashboard/';
    	}
    
    	// If it is anything else.
    	return $redirect_to;
    }

    Note, the difference in the optional arguments. In the first, you are passing all possible arguments; in the second, just the required arguments ($user_id is optional and you are not using it).

    Also, the other problem is that you were only returning a value if it was the front page in which case you returned the dashboard page. However, when using filters you still need to return item being filtered (in this case $redirect_to) even if you are not changing it. Otherwise no value is returned.

    Thread Starter caffeinehigh

    (@zaphan58)

    Thank you for your reply Chad, that has worked a treat. I didn’t realise the $redirect_to variable could be called that way for a default value, thats great ??

    Plugin Author Chad Butler

    (@cbutlerjr)

    Glad you got it working.

    I didn’t realise the $redirect_to variable could be called that way for a default value.

    (Almost) anytime you use a filter you need to make sure that you return the item being filtered. Think of it this way – filter hooks (in WP itself or a plugin or theme) pass a value through a filter. They may also pass additional values, but that first value is the item being filtered. (The value being filtered is always the first argument for the function if there is more then one.) You must always return that value whether you change it with your filter or not because main code where the filter hook was initiated needs to use that value. The filter is just allowing you an opportunity to change the value before it gets used.

    Notice I said “almost.” The exception to this is that sometimes a filter hook will be there to get values that may be merged with a set of defaults and if no value comes back from the filter, then the default is used. But when in doubt, return the filtered argument so you don’t end up with a null value being returned unintentionally.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirecting if logging in from the home page’ is closed to new replies.