WordPress中PHP匿名函数嵌入CSS实现未登录用户禁用WooCommerce弹窗失效求助
Hey there! I see exactly where the issue is with your code—you’re using a custom action hook disable_popup that WordPress doesn’t recognize, so your check_if_logged_in function never actually gets triggered. That’s why combining it with the login check does nothing, even though your standalone CSS works perfectly.
Let’s fix this by hooking directly into a standard WordPress action that fires reliably when the page loads. Here’s the corrected code:
add_action( 'wp_head', 'hide_popup_for_logged_out_users' ); function hide_popup_for_logged_out_users() { // Check if the user is NOT logged in if ( ! is_user_logged_in() ) { ?> <style> .ex-fdlist .exwf-order-method { display: none !important; } </style> <?php } }
Why this works:
- We’re using WordPress’s built-in
wp_headaction, which fires every time the<head>section of a page is rendered—so your function will actually run on every page load. - The login status check happens directly inside the hooked function, ensuring we only output the hiding CSS when a logged-out user visits.
- I added
!importantas a safeguard in case the plugin’s own CSS has higher specificity (you can remove it if your original CSS worked without it).
If the popup is loaded dynamically via JavaScript after the page loads, you might need an extra step (like checking login status with JS), but since you confirmed the standalone CSS works, this should resolve your issue right away.
内容的提问来源于stack exchange,提问作者The Thinker

