@@ -322,6 +322,9 @@ protected function get_all_items() {
322322 * [--network]
323323 * : If set, the plugin will be activated for the entire multisite network.
324324 *
325+ * [--force]
326+ * : If set, deactivates and reactivates the plugin to re-run activation hooks, even if already active.
327+ *
325328 * ## EXAMPLES
326329 *
327330 * # Activate plugin
@@ -346,13 +349,19 @@ protected function get_all_items() {
346349 * Plugin 'buddypress' network activated.
347350 * Success: Activated 2 of 2 plugins.
348351 *
352+ * # Force re-running activation hooks for an already active plugin.
353+ * $ wp plugin activate hello --force
354+ * Plugin 'hello' activated.
355+ * Success: Activated 1 of 1 plugins.
356+ *
349357 * @param array $args
350358 * @param array $assoc_args
351359 */
352360 public function activate ( $ args , $ assoc_args = [] ) {
353361 $ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' , false );
354362 $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
355363 $ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , '' );
364+ $ force = Utils \get_flag_value ( $ assoc_args , 'force ' , false );
356365
357366 /**
358367 * @var string $all_exclude
@@ -375,18 +384,28 @@ public function activate( $args, $assoc_args = [] ) {
375384 }
376385 foreach ( $ plugins as $ plugin ) {
377386 $ status = $ this ->get_status ( $ plugin ->file );
378- if ( $ all && in_array ( $ status , [ 'active ' , 'active-network ' ], true ) ) {
387+ if ( $ all && ! $ force && in_array ( $ status , [ 'active ' , 'active-network ' ], true ) ) {
379388 continue ;
380389 }
381390 // Network-active is the highest level of activation status.
382391 if ( 'active-network ' === $ status ) {
383- WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already network active. " );
384- continue ;
392+ // If force flag is set, deactivate and reactivate to run activation hooks.
393+ if ( $ force ) {
394+ deactivate_plugins ( $ plugin ->file , false , true );
395+ } else {
396+ WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already network active. " );
397+ continue ;
398+ }
385399 }
386400 // Don't reactivate active plugins, but do let them become network-active.
387401 if ( ! $ network_wide && 'active ' === $ status ) {
388- WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already active. " );
389- continue ;
402+ // If force flag is set, deactivate and reactivate to run activation hooks.
403+ if ( $ force ) {
404+ deactivate_plugins ( $ plugin ->file , false , false );
405+ } else {
406+ WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already active. " );
407+ continue ;
408+ }
390409 }
391410
392411 // Plugins need to be deactivated before being network activated.
0 commit comments