is user logged in not working

I am creating a plugin and wish to check if the user is logged in the run another function.

This is my first ever plugin and i am still learning how to program at the same time. following I have a simple user conditional, if user is logged in and i am also retrieving data from an array to check if the admin has checked Yes in the plugin options to check for user logged in then execute another function. however I am getting an error Call to undefined function is_user_logged_in() i think i may need to wrap my conditional in a function and then add an action, can someone please help me. thanks

Read More
/* Logged user conditionals */

$logged = get_option('lu_ban_data');

if ($logged['loggedU'] == 'loggedYes' && is_user_logged_in() ) {
    lu_ban_detect();
    } else {
    return false;
    }

update
Okay I think i managed to get it to work

add_action( 'init', 'lu_user_logged' );
function lu_user_logged() {
$logged = get_option('lu_ban_data');
echo $logged;
if ($logged['loggedU'] == 'loggedYes' && is_user_logged_in() ) {
    lu_ban_detect();
    } else {
    return false;
    }
}

is this the proper way?

Related posts

1 comment

  1. The point of the code is to load the lu_ban_detect in the footer of the page. That function then prints Javascript to the footer, per this comment:

    Has javascript wrapped in a function named “lu_ban_detect” and it
    prints it in the footer of the page

    That being the case, this is not the proper way to do this. Printing scripts directly to the page is discouraged, and the code looks overcomplicated to me.

    The first change I’d make would be this:

    function lu_user_logged() {
      if (is_user_logged_in()) {
        $logged = get_option('lu_ban_data');
        if ($logged['loggedU'] == 'loggedYes') {
          lu_ban_detect();
        }
      }
    }
    add_action( 'wp_footer', 'lu_user_logged' );
    

    You are printing in the footer, so just hook to wp_footer, and there is no need to return anything as far as I can tell. I also put is_user_logged_in() first because you might save a query that way.

    But, as mentioned, you should probably not be echoing scripts directly into the page.

    You can echo data into the page like this:

    function lu_user_logged() {
      if (is_user_logged_in()) {
        $logged = get_option('lu_ban_data');
        if ($logged['loggedU'] == 'loggedYes') {
          lu_ban_detect();
        }
      }
    }
    add_action( 'wp_enqueue_scripts', 'lu_user_logged' );
    
    function lu_ban_detect() {
      wp_localize_script('jquery','data','abcd');
    }
    

    The first parameter to wp_localize_script needs to be a script that is already enqueued.

    If you need to load more complicated Javascript, you should probably register and enqueue a .js file.

    function lu_ban_detect() {
      wp_register_script( 'lu_ban_detect', 'path/to/js', array('jquery'), 1, true );
      wp_enqueue_script('lu_ban_detect');
    }
    

Comments are closed.