• Hi

    I am creating a dashboard with overview and data from my different WP sites.
    I am therefore making an AJAX request to the different site’s admin.php file (as this is how I think I can get the data).
    But I get an 400 error msg returned, I have no idea if this is possible through another website. But I managed to get the data from a Postman request 🙂 My request looks like this:

    export async function getWordFenceData() {
    
      const form = new FormData();
    
      form.append('lastcttime', 'time');
      form.append('lastissuetime', 'time');
      form.append('action', 'wordfence_activityLogUpdate');
      form.append('nonce', 'nonceString');
    
      const response = await axios.post('DomainOfSite/wp-admin/admin-ajax.php', form);
    
      return response.data;
    } 

    And on the requested server I added this in the functions.php file :

    add_action('init', 'handle_preflight');
    
    function handle_preflight() {
        $origin = get_http_origin();
    
        if ($origin === 'DOMAIN') {
            header("Access-Control-Allow-Origin: *");
    
            header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
    
            header("Access-Control-Allow-Credentials: true");
    
            header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
    
            if ('OPTIONS' == $_SERVER['REQUEST_METHOD']) {
                status_header(200);
                exit();
            }
        }
    }
    
    add_filter('rest_authentication_errors', 'rest_filter_incoming_connections');
    
    function rest_filter_incoming_connections($errors) {
        $request_server = $_SERVER['REMOTE_ADDR'];
    
        $origin = get_http_origin();
    
        if ($origin !== 'DOMAIN') return new WP_Error('forbidden_access', $origin, array(
            'status' => 403
        ));
    
        return $errors;
    }

    I’ve no idea if this is possible, so please help me here 😉

    • Dette emne blev ændret 3 år, 11 måneder siden af ayice.
  • The topic ‘Post request to admin.php file from another WP site’ is closed to new replies.