My plugin Wp Photo Album Plus uses Ajax in many instances, both backend and frontend.
Because one should not have admin-ajax.php in an url from the frontend, i use the redirect method as follows:
// Add rewrite rule
function wppa_ajax_rewrite_rule() {
add_rewrite_rule( 'wppaajax/?$', 'index.php?wppa_ajax=1', 'top' );
$rewrite_rules = wppa_get_option( 'rewrite_rules', array() );
if ( ! is_array( $rewrite_rules ) || ! in_array( 'index.php?wppa_ajax=1', $rewrite_rules ) ) {
flush_rewrite_rules();
}
}
add_action( 'init', 'wppa_ajax_rewrite_rule' );
// Add wppa_ajax to query vars
function wppa_ajax_query_vars( $query_vars ) {
$query_vars[] = 'wppa_ajax';
return $query_vars;
}
add_filter( 'query_vars', 'wppa_ajax_query_vars' );So, rather than https://betatest.opajaap.nl/wp-admin/admin-ajax.php we can have https://betatest.opajaap.nl/wppaajax/ as url.
This works almost always…. except:
when an other plugin assumes there is consustency in the execution of the admin_init hook and the function is_admin().
The following test plugin (2 files) demonstrates the issue:
Filename: demo-bug.php:
<?php
/*
* Plugin Name: Demo bug
* Description: Shows a bug in frontend ajax method
* Version: 1
* Author: J.N. Breetvelt a.k.a. OpaJaap
*/
if ( ! class_exists( 'DemoBugClass' ) ) {
class DemoBugClass {
function __construct() {
$this->includes();
}
function includes() {
if ( is_admin() ) {
require_once( dirname( __FILE__ ) . '/demo-bug-functions.php' );
}
}
}
global $DenoBug;
$DemoBug = new DemoBugClass();
}
add_action( 'admin_init', 'bug_test_function' );Filename: demo-bug-functions.php:
<?php
function bug_test_function() {
// Do nothing
}When this plugin is activated and we use the redirect ajax method, we get the following fatal error:
PHP Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, function “bug_test_function” not found or invalid function name in /mnt/web208/b3/15/51893315/htdocs/opajaap/betatest/wp-includes/class-wp-hook.php:324
When this plugin is active and we use the admin-ajax.php method, it works withoput error.
So far, i noticed that a similar error is generated under these conditions by pluguns Disable Gutenberg and WPForms Lite. This is the reason for this topic.
these are my question:
1. Do i do something wrong, and if so how can i fix this?
2. Should not the conflicting plugins load the php files that containg the missing functions unconditionally? I proposed this to the Disable Gutenberg boys, bu they do not want to do that.
3. Is this a WP bug (is_admin() returns false because of not or too late defining WP_ADMIN )?
