Title: wp_ajax_update_plugin
Published: April 23, 2015
Last modified: February 24, 2026

---

# wp_ajax_update_plugin()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#see-also)
 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#wp--skip-link--target)

Handles updating a plugin via AJAX.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#see-also)󠁿

 * [Plugin_Upgrader](https://developer.wordpress.org/reference/classes/plugin_upgrader/)

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#source)󠁿

    ```php
    function wp_ajax_update_plugin() {
    	check_ajax_referer( 'updates' );

    	if ( empty( $_POST['plugin'] ) || empty( $_POST['slug'] ) ) {
    		wp_send_json_error(
    			array(
    				'slug'         => '',
    				'errorCode'    => 'no_plugin_specified',
    				'errorMessage' => __( 'No plugin specified.' ),
    			)
    		);
    	}

    	$plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );

    	$status = array(
    		'update'     => 'plugin',
    		'slug'       => sanitize_key( wp_unslash( $_POST['slug'] ) ),
    		'oldVersion' => '',
    		'newVersion' => '',
    	);

    	if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) {
    		$status['errorMessage'] = __( 'Sorry, you are not allowed to update plugins for this site.' );
    		wp_send_json_error( $status );
    	}

    	$plugin_data          = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
    	$status['plugin']     = $plugin;
    	$status['pluginName'] = $plugin_data['Name'];

    	if ( $plugin_data['Version'] ) {
    		/* translators: %s: Plugin version. */
    		$status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
    	}

    	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

    	wp_update_plugins();

    	$skin     = new WP_Ajax_Upgrader_Skin();
    	$upgrader = new Plugin_Upgrader( $skin );
    	$result   = $upgrader->bulk_upgrade( array( $plugin ) );

    	if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
    		$status['debug'] = $skin->get_upgrade_messages();
    	}

    	if ( is_wp_error( $skin->result ) ) {
    		$status['errorCode']    = $skin->result->get_error_code();
    		$status['errorMessage'] = $skin->result->get_error_message();
    		wp_send_json_error( $status );
    	} elseif ( $skin->get_errors()->has_errors() ) {
    		$status['errorMessage'] = $skin->get_error_messages();
    		wp_send_json_error( $status );
    	} elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {

    		/*
    		 * Plugin is already at the latest version.
    		 *
    		 * This may also be the return value if the `update_plugins` site transient is empty,
    		 * e.g. when you update two plugins in quick succession before the transient repopulates.
    		 *
    		 * Preferably something can be done to ensure `update_plugins` isn't empty.
    		 * For now, surface some sort of error here.
    		 */
    		if ( true === $result[ $plugin ] ) {
    			$status['errorMessage'] = $upgrader->strings['up_to_date'];
    			wp_send_json_error( $status );
    		}

    		$plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] );
    		$plugin_data = reset( $plugin_data );

    		if ( $plugin_data['Version'] ) {
    			/* translators: %s: Plugin version. */
    			$status['newVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
    		}

    		wp_send_json_success( $status );
    	} elseif ( false === $result ) {
    		global $wp_filesystem;

    		$status['errorCode']    = 'unable_to_connect_to_filesystem';
    		$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );

    		// Pass through the error from WP_Filesystem if one was raised.
    		if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    			$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    		}

    		wp_send_json_error( $status );
    	}

    	// An unhandled error occurred.
    	$status['errorMessage'] = __( 'Plugin update failed.' );
    	wp_send_json_error( $status );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ajax-actions.php#L4611)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ajax-actions.php#L4611-L4708)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Ajax_Upgrader_Skin::__construct()](https://developer.wordpress.org/reference/classes/wp_ajax_upgrader_skin/__construct/)`wp-admin/includes/class-wp-ajax-upgrader-skin.php` |

Constructor.

  | 
| [get_plugin_data()](https://developer.wordpress.org/reference/functions/get_plugin_data/)`wp-admin/includes/plugin.php` |

Parses the plugin contents to retrieve plugin’s metadata.

  | 
| [get_plugins()](https://developer.wordpress.org/reference/functions/get_plugins/)`wp-admin/includes/plugin.php` |

Checks the plugins directory and retrieve all plugin files with plugin data.

  | 
| [validate_file()](https://developer.wordpress.org/reference/functions/validate_file/)`wp-includes/functions.php` |

Validates a file name and path against an allowed set of rules.

  | 
| [wp_update_plugins()](https://developer.wordpress.org/reference/functions/wp_update_plugins/)`wp-includes/update.php` |

Checks for available updates to plugins based on the latest versions hosted on WordPress.org.

  | 
| [plugin_basename()](https://developer.wordpress.org/reference/functions/plugin_basename/)`wp-includes/plugin.php` |

Gets the basename of a plugin.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [sanitize_text_field()](https://developer.wordpress.org/reference/functions/sanitize_text_field/)`wp-includes/formatting.php` |

Sanitizes a string from user input or from the database.

  | 
| [wp_unslash()](https://developer.wordpress.org/reference/functions/wp_unslash/)`wp-includes/formatting.php` |

Removes slashes from a string or recursively removes slashes from strings within an array.

  | 
| [esc_html()](https://developer.wordpress.org/reference/functions/esc_html/)`wp-includes/formatting.php` |

Escaping for HTML blocks.

  | 
| [sanitize_key()](https://developer.wordpress.org/reference/functions/sanitize_key/)`wp-includes/formatting.php` |

Sanitizes a string key.

  | 
| [check_ajax_referer()](https://developer.wordpress.org/reference/functions/check_ajax_referer/)`wp-includes/pluggable.php` |

Verifies the Ajax request to prevent processing requests external of the blog.

  | 
| [wp_send_json_error()](https://developer.wordpress.org/reference/functions/wp_send_json_error/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating failure.

  | 
| [wp_send_json_success()](https://developer.wordpress.org/reference/functions/wp_send_json_success/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating success.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 11 more](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_update_plugin/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.2.0](https://developer.wordpress.org/reference/since/4.2.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_update_plugin%2F)
before being able to contribute a note or feedback.