• Im developing a WordPress-plugin, where the main file of the plugins includes a PHP-file depending on (supposed to at least) if you are back-end or front-end.

    As the is_admin() returns true on AJAX requests, I have used the DOING_AJAX constant to check whetever AJAX is done or not:

    if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
        require_once('admin/functions_admin.php'); 
     }
     else { 
        require_once('public/functions_public.php'); 
     }

    The correct file is loaded in wp-admin. The correct file is loaded front-end. Ajax-requests works front-end – but not back end. The “if” is not executed when doing Ajax back-end with this code.

    When adding the following “else if” code it works back-end, but then not frond-end of course:

    else if ( is_admin() ) {
       require_once('admin/functions_admin.php'); 
    }
Viewing 1 replies (of 1 total)
  • Thread Starter pmbs

    (@pmbs)

    Sorted this out by gathering all AJAX-functions (both front- and back-end) in a third file:

    // Is admin, but not doing ajaax
    if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
        require_once('admin/functions_admin.php'); 
    }
    // Is doing AJAX 
    else if ( is_admin() && ( defined( 'DOING_AJAX' ) || DOING_AJAX ) ) {
        require_once('functions_ajax.php'); 
    }
    // Front-end functions
    else { 
    require_once('public/functions_public.php'); 
    }
Viewing 1 replies (of 1 total)

The topic ‘is_admin() and DOING_AJAX in WordPress Plugins’ is closed to new replies.