@@ -321,6 +321,9 @@ protected function get_all_items() {
321321 * [--network]
322322 * : If set, the plugin will be activated for the entire multisite network.
323323 *
324+ * [--force]
325+ * : If set, deactivates and reactivates the plugin to re-run activation hooks, even if already active.
326+ *
324327 * ## EXAMPLES
325328 *
326329 * # Activate plugin
@@ -345,13 +348,19 @@ protected function get_all_items() {
345348 * Plugin 'buddypress' network activated.
346349 * Success: Activated 2 of 2 plugins.
347350 *
351+ * # Force re-running activation hooks for an already active plugin.
352+ * $ wp plugin activate hello --force
353+ * Plugin 'hello' activated.
354+ * Success: Activated 1 of 1 plugins.
355+ *
348356 * @param array $args
349357 * @param array $assoc_args
350358 */
351359 public function activate ( $ args , $ assoc_args = [] ) {
352360 $ network_wide = Utils \get_flag_value ( $ assoc_args , 'network ' , false );
353361 $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
354362 $ all_exclude = Utils \get_flag_value ( $ assoc_args , 'exclude ' , '' );
363+ $ force = Utils \get_flag_value ( $ assoc_args , 'force ' , false );
355364
356365 /**
357366 * @var string $all_exclude
@@ -374,18 +383,28 @@ public function activate( $args, $assoc_args = [] ) {
374383 }
375384 foreach ( $ plugins as $ plugin ) {
376385 $ status = $ this ->get_status ( $ plugin ->file );
377- if ( $ all && in_array ( $ status , [ 'active ' , 'active-network ' ], true ) ) {
386+ if ( $ all && ! $ force && in_array ( $ status , [ 'active ' , 'active-network ' ], true ) ) {
378387 continue ;
379388 }
380389 // Network-active is the highest level of activation status.
381390 if ( 'active-network ' === $ status ) {
382- WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already network active. " );
383- continue ;
391+ // If force flag is set, deactivate and reactivate to run activation hooks.
392+ if ( $ force ) {
393+ deactivate_plugins ( $ plugin ->file , false , true );
394+ } else {
395+ WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already network active. " );
396+ continue ;
397+ }
384398 }
385399 // Don't reactivate active plugins, but do let them become network-active.
386400 if ( ! $ network_wide && 'active ' === $ status ) {
387- WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already active. " );
388- continue ;
401+ // If force flag is set, deactivate and reactivate to run activation hooks.
402+ if ( $ force ) {
403+ deactivate_plugins ( $ plugin ->file , false , false );
404+ } else {
405+ WP_CLI ::warning ( "Plugin ' {$ plugin ->name }' is already active. " );
406+ continue ;
407+ }
389408 }
390409
391410 // Plugins need to be deactivated before being network activated.
0 commit comments