New Filter and Action Hooks in WP-Members 2.7.1

There are a number of fixes and features included in WP-Members 2.7.1 (view the list here), but I think the most important is the inclusion of some new action and filter hooks.  I’ve had a number of requests for certain redirects and changing certain texts, and these hooks will allow for that. 

Here is a list of the various hooks available:

  • wpmem_login_redirect
  • wpmem_logout_redirect
  • wpmem_register_redirect
  • wpmem_sidebar_status
  • wpmem_register_links
  • wpmem_member_links
  • wpmem_login_links

I’ll be putting together some more detailed documentation on each of these, but in the meantime, I have added information to the current Users Guide, and also I have put together a phps file with some documented examples.

Get Fresh Content Delivered

Join and get free content delivered automatically each time we publish.

Comments

  1. says

    I have used wp-members on my site and it blocked all admins from entering the site. Any solutions?
    I really liked this plugin until that happened!
    Thanks for the help.

  2. says

    Hi Chad,

    I added the redirect hook for login and works great if the member is not logged in when they come to the landing page. If when they login they check the box to be remembered and return to the site it still takes them to the landing page, I would like it to use the redirect still to take them to the articles page instead of the landpage since it has no real value once they are registered.

    Thanks DJ

    • says

      Hi DJ – I think I understand what you are asking. Basically, you have a landing page you are directing users to, they login there and are redirected to an articles page. However, if the user is already logged in, if they hit the landing page, they are not redirected.

      You could overcome this by using an action hook in your functions.php that would hook into some WP action before headers are sent so you could wp_redirect. If the page is the landing page, and the user is logged in, redirect them, otherwise do nothing. That would look something like this:

      add_action( 'wp_head', 'check_page' ); 
      function check_page()
      {
      	if( is_page( 'landing-page' ) && is_user_logged_in() ) {
      		wp_redirect( 'http:// mysite.com/my-page/' );
      		exit();
      	}
      	return;
      }

      In this example, I am hooking into the wp_head action, which builds the html head of the document. Headers are not sent at this point, so wp_redirect can be used. That action fires my function check_page. check_page uses the WP function is_page to determine if the page being requested is the landing page. You would set this parameter to be the page/post ID#, the page title, or the page slug. Any will do. Also checked here is the login status with is_user_logged_in(). If those two parameters are true, then we will fire wp_redirect(). If not, we are done and the page is loaded as normal.

  3. says

    You understood perfectly Chad, Thanks I will give this a try tomorrow and once again thanks for the great addon and support.

    DJ

  4. Des says

    Hello,
    I recently install WP-members plugin, to learn how things are working with members in wordpress.
    So far WP-members looks like GREAT plugin, everything works OK and even if I am new to wordpress I manage it quite good.
    Except this one thing that it is bothering me:
    whenever a user log in it redirects him to one of my category pages.

    I do not understand why it redirects specificly to that page and how to change that.

    I tried to use wpmem_login_redirect
    I put this code, with MYURL-a valid url of the page where I want to redirect user after login, into my function.php:

    add_filter( 'wpmem_login_redirect', 'my_login_redirect' );
    function my_login_redirect()
    {
    $url = 'http://MYURL';
    return $url;
    }

    But nothing happen.
    Should I put this code somewhere else, not function.php?
    I have some knowledge of php and wordpress but not much – did I miss something?

    Best regards,
    Des

    • says

      If you copied this code out of the Users Guide, the encoding in a PDF does not translate. If that’s what you did, type it out and it should work fine.

  5. Jason says

    Trying to use the wpmem_login_redirect function in order to detect a user’s level before redirecting. Is this possible? Is the user technically logged in at the point that the wpmem_login_redirect filter gets fired?

    If not, do you have any suggestions on that? I need to detect a user’s role and also get their ID prior to letting them log in fully to do a check.

    Thanks!

    • says

      That’s actually a really good question! wpmem_login_redirect is a filter that filters the value of $redirect_to that is passed during the login process. The WP-Members process is very similar to the WP process (in fact, it is using wp_signon and other WP functions). This filter comes actually before wp_signon is fired, so you are filtering the value prior to the user being logged in.

      In looking at the code, I think it might be possible to move the filter to happen as the wp_redirect function is being fired. That would mean you could filter it based on knowledge of the user since they would technically be logged in at that point – I THINK!

      Since you raised this question, that encourages me to research this. I’ll need to test it out to see if that is in fact going to work. Also, and more importantly, I have to make sure that it doesn’t break other possible uses of the filter that I know are out there. Both of these are going to require some testing and playing around.

      BUT… I do think I have a solution in the meantime, and if my thoughts about moving the location of the filter are correct, this would continue to work even if I moved it. While the user is not actually logged in at the point of the filter, you do have access to their username they are attempting to login with as that is being posted. Using that, you can use the WP function get_userdatabylogin to get the user data in an object. In my example I put that in $user and then you can get whatever you need via the $user object or $user->ID.

      add_filter( 'wpmem_login_redirect', 'my_login_redirect', 1 );
      function my_login_redirect( $redirect_to )
      {
      	// get the user object
      	$user = get_userdatabylogin( $_POST['log'] );
      
      	/* 
      		at this point, you have the $user 
      		object and $user->ID to get what you 
      		need. you should be able to get their 
      		login level and return various values 
      		for $redirect_to accordingly.
      	*/
      
      	return $redirect_to;
      }
      
      • Jason says

        Thanks Chad!

        I was onto that route myself after writing the comment. I looked into the core file and noticed that the wpmem_login was doing something similar so I just wrote a custom function in functions.php that will grab the user’s login via get_user_by() function using the action hook wp_login. Still working on the functionality though.

        Thanks again for responding so quickly!

      • Jason says

        Let me explain the end-goal further too…because I’m running into some issues. I don’t want to user to be logged in at the point of checking. In fact I want to bar the login unless another action passes. This is being used for the accept/decline of a Terms of Service. Here is what I’m attempting to accomplish:
        1) User inputs username/password and clicks login.

        2) If user hasn’t yet accepted the ToS, a popup shows after login is clicked and they can scroll through the ToS and click either ‘accept’ or ‘decline’. Accept updates user_meta with a value that ToS is accepted (or creates the user_meta key if it’s not in existence). Denial will simply close the popup and sit on the landing page without the user being logged in. In essence, denial will stop the login process short OR if that’s not possible, log the user out if this process logs them in temporarily (which is not the ideal scenario).

        This presents and interesting issue of an event that happens in between inputting login information and the actual event of logging in.

        • says

          Actually, I would disagree that logging the user first would be the less ideal scenario. I don’t think you want to be updating a user’s meta without a proper validation process. You can validate the password after you have the $user object with something like:

          // check to see if it's a good user/pass combination
          $pass = wp_check_password( $_POST['pwd'], $user->user_pass, $user->ID );
          if( ! $pass ) {
            // if this is false, the user's not going to be logged in anyway
            return $redirect_to;
          } else {
            // you could do something else here 
            // if you have a valid password
          }
          

          Then if it’s true, you could do something right there.

          But I wouldn’t probably do this the way you described. I’d probably be more inclined to force any logged in user to be redirected to information about accepting ToS before they are allowed to continue. That way, even if the user has a login cookie set, they are forced to update. There are many WP hooks you could hook into early in the page load, then check is_user_logged_in, if so, check the user meta for TOS, and if that’s not valid wp_redirect to an update process.

          • Jason says

            Excellent point! I didn’t consider that at all; thanks, I’m going to give this example code a whirl. I was also trying out the wp_authenticate action hook before I read your latest comment.

            Thanks again

          • says

            Hooking into the wp_authenticate action would be a great place to do this if you want to catch them at login.

    • says

      You could use that, but it would probably be more appropriate to use wpmem_post_register_data. They are both action hooks in the same general location. You do get the benefit of having the registration data available with wpmem_post_register_data (which you probably wouldn’t need for pushing a GA event, but it’s there if you do).

      • Jason says

        Chad –
        I’m also interested in implementing this into my Google Analytics. I found your documentation about wpmem_post_register_data, but I’m not sure how to conform the GA code to work with it. Google gives an example of how to use the code with an onClick event on a link: Play

        Can you please help me turn this code into something useable within your wpmem_post_register_data filter hook?

        Thanks!

          • says

            I think your challenge there is that you are trying to implement something that is developed for client side (tracking an event using GA’s javascript) into something that is server side (a php action – wpmem_post_register_data is an action hook, not a filter hook). You would probably need to focus on adding an event tracking javascript to the registration form itself, such as adding the onClick action to the form submission process. You could do that with the wpmem_register_form filter hook.

  6. Michael says

    Hi there,

    Ill try keep this simple. Basically I have downloaded WP-Members plugin and activated it. I have also created a register page and a log in page and a members area page, all of which are working. The registration works perfectly! My problem lies in the log in. Once I register a new user and attempt to log in at the log in page, I am presented with a blank screen after clicking “log in”.

    I was having a read of the WP-Members manual and thought that I may need the wpmem_login_redirect filter in order for it to know where to direct me once I log in? Am I right? If so where do I put this piece of php? Baring in mind I am using a child theme.

    Thanks in advanced!

    • says

      Hi Michael – That sounds like something interfering with the login process which happens at the WP action ‘init’ (initialization). I would suggest that you begin by making sure there are not any conflicts with any other plugins. You’ll need to deactivate all other plugins and re-test, then bring plugins back up one-by-one, testing each time.

      • Michael says

        Hi Chad,

        Just went through and deactivated all plugins apart from wp-members and I am still presented with the same problem. Is there a way I can check that ‘init’ is working?

  7. Robbert says

    Hi,

    Is there also a hook available when:
    -a user registers. I want to write some of the data to my mysql db then
    -a user is validated by the admin. Again i want to write some data to the mysql db.

    Any ideas?
    Thanks

    • says

      Yes, there is a hook for that, and there is an example in the users guide. Here’s a little more insight…

      There are four action hooks in the registration and user update process:

      1. wpmem_pre_register_data – action hook comes after form data validation but BEFORE new user insert.
      2. wpmem_post_register_data – action hook comes AFTER new user insert.
      3. wpmem_pre_update_data – same as _pre_register_data, but with the user update process.
      4. wpmem_post_update_data – same as _pre_update_data, but with the user update process.

      Depending on where you want your additional actions to occur, and what you need for them to work (such as $user_ID), it is important to note that for wpmem_pre_register_data there is no user ID yet as the new user has not been created. If you need the user ID, you’ll need to hook in at wpmem_post_register_data. wpmem_post_register_data does occur PRIOR to the new user email being sent, so anything you do in your additional action would be available to be picked up in the new user email (which can be hooked into with the wpmem_email_newreg filter).

      These four hooks pass the registration data in an array. The contents will vary depending upon what additional fields you have set up, so if you need to know what you have available to the action, it might be best to run the following:

      <?php
      add_action( 'wpmem_pre_register_data', 'my_reg_hook', 1 );
      function my_reg_hook( $fields ) {
      	echo "<pre>"; print_r( $fields ); echo "</pre>";
      	exit();
      } ?>

      This will stop the registration process at the action and will dump the array to the screen so you can see what you have. If you do the same with wpmem_post_register_data, you’ll see that you get the some additional fields, including the user ID.

      Hope that helps.

        • says

          As an update to this, the upcoming 2.7.5 release will give you the ability to return an error message after the _pre hooks if you are doing any validation. You’ll be able to globalize $wpmem_themsg and then give it a value if you need to return a relevant error message. Something like this:

          add_action( 'wpmem_pre_register_data', 'my_reg_function' );
          function my_reg_function( $fields )
          {
          	// doing something here that needs validation...
          	
          	if( ! $valid ){
          		global $wpmem_themsg;
          		$wpmem_themsg = 'Your information was not valid...';
          		
          		return $wpmem_themsg;
          	}
          }
  8. genesis says

    Hi Chad,
    Im just new here in wordpress, where will I put the codes? I want to put your sample of gravatar. really need your help.
    Thanks

    • says

      The hooks in the plugin are just like hooks in WordPress. You would put these into your theme’s functions.php file. Note: you should also be setting this up as a child theme so that if an update is loaded for the theme you don’t overwrite your changes.

      • Genesis says

        Hi Chad!, I did what you told me. I put ths code in function.php
        add_filter( ‘wpmem_sidebar_status, ‘my_sidebar_status’ );
        function my_sidebar_status( $string ) {
        // This is an example that puts a user’s avatar next to the
        // user if they are logged in.
        global $user_ID;
        $gravatar = ” .
        get_avatar( $user_ID, ’46’ ) . ”;
        $string = $gravatar . $string;
        return $string;
        }

        I didn’t include the code. but I got some errors. Any Help? Thanks for the advice.

        • says

          I’m guessing you copy/pasted some of this out of the PDF of the User’s Guide? There is a typo in there that is my fault – the add_filter parameter wpmem_sidebar_status must be wrapped with single quotes ‘wpmem_sidebar_status’ – that’s a typo in the User’s Guide.

          The other problem is that if you copy/pasted this code, you need to re-type all of those single quotes in something like notepad. The unicode encoding is incorrect (and oddly, not the same as what I get from copy/pasting out of the PDF). I don’t know your level of knowledge with PHP so rather than go into the whys and wherefores, I’ll just give you an example.

          Things like this:

          ‘my_sidebar_status’

          need to be changed to this:

          'my_sidebar_status'

          Note the single quotes – I’ve changed them from unicode left/right single quotation marks to what is technically and typographically an apostrophe but we all call it a single quote. When I copy from the PDF, I just get them that way for the add_filter parameters, but what you have pasted in your comment has them in almost every instance.

          So… add a closing single quote on wpmem_sidebar_status and retype all the single quotes in a basic text editor and you should be fine.

          • Genesis says

            Hi Chad!
            You’re absolutely right, I copy/paste the code in the PDF. I’ll just digest what you have said. As I have said I’m new here in wordpress so I don’t have enough experience in doing this stuff. Uhmm, is it possible to get some information in the members area and display it in a sidebar?
            This is the picture:

            Assuming that I have a field named ‘Major’ which means your major in your course like B.S in Business Major in Marketing and when a user log in they will see a gravatar and below it is their name and their major. Is it possible?How will I do that?Really need this for my first time creating a website. Thanks in Advance.

            Btw ( I used the WP-Members Registration Form )

  9. Genesis says

    By the way what I mean I didn’t the code is this ( ) The beginning and end tag of php.

  10. says

    I tried it on my website but I am not able to Register on it as a user. There is not option to register either

    Please reply soon. I want my users to see the “register Now !” so if they don’t have account they can get register their selves and use the website.

    Thanks,
    Punit

  11. Anita says

    Hi Chad,
    First of all thanks for great plugin! It really works for me but I am struggle with one issue.
    I want subscribers and admins to be redirected to different pages after login.
    For subscribers it will be customised user account page and for admin wp-admin page.
    I have used this code

    add_filter( ‘wpmem_login_redirect’, ‘my_login_redirect’ );
    function my_login_redirect()
    {
    $url = ‘there url';
    return $url;
    }

    but it redirects all users to the same page. The question is is there any way to specify after login page by role?

    Will be relly greatful for help.
    xx
    Anita

    • says

      You would need to query based on the user’s role to determine if they were an admin or subscriber, then use a conditional statement to set a different URL based on what their role is.

  12. Tessa says

    Hi Chad,
    I’d like to be able to receive a copy of the emails sent to new registrations so I know their passwords and can test them should they have issues. I saw that there was a filter for adding a second email address to the emails sent from the plugin…I just don’t know where, exactly to add the code and if I need to copy and paste the whole thing or just the portion that applies to what I’m trying to do. Any help would be much appreciated!

    • says

      At this time, you cannot send the password to anyone but the user. The filter hook you mentioned is only to send the admin notification to multiple addresses, but you cannot include the password in the admin notification.

      From a security standpoint, you shouldn’t allow access to user passwords to anyone but the user. That is part of the reason why CMSs like WordPress encrypt stored passwords. If you have users that are not technically savvy, you could simplify the random password generation (there is a premium tutorial here: http://rocketgeek.com/filter-hooks/easier-random-password-filter/). If you need to, you can also reset a user’s password (or they can can also request a password reset themselves), but allowing access to passwords is generally not a best practice you want to follow.

      That being said, there is an update in the upcoming 2.8 version that would allow you to have access to the plain text password in some of the action and filter hooks in the registration process. While that is being included to allow for admins to set up auto login upon registration without a workaround (http://rocketgeek.com/tips-and-tricks/configuring-auto-login-at-registration/), that would also allow you to include it in an email to the admin.