1

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

/* 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?

  • That is the right idea, yes. Make sure you carefully consider which hook you use depending on your purpose. You should post that solution as a proper "Answer" in the answer box below. – s_ha_dum Aug 31 '13 at 13:43
  • Well i need the lu_ban_detect() to be executed in the front end, is init hook fit for this? – David Garcia Aug 31 '13 at 17:05
  • It will work, yes, but it may not be the best hook. What does lu_ban_detect do? – s_ha_dum Aug 31 '13 at 17:19
  • Has javascript wrapped in a function named "lu_ban_detect" and it prints it in the footer of the page – David Garcia Aug 31 '13 at 17:37
2

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');
}
  • Thanks for your advice, i was en-queueing the js file and localizing the data, but choose to wrap it in a function, it is still in development, for now, how can I only make the function wrapping the javascript be executed when it is called in the lu_user_logged, at the moment the function is being executed regarless if I call it or not, maybe im missing an extra parameter, the following is the first two lines of the js wrapping function add_action('wp_footer', 'lu_ban_detect'); function lu_ban_detect() { – David Garcia Aug 31 '13 at 19:10
  • kk i got it to work, i added an action to the function wrapping the javascript and then call it using the user logged function do_action within the conditional statements – David Garcia Aug 31 '13 at 19:27

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.