get current_user_id in a filter in functions.php?

76 views
Skip to first unread message

barbara schendel

unread,
Jun 10, 2013, 12:08:24 PM6/10/13
to mpls-stpau...@googlegroups.com
Hey folks,

I am trying to write a function that redirects users to their own page upon login. There is an action for wp-members that does this. It works fine for a static url but my problem is that I can't seem to grab the current user's ID from within functions.php at the time that this filter runs.

I have simplified what I'm trying to do below, in order to pare this down to just the issue at hand.  

add_filter( 'wpmem_login_redirect', 'my_login_redirect' ); //this filter is part of the wp-members plugin
function my_login_redirect() 
{
$myuserid = get_current_user_id();
        // this is ideally supposed to return the url that the login should redirect to
        // it redirects okay but it inserts a 0 at the end instead of the user id.
}


Here are some others I have already tried -- 

    wp_get_current_user(); then $current_user->ID  //returns 0
    global $user_ID;  // also returns 0

Could this filter be running somehow before the user is officially logged in? Not sure how to find out.
Is there a native wordpress hook that I can use instead?

Appreciate any help! Thanks!
-barbara-

Nicholas Ciske

unread,
Jun 10, 2013, 1:22:20 PM6/10/13
to mpls-stpau...@googlegroups.com
template_redirect should work. 

Sent from my iPhone 5
--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
To post to this group, send email to mpls-stpau...@googlegroups.com.
Visit this group at http://groups.google.com/group/mpls-stpaul-wordpress?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tom Penney

unread,
Jun 10, 2013, 1:51:36 PM6/10/13
to mpls-stpau...@googlegroups.com
if get_current_user_id() returns 0 then the user is not yet logged
in. I don't know what is providing that wpmem_login_redirect hook but
it isn't native wordpress. try using the login_redirect hook instead.
--
Tom Penney
952-200-3363

barbara schendel

unread,
Jun 10, 2013, 4:00:31 PM6/10/13
to mpls-stpau...@googlegroups.com
Hi Nick, this needs to fire when a user first logs in, I can't see how
to use template_redirect in that fashion. ?

Tom, thanks for the idea, but that doesn't seem to do anything at all.
I played around with it as per
http://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
and some other simpler examples on stackoverflow but no matter how I
write it, it just goes to the default page that wp-members makes it go
to.

Is it possible for a plugin to hijack a filter and use its own values?
I think that must be what's happening. The only way I can get any
effect is to use their filter wpmem_login_redirect, but I cannot seem
to access the current user info with that.

Any other ideas?

Nicholas Ciske

unread,
Jun 11, 2013, 10:45:32 AM6/11/13
to mpls-stpau...@googlegroups.com
wpmem_login_redirect

It's not hijacking.

You can't remove a filter, but you can unhook the the default function hooked to that filter.

You can also add_filter with a higher priority (lower number) than the default (which is 10).

e.g.

add_filter('hook','default',10);

add_filter('hook','custom',5);

custom will run before default, and it it redirects and exits execution... default will never run.

Best way to determine what's happening is to look at the WP Members code and see how they are hooking in so you can determine if you need to unhook their function, or just run yours at a higher priority.

_________________________
Nick Ciske
Web Design & Development | Business Consulting 
@nciske

Reply all
Reply to author
Forward
0 new messages