For is_page() to work as expected, it must be called within the conventional WP loop context. If some other code invokes a different query in the process, is_page() will likely not work as expected. Lacking more detail about what’s going on with this page, I’m guessing that this is what is going on.
You could try calling wp_reset_query() before calling is_page(), but that could corrupt whatever other query happens to be in progress. You may need to verify the current page through other means. For example, you could check $_SERVER['REQUEST_URI']. Be aware that when you migrate from /wptest/ the code would also need to be modified. Thus it’s not a great solution, but it will work in a pinch.
ETA: Another thought; you could check is_page() earlier through a different hook where is_page() works as expected. If it’s the correct page, define a unique constant. Then in disable_wordfence_captcha_for_courses_login(), check if the constant is defined where you now use is_page()
-
This reply was modified 2 years ago by
bcworkz. Reason: new idea
@bcworkz Excellent idea, thanks!
I modified my code and used the template_redirect hook to call my function and set the parameter to false when I’m on the specific page. But for some reason the value is always reset to true by the time the wordfence function is called. This is driving me crazy 🙂
This is the code I have. I first tried with a global parameter but modified to code to use a class hoping this would solve the issue but it must be coming from something else.
// No Wordfence captcha check on courses login
// Global variable to store the captcha requirement
class My_Wordfence_Captcha_Handler {
private $disable_captcha = true;
public function __construct() {
add_action('template_redirect', array( $this, 'determine_captcha_requirement' ) );
add_filter('wordfence_ls_require_captcha', array( $this, 'modify_wordfence_captcha' ) );
}
public function determine_captcha_requirement() {
error_log( 'In determine captcha function' );
error_log( 'Variable value: ' . var_export( $this->disable_captcha, true ) );
if ( is_page('courses-login-page') ) {
$this->disable_captcha = false;
error_log( 'Wordfence Captcha: Disabled for specific login page' );
} else {
$this->disable_captcha = true;
error_log( 'Wordfence Captcha: NOT disabled for page: ' . get_queried_object()->post_name );
}
error_log( 'Variable value at end determine_captcha_requirement function: ' . var_export( $this->disable_captcha, true ) );
}
public function modify_wordfence_captcha( $requireCaptcha ) {
error_log( 'Variable value in wordfence function: ' . var_export( $this->disable_captcha, true ) );
return $this->disable_captcha;
}
}
new My_Wordfence_Captcha_Handler();
This is what the log shows:
If somebody has an idea what could be “resetting” the value in this variable, or where I could look I would appreciate.
[03-Oct-2024 15:33:23 UTC] In determine captcha function
[03-Oct-2024 15:33:23 UTC] Variable value: true
[03-Oct-2024 15:33:23 UTC] Wordfence Captcha: Disabled for specific login page
[03-Oct-2024 15:33:23 UTC] Variable value at end determine_captcha_requirement function: false
[03-Oct-2024 15:33:36 UTC] Variable value in wordfence function: true
It’s certainly an issue with variable or property scope. Using a global variable or object is one solution. Many say the use of globals is bad programming, but WP core uses them a lot. I’m a little weak on property scope in class objects, but I think if you declare $disable_captcha as static, the value will be within scope of the WF filter’s callback.
The other option is to define a constant. Its scope is always global. Obviously its value cannot be altered once defined, but there should be no need to for any given request.
Using static makes sense as it avoids that there can be multiple instances of the same variable but it gives the same result.
// No Wordfence captcha check on courses login
// Global variable to store the captcha requirement
class My_Wordfence_Captcha_Handler {
private static $disable_captcha = true;
public function __construct() {
add_action('template_redirect', array( $this, 'determine_captcha_requirement' ) );
add_filter('wordfence_ls_require_captcha', array( $this, 'modify_wordfence_captcha' ) );
}
public function determine_captcha_requirement() {
error_log( 'In determine captcha function' );
error_log( 'Variable value: ' . var_export( self::$disable_captcha, true ) );
if ( is_page('courses-login-page') ) {
self::$disable_captcha = false;
error_log( 'Wordfence Captcha: Disabled for specific login page' );
} else {
self::$disable_captcha = true;
error_log( 'Wordfence Captcha: NOT disabled for page: ' . get_queried_object()->post_name );
}
error_log( 'Variable value at end determine_captcha_requirement function: ' . var_export( self::$disable_captcha, true ) );
}
public function modify_wordfence_captcha( $requireCaptcha ) {
error_log( 'Variable value in wordfence function: ' . var_export( self::$disable_captcha, true ) );
return self::$disable_captcha;
}
}
new My_Wordfence_Captcha_Handler();
[03-Oct-2024 18:13:42 UTC] In determine captcha function
[03-Oct-2024 18:13:42 UTC] Variable value: true
[03-Oct-2024 18:13:42 UTC] Wordfence Captcha: Disabled for specific login page
[03-Oct-2024 18:13:42 UTC] Variable value at end determine_captcha_requirement function: false
[03-Oct-2024 18:13:56 UTC] Variable value in wordfence function: true
I will have another look tomorrow.