I'd like to implement a custom logout function that only runs if the conditions are met. Instead it returns me to the homepage whether the conditions are true or not.
Here's the code that should do the logging out:
function no_cookie() {
global $noCookiesLeft;
if($noCookiesLeft) {
wp_redirect("mysite.com/custom logout");
exit;
}
}
add_action('wp_logout','no_cookie',10,2);
And here's the code that should trigger this:
if($cookie_count >= 3) {
wp_delete_user($id);
$noCookiesLeft = true;
wp_logout();
}
noCookiesLeft is a global variable since I haven't found any other way to do this, and google failed me.
Edit:
function login_cookie($user_login, $user) {
global $wpdb;
global $noCookiesLeft;
$id = $user->ID;
if(!isset($_COOKIE['userCookie'])) {
if(in_array('administrator', $user->roles))
$wpdb->delete('wp_cookies',['user_id'=>$id]);
$cookie_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_cookies WHERE user_id=".$id."");
if($cookie_count >= 3) {
wp_delete_user($id);
}
else {
$value = md5($id);;
setcookie('userCookie', $value, time()+360000*24*100, "", "",false);
$wpdb->insert("wp_cookies",[
'user_id'=>$id,
'cookie_value'=>$value
],
[
'%d',
'%s'
]);
}
}
}
add_action('wp_login','login_cookie',10,2);
function no_cookie() {
wp_redirect("/error);
exit;
}
}
add_action('delete_user','no_cookie',10,2);
wp_cookies
isn't a Core table. Is this a table you've created?