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');
}