Title: wp_ajax_activate_plugin
Published: April 3, 2024
Last modified: February 24, 2026

---

# wp_ajax_activate_plugin()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_activate_plugin/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_activate_plugin/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_activate_plugin/?output_format=md#changelog)

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

Handles activating a plugin via AJAX.

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

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

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

    	$status = array(
    		'activate'   => 'plugin',
    		'slug'       => wp_unslash( $_POST['slug'] ),
    		'pluginName' => wp_unslash( $_POST['name'] ),
    		'plugin'     => wp_unslash( $_POST['plugin'] ),
    	);

    	if ( ! current_user_can( 'activate_plugin', $status['plugin'] ) ) {
    		$status['errorMessage'] = __( 'Sorry, you are not allowed to activate plugins on this site.' );
    		wp_send_json_error( $status );
    	}

    	if ( is_plugin_active( $status['plugin'] ) ) {
    		$status['errorMessage'] = sprintf(
    			/* translators: %s: Plugin name. */
    			__( '%s is already active.' ),
    			$status['pluginName']
    		);
    	}

    	$activated = activate_plugin( $status['plugin'] );

    	if ( is_wp_error( $activated ) ) {
    		$status['errorMessage'] = $activated->get_error_message();
    		wp_send_json_error( $status );
    	}

    	wp_send_json_success( $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#L4557)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ajax-actions.php#L4557-L4600)

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

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

Determines whether a plugin is active.

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

Attempts activation of plugin in a “sandbox” and redirects on success.

  | 
| [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.

  | 
| [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.

  | 
| [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 7 more](https://developer.wordpress.org/reference/functions/wp_ajax_activate_plugin/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_activate_plugin/?output_format=md#)

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

| Version | Description | 
| [6.5.0](https://developer.wordpress.org/reference/since/6.5.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_activate_plugin%2F)
before being able to contribute a note or feedback.