wp_ajax_image_editor

The timeline below displays how wordpress function wp_ajax_image_editor has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Handles image editing via AJAX.
 *
 * @since 3.1.0
 */
function wp_ajax_image_editor()
{
    $attachment_id = (int) $_POST['postid'];
    if (empty($attachment_id) || !current_user_can('edit_post', $attachment_id)) {
        wp_die(-1);
    }
    check_ajax_referer("image_editor-{$attachment_id}");
    require_once ABSPATH . 'wp-admin/includes/image-edit.php';
    $msg = false;
    switch ($_POST['do']) {
        case 'save':
            $msg = wp_save_image($attachment_id);
            if (!empty($msg->error)) {
                wp_send_json_error($msg);
            }
            wp_send_json_success($msg);
            break;
        case 'scale':
            $msg = wp_save_image($attachment_id);
            break;
        case 'restore':
            $msg = wp_restore_image($attachment_id);
            break;
    }
    ob_start();
    wp_image_editor($attachment_id, $msg);
    $html = ob_get_clean();
    if (!empty($msg->error)) {
        wp_send_json_error(array('message' => $msg, 'html' => $html));
    }
    wp_send_json_success(array('message' => $msg, 'html' => $html));
}

WordPress Version: 5.6

/**
 * Ajax handler for image editing.
 *
 * @since 3.1.0
 */
function wp_ajax_image_editor()
{
    $attachment_id = (int) $_POST['postid'];
    if (empty($attachment_id) || !current_user_can('edit_post', $attachment_id)) {
        wp_die(-1);
    }
    check_ajax_referer("image_editor-{$attachment_id}");
    include_once ABSPATH . 'wp-admin/includes/image-edit.php';
    $msg = false;
    switch ($_POST['do']) {
        case 'save':
            $msg = wp_save_image($attachment_id);
            if (!empty($msg->error)) {
                wp_send_json_error($msg);
            }
            wp_send_json_success($msg);
            break;
        case 'scale':
            $msg = wp_save_image($attachment_id);
            break;
        case 'restore':
            $msg = wp_restore_image($attachment_id);
            break;
    }
    ob_start();
    wp_image_editor($attachment_id, $msg);
    $html = ob_get_clean();
    if (!empty($msg->error)) {
        wp_send_json_error(array('message' => $msg, 'html' => $html));
    }
    wp_send_json_success(array('message' => $msg, 'html' => $html));
}

WordPress Version: .10

/**
 * Ajax handler for image editing.
 *
 * @since 3.1.0
 */
function wp_ajax_image_editor()
{
    $attachment_id = intval($_POST['postid']);
    if (empty($attachment_id) || !current_user_can('edit_post', $attachment_id)) {
        wp_die(-1);
    }
    check_ajax_referer("image_editor-{$attachment_id}");
    include_once ABSPATH . 'wp-admin/includes/image-edit.php';
    $msg = false;
    switch ($_POST['do']) {
        case 'save':
            $msg = wp_save_image($attachment_id);
            if (!empty($msg->error)) {
                wp_send_json_error($msg);
            }
            wp_send_json_success($msg);
            break;
        case 'scale':
            $msg = wp_save_image($attachment_id);
            break;
        case 'restore':
            $msg = wp_restore_image($attachment_id);
            break;
    }
    ob_start();
    wp_image_editor($attachment_id, $msg);
    $html = ob_get_clean();
    if (!empty($msg->error)) {
        wp_send_json_error(array('message' => $msg, 'html' => $html));
    }
    wp_send_json_success(array('message' => $msg, 'html' => $html));
}

WordPress Version: 5.5

/**
 * Ajax handler for image editing.
 *
 * @since 3.1.0
 */
function wp_ajax_image_editor()
{
    $attachment_id = intval($_POST['postid']);
    if (empty($attachment_id) || !current_user_can('edit_post', $attachment_id)) {
        wp_die(-1);
    }
    check_ajax_referer("image_editor-{$attachment_id}");
    include_once ABSPATH . 'wp-admin/includes/image-edit.php';
    $msg = false;
    switch ($_POST['do']) {
        case 'save':
            $msg = wp_save_image($attachment_id);
            if ($msg->error) {
                wp_send_json_error($msg);
            }
            wp_send_json_success($msg);
            break;
        case 'scale':
            $msg = wp_save_image($attachment_id);
            break;
        case 'restore':
            $msg = wp_restore_image($attachment_id);
            break;
    }
    ob_start();
    wp_image_editor($attachment_id, $msg);
    $html = ob_get_clean();
    if ($msg->error) {
        wp_send_json_error(array('message' => $msg, 'html' => $html));
    }
    wp_send_json_success(array('message' => $msg, 'html' => $html));
}

WordPress Version: 4.1

/**
 * Ajax handler for image editing.
 *
 * @since 3.1.0
 */
function wp_ajax_image_editor()
{
    $attachment_id = intval($_POST['postid']);
    if (empty($attachment_id) || !current_user_can('edit_post', $attachment_id)) {
        wp_die(-1);
    }
    check_ajax_referer("image_editor-{$attachment_id}");
    include_once ABSPATH . 'wp-admin/includes/image-edit.php';
    $msg = false;
    switch ($_POST['do']) {
        case 'save':
            $msg = wp_save_image($attachment_id);
            $msg = wp_json_encode($msg);
            wp_die($msg);
            break;
        case 'scale':
            $msg = wp_save_image($attachment_id);
            break;
        case 'restore':
            $msg = wp_restore_image($attachment_id);
            break;
    }
    wp_image_editor($attachment_id, $msg);
    wp_die();
}

WordPress Version: 4.0

/**
 * Ajax handler for image editing.
 *
 * @since 3.1.0
 */
function wp_ajax_image_editor()
{
    $attachment_id = intval($_POST['postid']);
    if (empty($attachment_id) || !current_user_can('edit_post', $attachment_id)) {
        wp_die(-1);
    }
    check_ajax_referer("image_editor-{$attachment_id}");
    include_once ABSPATH . 'wp-admin/includes/image-edit.php';
    $msg = false;
    switch ($_POST['do']) {
        case 'save':
            $msg = wp_save_image($attachment_id);
            $msg = json_encode($msg);
            wp_die($msg);
            break;
        case 'scale':
            $msg = wp_save_image($attachment_id);
            break;
        case 'restore':
            $msg = wp_restore_image($attachment_id);
            break;
    }
    wp_image_editor($attachment_id, $msg);
    wp_die();
}

WordPress Version: 3.7

function wp_ajax_image_editor()
{
    $attachment_id = intval($_POST['postid']);
    if (empty($attachment_id) || !current_user_can('edit_post', $attachment_id)) {
        wp_die(-1);
    }
    check_ajax_referer("image_editor-{$attachment_id}");
    include_once ABSPATH . 'wp-admin/includes/image-edit.php';
    $msg = false;
    switch ($_POST['do']) {
        case 'save':
            $msg = wp_save_image($attachment_id);
            $msg = json_encode($msg);
            wp_die($msg);
            break;
        case 'scale':
            $msg = wp_save_image($attachment_id);
            break;
        case 'restore':
            $msg = wp_restore_image($attachment_id);
            break;
    }
    wp_image_editor($attachment_id, $msg);
    wp_die();
}