Hot answers tagged

24

Within the php/wp-cli.php we find these lines: // Can be used by plugins/themes to check if WP-CLI is running or not define( 'WP_CLI', true ); define( 'WP_CLI_VERSION', trim( file_get_contents( WP_CLI_ROOT . '/VERSION' ) ) ); define( 'WP_CLI_START_MICROTIME', microtime( true ) ); so you could check if WP_CLI or WP_CLI_VERSION are defined.


24

The canonical check for WP-CLI used in the majority of plugins and specifically mentioned in the docs is to check WP_CLI is defined and set to true: if ( defined( 'WP_CLI' ) && WP_CLI ) { // Do WP-CLI-specific things. }


19

Generally, plugins have some functionality hooked onto the deactivation action. This could be clearing cache, resetting options, you name it. Therefore the best practice is to deactivate them first, so they have the opportunity to clean up and execute whatever functionality they have hooked onto the deactivate event. Now if the plugin is broken and can't ...


17

This does the trick: wp user update USERNAME --user_pass="PASSWORD" (Found it here.)


17

Go into your wp-config.php and change your DB_HOST to 127.0.0.1 instead of localhost. Credit goes to Craig Wayne above in the comments.


14

You can use the skip-plugins option in WP-CLI to not load individual plugins when using WP-CLI. You can either use it in command like this: wp user list --skip-plugins=my-plugin Or you can add this to your wp-cli.yml file: skip-plugins: - my-plugin


12

I think the best way is to use WP-CLI but you'd need to write a bash script to do this. Here is one that should do it for you: WP_PATH="/path/to/wp" for SITE_URL in = $(wp site list --fields=domain,path,archived,deleted --format=csv --path="$WP_PATH" | grep ",0,0$" | awk -F ',' '{print $1 $2}') do for EVENT_HOOK in $(wp cron event list --format=csv --...


11

After you've added the constant in wp-config.php defined('DISABLE_WP_CRON') or define('DISABLE_WP_CRON', true); WP-CLI And assuming you have your config.yml setup correctly, you can ommit the --path flag when calling cron run. wp cron event run --due-now [<hook>…] One or more hooks to run. [--due-now] Run all hooks due right now. [-...


11

Solved it myself, this is how to do it: If you want to programatically update serialized options: download wp-cli from wp-cli.org Find out what is the "key" for the options you are using. In this example the key is "wp_smtp_options" If you don't know the key, search in wp_options table and try to figure it out. Example: select * from wp_options where ...


11

One of the first things WordPress does to load plugins is get the active plugins as saved in the database: $active_plugins = (array) get_option( 'active_plugins', array() ); Since it uses get_option() we can use the option_active_plugins filter to modify the list of active plugins on the fly. function wpse_301282_disable_plugin( $active_plugins ) { if ...


9

From the WP-CLI documentation about wp post delete: wp post delete $(wp post list --post_type='attachment' --format=ids) See wp post list for additional information.


9

Not allowed by design: If we try to change the user login by the email: wp user update mary@example.tld --user_login=mary_new or by the user id: wp user update 123 --user_login=mary_new we get the following warning: User logins can't be changed. This is the reason: if ( isset( $assoc_args['user_login'] ) ) { WP_CLI::warning( "User logins can't ...


9

WP-CLI is definitely the answer to this after the update to 1.4.0 which introduced the pluck and patch commands for accessing serialized data in WordPress. The pluck command takes this format for grabbing serialized values wp option pluck <key> <key-name> For example in the active_plugins option you can grab first item wp option pluck ...


9

You can enable maintenance mode in WordPress by adding a .maintenance file to your root WordPress directory. It will need to include: <?php $upgrading = time(); With this file in place, your site will be in maintenance mode until you remove the file.


9

This should delete all posts in your category: wp post delete $(wp post list --cat=your_category_ID --format=ids) Or directly: wp db query [<your_sql_query>] For more info: wp post delete --help wp post list --help wp db query --help


6

I have been working on a simple and pluggable command line interface. The basic work is done but we now need to start writing more commands (and implement it in plugins). Please feel free to fork and contribute! You can find the code on Github.


6

TL;DR: Run wp version --debug Check that wp-cli.yml was found Check that the ABSPATH set by WP-CLI matches the directory where WP is installed Look at find_wp_root() for hints about why it's finding the wrong folder The docs say: inside the current working directory (or upwards) ...so you should be able to use it in parent directories, not just in the ...


6

search-replace does the trick but can have undesired side effects if old_login appears in other contexts in the database: wp search-replace old_login new_login But before doing that run wp sql dump wp search-replace old_login new_login --dry-run To make an SQL dump and see what is going to be replaced.


6

Here is how I do it in a bash script: wp option get wp_smtp_options --format=json | php -r ' $var = json_decode( fgets(STDIN) ); $var->from = "email@email.com"; $var->fromname = "me"; print json_encode($var); ' | wp option set wp_smtp_options --format=json


6

WP-CLI now has native commands for it. # Activate Maintenance mode $ wp maintenance-mode activate # Deactivate Maintenance mode $ wp maintenance-mode deactivate See wp-cli/maintenance-mode-command for more information.


6

Since WP-CLI 0.24.0 you can now use aliases which enable you to import a remote database quite easily. By using aliases, you can run WP-CLI commands against another WP-CLI install. That install could be a remote machine. With this in mind I've hacked together a bash alias that chains together several WP-CLI commands to pull a remote WP database into a ...


6

Thanks to a tip from milo in the comments above, I looked at this similar question. There is an answer provided by Laurent which basically gets the option using wp-cli, pipes it to an inline php program, makes the adjustments and then pipes it back to wp-cli. I took that idea and generalized it somewhat by creating a file sibling to my cloning script: ...


6

I was able to duplicate the issue when running WP-CLI version 1.1.0, but the command worked successfully when I updated to the nightly build using: wp cli update --nightly The --image-size parameter was added to wp-cli/media-command on April 13, 2017 after the current stable release of WP-CLI v1.1.0 on February 1, 2017. This feature is available in the ...


6

Here is an untested suggestion for wp-cli approach: We can list post IDs of published posts with open comment status with: wp post list --post-status=publish --post_type=post comment_status=open --format=ids and update a post to a closed comment status with: wp post update 123 --comment_status=closed where 123 is a post id. We can then combine those ...


5

The main problem is that your wp_reset_init() method deals with too many things. It's looking at the $_POST request, it's performing database queries, and it's performing redirects. You need to separate your concerns so you end up with a new method which only performs the database reset, and which accepts parameters rather than using $_POST directly. You ...


5

Since the issue is probably DB configuration you could try adjusting it conditionally in wp-config.php: if ( defined( 'WP_CLI' ) ) { define( 'DB_HOST', 'example.com' ); } else { define( 'DB_HOST', 'localhost' ); }


5

For regular users, the preferred update method is pressing the "Update" button in wp-admin. For sysadmins, wp core update + wp core update-db will probably be a lot faster. Note that for multisite instances, you'll have to update the DB for each site in the network. See https://github.com/wp-cli/wp-cli/issues/683 NB: I might be biased, since I'm currently ...


5

Yes, of course it does, as long as you also have SSH access to the instance (it won't work over FTP). There's nothing special about WordPress instances installed via WP-CLI. Commands such as wp core download and wp core install perform the same actions you would normally do when installing manually.


5

It's as if you were trying to run: wp plugin status --path=$(/home/me/domains/example.com/public_html) because what's inside the backticks get's evaluated. Here's a good reading about using backticks in the command line. Let me quote @rozcietrzewiacz: Backtick is not a quotation sign, it has a very special meaning. Everything you type between ...


5

Cloning a post via wp-cli is little trickier. It needs two steps: Create a file where save the information of post. Suppose, following command create a file named file.txt from hello post (id 1). In this case file.txt which save on root directory. wp post get 1 > file.txt Create new post from this file. In our scenario file.txt saved all information of ...


Only top voted, non community-wiki answers of a minimum length are eligible