wp_ajax_get_attachment

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

WordPress Version: 6.3

/**
 * Handles getting an attachment via AJAX.
 *
 * @since 3.5.0
 */
function wp_ajax_get_attachment()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    $id = absint($_REQUEST['id']);
    if (!$id) {
        wp_send_json_error();
    }
    $post = get_post($id);
    if (!$post) {
        wp_send_json_error();
    }
    if ('attachment' !== $post->post_type) {
        wp_send_json_error();
    }
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $attachment = wp_prepare_attachment_for_js($id);
    if (!$attachment) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 5.5

/**
 * Ajax handler for getting an attachment.
 *
 * @since 3.5.0
 */
function wp_ajax_get_attachment()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    $id = absint($_REQUEST['id']);
    if (!$id) {
        wp_send_json_error();
    }
    $post = get_post($id);
    if (!$post) {
        wp_send_json_error();
    }
    if ('attachment' !== $post->post_type) {
        wp_send_json_error();
    }
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $attachment = wp_prepare_attachment_for_js($id);
    if (!$attachment) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 5.3

/**
 * Ajax handler for getting an attachment.
 *
 * @since 3.5.0
 */
function wp_ajax_get_attachment()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    $id = absint($_REQUEST['id']);
    if (!$id) {
        wp_send_json_error();
    }
    $post = get_post($id);
    if (!$post) {
        wp_send_json_error();
    }
    if ('attachment' != $post->post_type) {
        wp_send_json_error();
    }
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    $attachment = wp_prepare_attachment_for_js($id);
    if (!$attachment) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 4.0

/**
 * Ajax handler for getting an attachment.
 *
 * @since 3.5.0
 */
function wp_ajax_get_attachment()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (!$id = absint($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (!$post = get_post($id)) {
        wp_send_json_error();
    }
    if ('attachment' != $post->post_type) {
        wp_send_json_error();
    }
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    if (!$attachment = wp_prepare_attachment_for_js($id)) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 3.7

/**
 * Get an attachment.
 *
 * @since 3.5.0
 */
function wp_ajax_get_attachment()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (!$id = absint($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (!$post = get_post($id)) {
        wp_send_json_error();
    }
    if ('attachment' != $post->post_type) {
        wp_send_json_error();
    }
    if (!current_user_can('upload_files')) {
        wp_send_json_error();
    }
    if (!$attachment = wp_prepare_attachment_for_js($id)) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}