Created
March 17, 2020 11:20
-
-
Save premanshup/8cee47fdfd1c2ebc7c232976b5ff26b1 to your computer and use it in GitHub Desktop.
Delete Astra single option from multisites.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Command - wp delete-astra-option delete --key={astra-settings key} | |
namespace DeleteAstraOption; | |
use WP_CLI, WP_CLI_Command; | |
if( class_exists( 'WP_CLI_Command' ) ) { | |
class Commands extends WP_CLI_Command { | |
public function delete( $args, $assoc_args ) { | |
$option_key = isset( $assoc_args['key'] ) ? $assoc_args['key'] : false; | |
if( ! $option_key ) { | |
WP_CLI::error( 'Invalid arguments. Please enter vaild Key, ex. --key={key}' ); | |
} | |
// Create progress bar. | |
$progress = \WP_CLI\Utils\make_progress_bar( 'Deleting key and its value from astra-settings ' . $option_key, 0 ); | |
$blog_ids = get_sites(); | |
foreach( $blog_ids as $b ) { | |
switch_to_blog( $b->blog_id ); | |
// Delete astra-settings option one by one. | |
astra_delete_option( $option_key ); | |
WP_CLI::log( sprintf( 'Option %s for Site - %s is deleted!', $option_key, $b->blog_id ) ); | |
restore_current_blog(); | |
$progress->tick(); | |
} | |
$progress->finish(); | |
// Success message. | |
WP_CLI::success( $option_key . ' astra-settings Option deleted!' ); | |
} | |
} | |
WP_CLI::add_command( 'delete-astra-option', 'DeleteAstraOption\Commands' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment