0

in my web I have a button (.x-btn.widgetbar) that displays a section of widgets in the upper right as in this example.

I have modified the icon to change based on whether the user is registered or not, I would like that when the user is not logged in redirect to a login page instead of displaying the widgetbar.

I'm new to this and I do not know where to start, I've consulted in several forums and I think I could find the code but I do not know what file to edit, I've been trying for weeks but I can not apologize in advance for the query.

I try this function in functions.php but doesnt works..

function wp_redirect_NotLogin ()
{
    if( isset($_POST['x-btn-widgetbar']) && ! is_user_logged_in() ) {
        wp_redirect( home_url( '/register/' ) ); 
    exit(); 
    }
}
  • Sorry to say that, but you obviously don´t know what you are doing. Do you even know what $_POST['x-btn-widgetbar'] means and why this might or might not make any sense here? – flomei Sep 29 '17 at 9:52
1

when you want interaction between client side and server side you should use ajax .

i think this Link will be very helpful to you :) Put this in your javascript

var data = {
    action: 'is_user_logged_in'
};
jquery.('.x-btn-widgetbar').on('click',function(){
   jQuery.post(ajaxurl, data, function(response) {
      if(response == 'yes') {
           // user is logged in, do your stuff here
       } else {
           // user is not logged in, show login form here
               window.location = <login url here >;

       }
   });
});

put this in your functions.php

function ajax_check_user_logged_in() {
    echo is_user_logged_in()?'yes':'no';
    die();
}
add_action('wp_ajax_is_user_logged_in', 'ajax_check_user_logged_in');
add_action('wp_ajax_nopriv_is_user_logged_in', 'ajax_check_user_logged_in');

Update #1 26-9-2017

and if you want widgetbar to hide you can use this jquery :

jquery('.x-widgetbar').hide();

or

jquery('.x-widgetbar').css({display:'none'});

or simplay put this in *.css file:

.x-widgetbar{display:'none'}

update#2 26-9-2017

if want only hide the widgetbar without hiding the x button it self you can replace .x-widgetbar by .x-widgetbar-inner

| improve this answer | |
  • Many thanks ibrahim, I have tried the code and nothing has changed, maybe the problem is mine, I use the same template as in the example of the post and the button is similar in name, when inspecting it has several states, .collapsed, I do not know if this generates some problem. I hope I explained well, it is not my language, even so I thank you very much for your help – wpecko Sep 26 '17 at 0:34
  • Thanks, i solve the problem.. The issue was in code, if i use .on('click' works perfect! – wpecko Sep 26 '17 at 2:10
  • Now i have a doubt, how can I prevent the widgetbar from being displayed or collapsed and only redirects? – wpecko Sep 26 '17 at 2:13
  • you can hide widgetbar easily by jquery like so : jquery('<.widgetbar -class-here>').hide(); and i will update my answer to fit your site ,and you are welcome any time dude – IBRAHIM EZZAT Sep 26 '17 at 7:59
  • Thanks again man! Your answer is a great help to me. I explained myself wrong before, it's not my language I'm sorry. If I click on the button the widgets bar expands and shows the widgets, I just want to avoid expanding it but still see the x-btn-widgetbar. – wpecko Sep 26 '17 at 11:11
1

just checking the body for logged-in class

if( jQuery('body').hasClass('logged-in') ) {
// do something
} 
| improve this answer | |
0

Thanks all for your help, Ibrahim youre great!!

Finally I got it to work as I wanted, so I close the post and leave what has worked for me if someone can at some point take advantage of it.

I try with the code that you provide me in Javascript:

var data = {
    action: 'is_user_logged_in'
};

jQuery('.x-btn-widgetbar').on('click', function() {       

  jQuery.post(ajaxurl, data, function(response) {
      if(response == 'yes') {
           // user is logged in, do your stuff here

       } else {
           // user is not logged in, show login form here
           jQuery('.x-widgetbar').hide();
           window.location.href='https://website.com/login';
       }
  });

});

And this in functions.php:

function ajax_check_user_logged_in() {
    echo is_user_logged_in()?'yes':'no';
    die();
}
add_action('wp_ajax_is_user_logged_in', 'ajax_check_user_logged_in');
add_action('wp_ajax_nopriv_is_user_logged_in', 'ajax_check_user_logged_in');

This code does exactly what I wanted but I had a slight delay in responding by taking a few seconds to redirect and sending an alert that I did not add in the code.

I've tried what Samuel and Parthavi (thanks a lot!) wrote and put this code in Javascript:

jQuery('.x-btn-widgetbar').on('click', function() {

  if(jQuery('body').hasClass('logged-in') ) {

  } else {
     jQuery('.x-widgetbar').hide();
     window.location.href='https://website.com/login';
  }

});

With this code the behavior is the same and works well, but the response is faster and prevents small delays.

If you see some way to debug the code or see something that would help me avoid problems I would appreciate it since I am not an expert in javascript.

Thanks in advance for all your help, I hope everything goes well for you.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.