Category: WP CLI

  • WP-CLI for Developers: Automating WordPress Tasks the Right Way

    If you’re still doing routine WordPress tasks through wp-admin, you’re leaving speed, safety, and scalability on the table.

    For modern WordPress developers, WP-CLI is not optional anymore — it’s the foundation of an automation-first workflow.

    This article explains how and why I use WP-CLI in real projects, not as a tutorial, but as a system.


    Why WP-CLI Changes Everything for Developers

    wp-admin is designed for:

    • site owners
    • editors
    • one-off actions

    WP-CLI is designed for:

    • developers
    • automation
    • repeatable workflows

    Once you cross more than one site, wp-admin stops scaling.
    WP-CLI starts shining.


    My Core Rule

    If a task is repeatable, it should not require a browser.

    That rule alone eliminated hours of manual work every month.


    Real WP-CLI Workflows I Use Weekly

    1. Environment Setup in Minutes

    Instead of clicking:

    • creating users
    • configuring URLs
    • activating plugins

    I rely on:

    • scripted installs
    • predefined plugin sets
    • automated config changes

    This ensures:

    • consistency across environments
    • zero missed steps
    • faster onboarding

    2. Safe Search & Replace (No phpMyAdmin)

    Database operations are risky when done manually.

    With WP-CLI:

    • search & replace is predictable
    • dry-runs are possible
    • mistakes are reversible

    This turns a “dangerous task” into a routine operation.


    3. Plugin & Theme Management at Scale

    Manually activating plugins across sites does not scale.

    WP-CLI allows:

    • bulk activation/deactivation
    • scripted updates
    • environment-specific logic

    This is especially powerful when:

    • managing multiple client sites
    • maintaining your own products
    • preparing releases

    4. Debugging Without Guessing

    Instead of “let’s try disabling plugins”:

    • I inspect active plugins
    • check configs
    • isolate issues faster

    WP-CLI removes random trial-and-error from debugging.


    WP-CLI + Automation = Systems, Not Tricks

    WP-CLI is most powerful when combined with:

    • shell scripts
    • CI pipelines
    • repeatable checklists

    This allows:

    • predictable deployments
    • safer updates
    • fewer production surprises

    You stop reacting and start controlling the system.


    Where AI Fits Here (Important Perspective)

    AI doesn’t replace WP-CLI.
    It accelerates how you use it.

    I use AI to:

    • generate scripts faster
    • explain unfamiliar commands
    • refactor repetitive logic

    But the workflow remains automation-first.

    Tools change.
    Systems last.


    Why WP-CLI Is Future-Ready

    WordPress is moving toward:

    • tooling
    • structured workflows
    • professional development practices

    WP-CLI aligns perfectly with that direction.

    Developers who ignore it will:

    • work slower
    • break more things
    • burn out faster

    Final Thought

    If you want to work on bigger problems, stop spending time on small manual tasks.

    WP-CLI doesn’t just save time —
    it changes how you think about WordPress development.

    Read more: Automation-First WordPress Development

  • Day 6: Advanced WP-CLI Topics and Putting It All Together

    Cron Jobs and Scheduled Tasks

    WP-CLI allows you to manage WordPress cron jobs and scheduled tasks efficiently. Here’s how you can work with them:

    • To list all scheduled cron events:
    wp cron event list
    • To run all pending cron events:
    wp cron event run --due-now
    • To schedule a new cron event:
    wp cron schedule single --hook=my_custom_hook --time="2025-03-01 12:00:00"

    This command schedules a one-time event for the my_custom_hook hook to run on March 1, 2025, at 12:00 PM.

    Debugging and Troubleshooting

    WP-CLI provides tools for debugging and troubleshooting your WordPress site:

    • To enable WP_DEBUG and display errors:
    wp config set WP_DEBUG true --raw
    wp config set WP_DEBUG_LOG true --raw
    wp config set WP_DEBUG_DISPLAY true --raw
    • To check for common WordPress issues:
    wp core verify-checksums
    wp plugin list --status=active
    wp theme list --status=active

    These commands help you verify the integrity of your WordPress files and list active plugins and themes, which can be useful for troubleshooting.

    Real-World Examples and Use Cases

    Let’s explore some real-world examples of how WP-CLI can be used:

    1. Automating Backups: You can create a script that uses WP-CLI to export your database and files regularly, then upload them to a remote storage service.
    2. Bulk Content Updates: If you need to update multiple posts or pages with new information, you can use WP-CLI to perform these updates efficiently.
    3. Staging to Production Deployment: WP-CLI can help you automate the process of deploying changes from a staging site to your live production site.
    4. Performance Optimization: You can use WP-CLI to optimize your database, clear caches, and run performance tests to improve your site’s speed.

    Best Practices and Tips

    Here are some best practices and tips for efficient WP-CLI usage:

    • Script Your Tasks: Create shell scripts or use WP-CLI’s built-in scripting capabilities to automate repetitive tasks.
    • Use Aliases: Set up aliases for frequently used commands to save time and reduce typing.
    • Test on Staging: Always test your WP-CLI commands on a staging site before running them on your live site.
    • Keep WP-CLI Updated: Regularly update WP-CLI to ensure you have the latest features and security patches.
    • Document Your Commands: Keep a record of the WP-CLI commands you use for future reference and to share with your team.

    Conclusion

    Congratulations on completing this 7-day course on WP-CLI! You’ve learned how to manage WordPress core, content, users, databases, and more using WP-CLI commands. Remember to practice what you’ve learned and explore additional WP-CLI commands and plugins to further enhance your WordPress management skills.

    If you have any questions or need further assistance, feel free to reach out. Happy WP-CLI-ing!

    First Name
    Last Name
    Email
    Message
    The form has been submitted successfully!
    There has been some error while submitting the form. Please verify all form fields again.
  • Day 5: Managing Your WordPress Database with WP-CLI

    Exporting and Importing Databases

    WP-CLI provides powerful commands for managing your WordPress database. Let’s explore how to export and import databases using WP-CLI.

    • To export your entire WordPress database:
    wp db export wp_database_backup.sql

    This command creates a SQL file named wp_database_backup.sql containing your entire WordPress database.

    • To import a database backup:
    wp db import wp_database_backup.sql

    This command imports the contents of the wp_database_backup.sql file into your WordPress database, overwriting the existing data.

    Running Database Queries

    WP-CLI allows you to run custom SQL queries directly on your WordPress database. Here are some examples:

    • To run a simple SELECT query:
    wp db query "SELECT * FROM wp_posts WHERE post_type = 'post'"

    This command retrieves all posts from the wp_posts table.

    • To run an UPDATE query:
    wp db query "UPDATE wp_options SET option_value = 'new_value' WHERE option_name = 'option_name'"

    This command updates the value of a specific option in the wp_options table.

    • To run an INSERT query:
    wp db query "INSERT INTO wp_posts (post_title, post_content, post_status) VALUES ('New Post', 'This is a new post.', 'publish')"

    This command inserts a new post into the wp_posts table.

    Optimizing and Repairing the Database

    WP-CLI provides commands to optimize and repair your WordPress database tables:

    • To optimize all tables:
    wp db optimize
    • To repair all tables:
    wp db repair

    These commands can help improve database performance and fix any issues with corrupted tables.

    Best Practices

    When managing your WordPress database with WP-CLI, consider the following best practices:

    • Regularly backup your database before making any changes
    • Use the --dry-run flag when testing queries to see the results without modifying the database
    • Be cautious when running UPDATE or DELETE queries, as they can permanently modify or remove data
    • Use the --skip-plugins and --skip-themes flags when running database commands to prevent conflicts with plugins or themes

    In the next and final lesson, we’ll explore some advanced WP-CLI topics and put it all together. Stay tuned!

  • Day 4: Managing Users with WP-CLI

    Creating, Updating, and Deleting Users

    WP-CLI provides powerful commands for managing users on your WordPress site. Let’s explore how to create, update, and delete users using WP-CLI.

    • To create a new user:
    wp user create username email@example.com --role=author --user_pass=password123

    This command creates a new user with the specified username, email, role, and password. You can customize the role and other user metadata as needed.

    • To update an existing user:
    wp user update 123 --display_name="New Display Name" --role=editor

    This command updates the user with ID 123, changing their display name and role to editor. You can update various user fields using this command.

    • To delete a user:
    wp user delete 123 --reassign=456

    This command deletes the user with ID 123 and reassigns their content to the user with ID 456. If you don’t want to reassign content, you can omit the --reassign parameter.

    Managing User Roles and Capabilities

    WP-CLI allows you to manage user roles and their associated capabilities. Here are some examples:

    • To list all available roles:
    wp role list
    • To create a new role:
    wp role create custom_role "Custom Role"
    • To add a capability to a role:
    wp role add custom_role edit_posts
    • To remove a capability from a role:
    wp role remove custom_role edit_posts
    • To assign a role to a user:
    wp user set-role 123 custom_role

    Best Practices

    When managing users with WP-CLI, consider the following best practices:

    • Use strong, unique passwords for each user account
    • Regularly review and update user roles and capabilities to ensure proper access control
    • Use the --porcelain flag when scripting user management tasks to ensure consistent output
    • Backup your user database before making bulk changes or deletions

    In the next lesson, we’ll explore how to manage your WordPress database using WP-CLI. Stay tuned!

  • Day 3: Managing Content with WP-CLI

    Creating and Managing Posts, Pages, and Custom Post Types

    WP-CLI provides powerful commands for managing your WordPress content. Let’s explore how to create and manage posts, pages, and custom post types.

    • To create a new post:
    wp post create --post_type=post --post_title="My New Post" --post_content="This is the content of my new post." --post_status=publish
    • To create a new page:
    wp post create --post_type=page --post_title="About Us" --post_content="This is the content of our About Us page." --post_status=publish
    • To create a custom post type (assuming you have already registered the custom post type):
    wp post create --post_type=book --post_title="The Great Gatsby" --post_content="A novel by F. Scott Fitzgerald." --post_status=publish

    You can also update existing content using the wp post update command. For example, to update the title of a post with ID 123:

    wp post update 123 --post_title="Updated Post Title"

    Importing and Exporting Content

    WP-CLI makes it easy to import and export content using the WordPress eXtended RSS (WXR) format.

    • To export all content to a WXR file:
    wp export --dir=/path/to/export --skip_comments
    • To import content from a WXR file:
    wp import /path/to/import/file.wxr

    Managing Post Metadata

    You can use WP-CLI to manage post metadata, such as featured images and custom fields.

    • To set a featured image for a post:
    wp post meta set 123 _thumbnail_id 456
    • To add a custom field to a post:
    wp post meta add 123 custom_field_key "Custom field value"

    Best Practices

    When managing content with WP-CLI, consider the following best practices:

    • Use descriptive post titles and slugs for better SEO
    • Organize your content using categories and tags
    • Regularly backup your content before making bulk changes
    • Test your commands on a staging site before applying them to your live site

    In the next lesson, we’ll explore how to manage users and their roles using WP-CLI. Stay tuned!

  • Day 2: Managing WordPress Core with WP-CLI

    Updating WordPress Core

    One of the most common tasks you’ll perform with WP-CLI is updating WordPress core. To check if an update is available, you can use the following command:

    wp core check-update

    If an update is available, you can update WordPress core using:

    wp core update

    This command will download the latest version of WordPress and update your installation. If you want to update to a specific version, you can use:

    wp core update --version=5.8

    Installing and Managing Themes and Plugins

    WP-CLI makes it easy to install, activate, and manage themes and plugins. Here are some examples:

    • To install a theme:
    wp theme install twentysixteen
    • To install a theme:
    wp theme activate twentysixteen
    • To install a plugin:
    wp plugin install akismet

    You can also update all installed themes and plugins at once using:

    wp theme update --all
    wp plugin update --all

    Managing WordPress Core Versions

    WP-CLI allows you to manage different versions of WordPress core. You can roll back to a previous version using:

    wp core update --version=5.7

    To verify the integrity of your WordPress files, you can use:

    wp core verify-checksums

    This command checks your WordPress files against the official checksums to ensure they haven’t been modified.

    Best Practices

    When managing WordPress core with WP-CLI, it’s a good idea to:

    • Regularly check for updates and keep your WordPress installation up to date
    • Test updates on a staging site before applying them to your live site
    • Use version control to track changes to your WordPress files
    • Backup your site before performing any updates or changes

    In the next lesson, we’ll explore how to manage content using WP-CLI, including creating and managing posts, pages, and custom post types. Stay tuned!

  • Day 1: Introduction to WP-CLI

    What is WP-CLI and its Benefits

    WP-CLI is a command-line interface for managing WordPress installations. It allows you to perform various tasks and manage your WordPress site more efficiently than using the WordPress admin dashboard. Some benefits of using WP-CLI include:

    • Automating repetitive tasks
    • Managing multiple WordPress sites from a single interface
    • Performing complex operations that are difficult or impossible through the WordPress admin
    • Integrating WordPress with other command-line tools and scripts

    Installation and Setup

    To use WP-CLI, you’ll need to have PHP and a command-line interface on your computer. Here’s how to install WP-CLI:

    1. Download the WP-CLI phar file from the official website.
    2. Move the file to a directory in your system’s PATH, such as /usr/local/bin/.
    3. Make the file executable by running chmod +x /usr/local/bin/wp.

    Once installed, you can verify the installation by running wp --info in your terminal.

    Basic Commands

    Let’s explore some basic WP-CLI commands to get you started:

    • wp help: Displays a list of available commands and their descriptions.
    • wp version: Shows the current version of WP-CLI and WordPress.

    Here’s an example of using the wp help command:

    $ wp help
    usage: wp [--path=<path>] [--url=<url>] [--user=<id>|<login>|<email>] [--skip-plugins] [--skip-themes] [--skip-packages] [--require=<path>] [--exec=<php-code>] [--<field>=<value>] <command> [<subcommand>] [<args>] [--<assoc>=<value>]
    
    Global parameters:
      --path=<path>           Path to WordPress files.
      --url=<url>             Pretend request came from given URL. In multisite, this argument is how the target site is specified.
      --user=<id>|<login>|<email>
                              Set the WordPress user.
      --skip-plugins         Skip loading all plugins.
      --skip-themes          Skip loading all themes.
      --skip-packages        Skip loading all must-use plugins.
      --require=<path>        Load PHP file before running the command (may be used more than once).
      --exec=<php-code>       Execute PHP code before running the command (may be used more than once).
      --<field>=<value>       Set arbitrary value to predefined constant or WordPress option.
      --<assoc>=<value>       Set arbitrary value to command argument.
    
    Available commands:
      cache                  Manage the object cache.
      cap                    Manage user capabilities.
      cli                    Manage WP-CLI itself.
      comment                Manage comments.
      config                 Generate and read WordPress config files.
      core                   Download, install, update, and manage a WordPress installation.
      cron                   Manage WP-Cron events and schedules.
      db                     Perform database operations.
      embed                  Manage oEmbed caches.
      eval                   Execute arbitrary PHP code.
      export                 Export content to a WordPress eXtended RSS (WXR) file.
      find                   Loop over posts and execute PHP on each of them.
      import                 Import content from a WordPress eXtended RSS (WXR) file.
      media                  Manage attachments.
      menu                   Manage menus.
      network                Manage multisite networks.
      option                 Manage WordPress options.
      package                Manage WordPress packages.
      plugin                 Manage plugins.
      post                   Manage posts.
      rewrite                Manage rewrite rules.
      role                   Manage user roles.
      scaffold               Generate code for common WordPress development tasks.
      search-replace         Search/replace strings in the database.
      server                 Launch PHP's built-in web server.
      shell                  Open an interactive PHP console for running WP-CLI commands.
      site                   Manage multisite sites.
      super-admin            Manage super admins.
      term                   Manage terms.
      theme                 Manage themes.
      transient              Manage transients.
      user                   Manage users.
      widget                Manage widgets.
    
    See 'wp help <command>' for more information on a specific command.

    This output shows you the available commands and their brief descriptions. You can use wp help <command> to get more detailed information about a specific command.

    For example, to learn more about the wp core command, you would run:

    $ wp help core
    usage: wp core <command>
    
    Manage WordPress core.
    
    See 'wp help core <command>' for information on a specific command.
    
    Available commands:
      core download          Download WordPress core.
      core install           Install WordPress core.
      core update            Update WordPress core.
      core update-db         Update the WordPress database.
      core verify-checksums Verify WordPress files against WordPress.org's checksums.
      core version           Display the WordPress version.

    Now that you have a basic understanding of WP-CLI and its installation, you’re ready to dive deeper into its capabilities in the coming days. Stay tuned for more lessons on managing WordPress core, content, users, and more using WP-CLI!