Register/Login button on restricted page
-
Hi
Using this plugin we can restrict the content of any page. When some one tries its shows “Sorry, but you do not have permission to view this content.”
Instead of showing this message, can we show register/login button. So that they can login.
-
Hi
I had the same requirement. The settings for the plugin allows you to define the content that is output when content is restricted. If you define a shortcode to display the login form you can use this.The plugin author has a post https://justintadlock.com/archives/2011/08/30/adding-a-login-form-to-a-page about this subject and https://www.ads-software.com/support/topic/can-you-stop-wp_login_form-redirecting-to-wp-login-on-fail was also useful.
I added the code in these posts to my themes functions.php file and added the shortcode to the plugin settings page and it works well enough.
There are plugins that provide login form shortcodes, here is one https://www.ads-software.com/plugins/profile-builder/ and these plugins may handle the nuances of the login process better than adding your own code.
Here is the code I used
add_action( 'init', 'add_login_form_shortcode' );
function add_login_form_shortcode() {
add_shortcode( 'login-form', 'login_form_shortcode' );
}function login_form_shortcode() {
if ( is_user_logged_in() )
return '<p>You are already logged in!</p>';$the_form = '';
if ($_GET['login']=='failed')
$the_form .= '<p>Incorrect username and password, please try again</p>';/* Set up some defaults. */
$defaults = array(
'label_username' => 'Username',
'label_password' => 'Password'
);/* Merge the user input arguments with the defaults. */
$attr = shortcode_atts( $defaults, $attr );/* Set 'echo' to 'false' because we want it to always return instead of print for shortcodes. */
$attr['echo'] = false;$the_form .= wp_login_form( $attr );
return $the_form;
}add_action( 'wp_login_failed', 'my_front_end_login_fail' ); // hook failed login
function my_front_end_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from?
// if there's a valid referrer, and it's not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
wp_redirect( $referrer . '?login=failed' ); // let's append some information (login=failed) to the URL for the theme to use
exit;
}
}regards
Dean…but if anyone has a neater solution then do tell…
Having looked at the plugin code I can see there is already a shortcode to display a login form
[members_login_form]
Just add this to the ‘Error Message’ field. From the dashboard -> settings -> Members
No need to update your functions.php!
regards
DeanHi Dean Radhish
Thanks for the information.
Can we show a Register/Login button. There should be option of both.
Using [members_login_form] shortcode, we are showing register form, but if someone is not registered he wont be able to login.
Hi,
I can’t see a way to add a registration link using the shortcodes available in the Members plugin but you could add it to your ‘Error Message’ field. Something like
<a href="/wp-login.php?action=register">register</a>
This hard codes the location of the login script which is not ideal as the location can be changed. You could create a custom shortcode that allows you to handle this situation.As a third and better option, you could add this to your functions.php in your theme
function force_login_if_page_protected() { global $post; if (is_single() || is_page()) { $required_roles = get_post_meta( $post->ID, '_members_access_role', false ); if (empty($required_roles) || is_user_logged_in()) { return; } else { auth_redirect(); } } } add_action( 'template_redirect', 'force_login_if_page_protected' );
This displays the default WordPress login screen and this contains a Register link (assuming users are allowed to register).
The above code isn’t perfect. It will continue to protect the posts/pages if you disable the Members plugin or don’t enable the content permissions feature in the Members settings, and it only works for single pages/posts
regards
DeanHi,
I can’t see a way to add a registration link using the shortcodes available in the Members plugin but you could add it to your ‘Error Message’ field. Something like
<a href="/wp-login.php?action=register">register</a>
This hard codes the location of the login script which is not ideal as the location can be changed. You could create a custom shortcode that allows you to handle this situation.As a third and better option, you could add this to your functions.php in your theme
function force_login_if_page_protected() { global $post; if (is_single() || is_page()) { $required_roles = get_post_meta( $post->ID, '_members_access_role', false ); if (empty($required_roles) || is_user_logged_in()) { return; } else { auth_redirect(); } } } add_action( 'template_redirect', 'force_login_if_page_protected' );
This displays the default WordPress login screen and this contains a Register link (assuming users are allowed to register).
The above code isn’t perfect. It will continue to protect the posts/pages if you disable the Members plugin or don’t enable the content permissions feature in the Members settings, and it only works for single pages/posts
regards
DeanHi,
I can’t see a way to add a registration link using the shortcodes available in the Members plugin but you could add it to your ‘Error Message’ field. Something like
register
This hard codes the location of the login script which is not ideal as the location can be changed. You could create a custom shortcode that allows you to handle this situation.As a third and better option, you could add this to your functions.php in your theme
function force_login_if_page_protected() {
global $post;
if (is_single() || is_page()) {
$required_roles = get_post_meta( $post->ID, '_members_access_role', false );
if (empty($required_roles) || is_user_logged_in()) {
return;
} else {
auth_redirect();
}
}
}add_action( 'template_redirect', 'force_login_if_page_protected' );
This displays the default WordPress login screen and this contains a Register link (assuming users are allowed to register).
The above code isn’t perfect. It will continue to protect the posts/pages if you disable the Members plugin or don’t enable the content permissions feature in the Members settings, and it only works for single pages/posts
Hi,
I can’t see a way to add a registration link using the shortcodes available in the Members plugin but you could add it to your ‘Error Message’ field./wp-login.php?action=register
This hard codes the location of the login script which is not ideal as the location can be changed. You could create a custom shortcode that allows you to handle this situation.
regards
DeanHi,
As a third and better option, you could add this to your functions.php in your theme
function force_login_if_page_protected() { global $post; if (is_single() || is_page()) { $required_roles = get_post_meta( $post->ID, '_members_access_role', false ); if (empty($required_roles) || is_user_logged_in()) { return; } else { auth_redirect(); } } } add_action( 'template_redirect', 'force_login_if_page_protected' );
This displays the default WordPress login screen and this contains a Register link (assuming users are allowed to register).
The above code isn’t perfect. It will continue to protect the posts/pages if you disable the Members plugin or don’t enable the content permissions feature in the Members settings, and it only works for single pages/posts
regards
DeanHi,
Here is code that plays better with the plugin
// this function works with the Members plugin. If a page or post requires a user to have a certain role
// and if the user has not logged in then they are asked to login. Members plugin will then check their roles
function force_login_if_page_protected() {// if already logged in then nothing to force
if (is_user_logged_in()) {
return;
}// check members plugin loaded
if(!function_exists( 'members_can_user_view_post' )) {
return;
}global $post;
// if request is for a single post or page and that post or page is
// protected then require the user to login
if (is_single() || is_page()) {
if (!members_can_user_view_post($post->ID)) {
auth_redirect();
}
}}
add_action( 'template_redirect', 'force_login_if_page_protected' );
This is my final word on this subject!
Hi Dean,
Where do i need to place this code?
Is it function.php ?Hi, yes in functions.php of your theme. I plan to write an add-on plugin for this in the next few days if that will help
regards
DeanGreat !
Hi, plugin now available at https://www.ads-software.com/plugins/force-members-logon/
If you have any issues please contact me through the plugins support forum
regards
Dean
- The topic ‘Register/Login button on restricted page’ is closed to new replies.