Код: Выделить всё
function check_user_status() {
$response = array(
'loggedIn' => is_user_logged_in(),
'username' => '',
'isSubscriber' => false
);
if ($response['loggedIn']) {
$current_user = wp_get_current_user();
$response['username'] = $current_user->user_login;
$response['isSubscriber'] = in_array('subscriber', $current_user->roles);
}
wp_send_json($response);
}
add_action('wp_ajax_check_user_status', 'check_user_status');
add_action('wp_ajax_nopriv_check_user_status', 'check_user_status');
Код: Выделить всё
let currentUser = null;
let isSubscriber = false;
function checkUserStatus() {
$.ajax({
url: wpApiSettings.ajaxUrl,
type: "POST",
data: {
action: "check_user_status",
nonce: wpApiSettings.nonce,
},
success: function (response) {
console.log("User status response:", response);
if (response.loggedIn) {
currentUser = response.username;
isSubscriber = response.isSubscriber;
} else {
currentUser = null;
isSubscriber = false;
}
updateAuthUI();
},
error: function (xhr, status, error) {
console.error("Error checking user status:", error);
},
});
}
function updateAuthUI() {
const authSection = $("#auth-section");
const userInfo = $("#user-info");
const usernameSpan = $("#username");
const subscriberContent = $("#subscriber-content");
if (currentUser) {
authSection.find("#login-button, #register-button").hide();
userInfo.show();
usernameSpan.text(currentUser);
if (isSubscriber) {
subscriberContent.show();
} else {
subscriberContent.hide();
}
} else {
authSection.find("#login-button, #register-button").show();
userInfo.hide();
subscriberContent.hide();
}
}
$(document).on("click", "#logout-button", function (e) {
e.preventDefault();
window.location.href = wpApiSettings.logoutUrl;
});
checkUserStatus();
setInterval(checkUserStatus, 60000);
document.addEventListener("DOMContentLoaded", () => {
setMealType("breakfast");
populatePopularDishes();
checkUserStatus();
});
Код: Выделить всё
Zaloguj się
Zarejestruj się
Witaj, !
Wyloguj się
Zawartość dla subskrybentów
Ta treść jest widoczna tylko dla zalogowanych subskrybentów.
На самом деле я хочу, чтобы определенный DIV ограничивался ролью в учетных записях Wordpress.
Я попробовал, например ИИ говорил мне о каждой настройке, и никто не работал.
Подробнее здесь: https://stackoverflow.com/questions/788 ... hp-js-html