Tag: wp cli

  • LocalWP and Homebrew Conflicts

    Like an idiot while I was going to do a “quick” bit of work to get ahead for next week on Friday. First I decided it was time to update Homebrew before I really got into diagnosing a problem for a client. Of course mayhem ensued with the wpcli commands no longer working with LocalWP after I ran brew update && brew upgrade.

    There were two main errors to search. First the icu4u library wasn’t loaded. This library is used for unicode characters and is a requirement of PHP, MySQL and a number of other things that Homebrew was managing. The specific error is below.

    Library not loaded: /opt/homebrew/opt/icu4c/lib/libicuio.73.dylib

    Further down in the same error block there was a note about PHP 8.0 not being installed.

    Error: php@8.0 has been disabled because it is a versioned formula!

    After searching and finding these general directions to remove and reinstall LocalWP, I was still out of luck. Worse, I no longer had any development sites installed locally as I removed them all during the uninstall process.

    Fixing with Homebrew

    Ultimately after a bunch of fruitless searching I came across this article saying that PHP 8.0 was no longer supported in Homebrew. To continue to have it installed I needed to get it from a different repository with the following commands.

    • Add the new repository to Homebrew – brew tap shivammathur/php
    • install php 8.0 – brew install shivammathur/php/php8.0

    Now my wpcli commands work as expected and I no longer see the errors from icu4c. The big problem with this, and anything installed with Homebrew, is that we’re messing with my main PHP version in my shell. I’m always running PHP 8.0 now, even on projects that may need earlier or later versions of PHP.

    Fixing it with nix-shell

    Of course you’ll need to install nix-shell first with these instructions.

    A far more portable and repeatable way to fix my issue is to use nix-shell. Instead of messing with the main versions of software running on my machine, I’m only messing with the current instance of the terminal that I’m working in.

    The easy way to test this is to run the following command which temporarily installs php8.1 and wpcli in my terminal.

    nix-shell -p php81 wp-cli
    

    You’ll still get an error connecting to the database because the shell doesn’t understand how to make the connection to LocalWP but that can be fixed by going to the Database tab in LocalWP and copying the socket value into the DB_HOST value. It should look something like this:

    define( 'DB_HOST', 'localhost:/Users/curtismchale/Library/Application Support/Local/run/CiNKEaTuy/mysql/mysqld.sock' );
    

    Now when I run a wp-cli command I have a connection which means the next step is to create a shell.nix file I can place in the root directory of my project which has the values I need setup already so I don’t have to manually run them each time. This is what my shell.nix file looks like.

    let
      nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
      pkgs = import nixpkgs { config = {}; overlays = []; };
    in
    
    pkgs.mkShellNoCC {
      packages = with pkgs; [
        wp-cli
    	php81
      ];
    }
    

    Then when I open up my terminal I run nix-shell and the current shell is setup with php81 and wp-cli every time. I can move the same shell.nix file to a new site, or a new computer and run nix-shell and I’ll get the same environment in my shell every time.

  • Enable Specific PHP Version in VVV for WP CLI

    Out of the box Varying Vagrant Vagrants lets you define a PHP version to use for each site you’re adding to the configuration file. What this doesn’t do is change the PHP version that WP CLI uses when you use vagrant ssh to log into the sites you’ve just created. Unless you take an extra step WP CLI will continue to use the default PHP version for VVV.

    Installing Other PHP Versions

    In your ~/vvv-local/config/config.php file you’ll see a section that looks like this.

    # Extensions https://varyingvagrantvagrants.org/docs/en-US/utilities/
    # are system level items that aren't websites, that install tools or packages
    # the core extensions install tools such as phpmyadmin or new PHP versions
    #
    # these used to be called utilities but people kept requesting
    # extensions not realising so it was renamed
    extensions:
      core: # The core VVV extensions
        - tls-ca # HTTPS SSL/TLS certificates
        - phpmyadmin # Web based database client
        #- memcached-admin # Object cache management
        #- opcache-status # opcache management
        #- webgrind # PHP Debugging
        #- mongodb # needed for Tideways/XHGui
        #- tideways # PHP profiling tool, also installs xhgui check https://varyingvagrantvagrants.org/docs/en-US/references/tideways-xhgui/
        #- nvm # Node Version Manager
        #- php56
        #- php70
        #- php71
        #- php72
        #- php73
        #- php74
        - php80
        - php81
        - php82
    

    Note at the bottom of the section of code I’ve removed the comments on PHP 8.0, 8.1, and 8.2. By doing this I’ve asked VVV to install those versions of PHP when it provisions.

    Setting PHP Version for WP CLI

    Now we need to log into VVV with vagrant ssh. To see which versions of PHP you have available navigate to /usr/bin. Then I usually type php and hit the tab key twice to try and autocomplete the command. Since there are many things starting with php terminal shows me a bunch of options.

    Now I need to set the version of PHP I want to use with sudo update-alternatives --set php /usr/bin/php8.0 if I want to use 8.0. This sets the default php call to the version of PHP you want to use for a site.

    Now in the documentation for changing PHP versions in VVV there is a troubleshooting section at the bottom that talks about changing the value in vvv.nginx.conf file, but as with much documenation…it doesn’t tell you where that file is and I can’t find it. Ideally I’d change the default PHP version to 8.2 as it’s the most current version, but after looking through the docs a few times…there aren’t clear instructions on it.

    If I do figure this out, I’ll update the documentation so that everyone can benefit from clear obvious documentation.

  • Deleting 20,000 Spam WordPress Users with WP CLI

    While doing a site inspection I noticed that we had over 20,000 spam subscribers on the site with addresses that lead to telegram. The WordPress admin is a terrible way to try and delete users when you have this many to do, so we turned to wp cli.

    First I noted that the site couldn’t delete all the users at once because the command would time out so I used wp list user --role=subscriber --number=10 --field=ID to list 10 users and then increased the number of returned results till I was able to retrieve 5,000 users at once. Then it was a matter of combining this with wp delete user to delete the listed users. Then I ran the command a few times to clean up the site.

    Our final command looked like this.

    wp user delete $(wp user list --role=subscriber --field=ID --number=5000) --reassign=1
  • Adding Random Content to WordPress via WP CLI

    Right now I’m working on a Google Analytics 4 integration that needs some data and views. To get the clicks/views I’m using the Selenium IDE Firefox Extension which records my clicks and the replays them. But to even have content for those clicks, I need to generate a bunch of it.

    Enter WP CLI Random Content by Bryan Richards.

    I’ve used two commands so far. wp random generate --count=50 to generate 50 posts. Then wp random generate --count=50 --post_type=question to generate 50 questions for the WP FAQ Manager plugin.

    WP CLI Random Content has far more options than I’ve described above. You can have it add taxonomies to your generated content, use --with-terms=true to have it generate terms first then generate content and attach it to those terms.

    Overall, it’s a one stop shop for generating basic content for testing your WordPress sites.