Chronicles of a build - the signup plugin 04

Disclaimer

What I write in these posts is not the perfect travel of an expert and consumed WordPress developer but the gnarly and error-prone stroll of an average developer (pun intended).
I will make mistakes and will try to correct them along the road and mean to share the path I’ve taken with all its fallbacks and wrong turns and not to show the best possible one.

Follow along

After setting up the plugin using grunt-init I’ve deployed it in my local site and made it available on GitHub. I will commit to the GitHub repository throughout the work and the plugin can now be downloaded and installed in WordPress. The code I show becomes much more comprehensible when observed in context.

Giving the skeleton some life

In yesterday article I’ve shown the creation of a first “end-to-end” test (although I made a note about the definition of “end-to-end” in this particular context) to watch it fail and now I need to pass it.
Creating the first test made me take some commitments I will fulfill in code:

  1. the plugin redirects users not logged-in users to a custom login page
  2. the plugin redirects users of type “member” to a custom admin page
  3. the plugin will output, in this page, an element with an id = "member_login_form" attribute

To fulfil the points listed above I have some preliminary steps to take:

  1. I have to create a “member” user role: since the plugin will rely upon this user role it makes sense to delegate its creation to the plugin. I want to avoid pre-conditions as much as possible and want the plugin to be as robust as possible.
  2. I have to setup some redirection functions to redirect users to the right page
  3. I have to create a shortcode to allow site administrators to add the plugin front-end output in any page (or even a post for the matter) and be able to set the custom login page

Creating the “member” role

Tom McFarlin made an excellent job in his WordPress Plugin Boilerplate and having included his job into my grunt-init template leaves me with a plugin structure very easy to upgrade.
To make the plugin register the “member” user role I will use the plugin activation hook in the /member-signup.php file inside the function single-activate:

private static function single_activate() { // add the "member" role add_role( 'member', esc_html__( 'Member', $domain = $plugin_slug ), $capabilities = array() ); }

I leave capabilities empty now as what I really need in this moment is just to have a “member” role, more tests will guide me in future changes.

Redirecting or “you have to understand the rules before breaking them”

Even though I’m not trying at all to break the rules here some clarity sure helps: wp-admin page and wp-login page are different and play a different role. Clear as it may seem it took me a while to understand it and see where to hook in moment:
[caption id=“attachment_134” align=“aligncenter” width=“564”]Normal login and admin flow Normal login and admin flow[/caption]
What I want to accomplish is to simply add some member type check on logged-in users and replace the wp-login.php page.
[caption id=“attachment_135” align=“aligncenter” width=“684”]Modified login and admin flow Modified login and admin flow[/caption]

Adding the user check

This user type check I can make, see WordPress Action Reference, as early as when the init hook is called and will add it in the code:

public function redirect_members() { if ( is_user_logged_in() ) { // if the user is a member then do redirect him to the custom admin page if ( membersignup_User_Role_Checker::check_user_role('member') ) { $member_admin_page = membersignup_Options_Getter::get_member_admin_page_url(); wp_safe_redirect( $member_admin_page ); } exit; } }

Adding the redirection

Once again I hook in into init to check if the user has not logged in and redirect him to the custom login page:

`add_action( ‘init’, array( $this, ‘redirect_to_member_login’));

public function redirect_to_member_login(){ if ( ! is_user_logged_in() ) { // redirect visitors to a custom login page set by admins
// code below comes from StackOverflow:
// http://stackoverflow.com/questions/1976781/redirecting-wordpresss-login-register-page-to-a-custom-login-registration-page
global $pagenow;
if( ‘wp-login.php’ == $pagenow ) {
if ( isset( $_POST[‘wp-submit’] ) || // in case of LOGIN
( isset($_GET[‘action’]) && $_GET[‘action’]==‘logout’) || // in case of LOGOUT
( isset($_GET[‘checkemail’]) && $_GET[‘checkemail’]==‘confirm’) || // in case of LOST PASSWORD
( isset($_GET[‘checkemail’]) && $_GET[‘checkemail’]==‘registered’) ) return; // in case of REGISTER
else {
$member_login_url = membersignup_Options_Getter::get_member_login_page_url(); wp_redirect( $member_login_url );
}
exit();
}
}
}
`

More work to do

I still did not make it to the first green light. The sprint has become a crawl but that’s normal: at 8 AM this morning I knew nothing about redirection and how a WordPress login works.