Hi all
By default, s2Member will redirect a user who doesnât have access to protected content. The redirection points to your Membership Options Page where you can display anything youâd like. Whenever s2Member performs this redirection it calls upon wp_redirect_w_mop_vars(), which also contains a hook, so that developers can customize this further if theyâd like to.
The hook can be seen here and this is the name of that hook:
ws_plugin__s2member_after_wp_redirect_w_mop_vars
So if you would like the redirection to point to the login page and include an additional redirection back to the original content once they log in, you can use an s2Member plugin file like this.
Create: wp-content/mu-plugins/s2-redirect-wp-login.php
<?php
// Requires PHP v5.4 (or higher).
add_action('ws_plugin__s2member_after_wp_redirect_w_mop_vars', function(array $vars) {
wp_redirect(add_query_arg('redirect_to', urlencode($vars['seeking_uri']), wp_login_url()), $vars['status']);
});
This redirects a visitor without access, to: /wp-login.php?redirect_to=[original URL]
. Once they login they will be returned to the page they were trying to access. Assuming they have an account they can log in with, and that their account is associated with a Membership Level that allows them to access the page they were trying see prior to logging in.
You could take this a step further and only redirect to the login page if other conditions apply; e.g., only if you know for sure that they are not logged in yet.
<?php
// Requires PHP v5.4 (or higher).
add_action('ws_plugin__s2member_after_wp_redirect_w_mop_vars', function(array $vars) {
if (!is_user_logged_in()) {
wp_redirect(add_query_arg('redirect_to', urlencode($vars['seeking_uri']), wp_login_url()), $vars['status']);
}
});
Taking it another step further I can limit this behavior to a specific page ID as well.
<?php
// Requires PHP v5.4 (or higher).
add_action('ws_plugin__s2member_after_wp_redirect_w_mop_vars', function(array $vars) {
if (!is_user_logged_in() && is_page(123)) {
wp_redirect(add_query_arg('redirect_to', urlencode($vars['seeking_uri']), wp_login_url()), $vars['status']);
}
});