Hot answers tagged

27

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


27

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.


27

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.


26

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


21

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


16

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


15

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


15

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


15

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


14

first check the user name: wp user list then change password without leaving traces in history wp user update admin --prompt=user_pass


13

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


13

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


12

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


10

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


10

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


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 can now be done via WP-CLI using $ wp post create --from-post=1. It even dupes the meta data.


8

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


7

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


7

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.


7

You can set the php binary that WP-CLI uses by setting an environment variable in your linux shell. export WP_CLI_PHP=/path/to/php5.6


7

Got the same problem! Just switch the php version. On my server PHP5.6 was default for apache, while CLI was configured with PHP7.1. After installing WP-CLI, with wp --info I got this result: PHP binary: /usr/bin/php7.1 PHP version: 7.1.5-1+deb.sury.org~xenial+1 php.ini used: /etc/php/7.1/cli/php.ini WP-CLI root dir: phar://wp-cli.phar And when i used the ...


7

Just figured it out. This isn't clearly or explicitly documented at https://developer.wordpress.org/cli/commands/post/list/ or elsewhere. wp post list --fields=ID,post_title,Amazon.com,Amazon.co.uk --meta_key=Amazon.com --meta_key=Amazon.co.uk


6

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


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

If we restrict us to the output of wp post list, then here's a way to search for %test% within the post titles of published posts: wp post list --ignore_sticky_posts=1 --post__in=$(wp db query 'SELECT ID FROM wp_posts WHERE post_title LIKE "%test%" AND post_status="publish" AND post_type="post"' --skip-column-names | paste -s -d ',' - ) Here's the ...


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

You'll need to wrap your statement within a class_exists() check: function foo_command( $args ) { WP_CLI::success( $args[0] ); } if ( class_exists( 'WP_CLI' ) ) { WP_CLI::add_command( 'foo', 'foo_command' ); } This way, the command is only registered when the WP_CLI class exists — because the WP_CLI class will only exist when WP-CLI is running.


6

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