Successfully reported this slideshow.

Saving Time with WP-CLI

3

Share

Upcoming SlideShare
You Got React.js in My PHP
You Got React.js in My PHP
Loading in …3
×
1 of 34
1 of 34

Saving Time with WP-CLI

3

Share

Download to read offline

WP-CLI is an awesome WordPress plugin that can be used to automate a bunch of difficult WordPress tasks. This presentation walks you through basic WP-CLI commands and creating custom commands.

WP-CLI is an awesome WordPress plugin that can be used to automate a bunch of difficult WordPress tasks. This presentation walks you through basic WP-CLI commands and creating custom commands.

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Saving Time with WP-CLI

  1. 1. Saving Time with WP-CLI
  2. 2. Who Am I? • My name is Taylor Lovett Senior Strategic Engineer at 10up Plugin Author (Safe Redirect Manager, Feed Pull) Plugin Contributor (WP-CLI and others) Core Contributor
  3. 3. We’re hiring!
  4. 4. What Is WP-CLI? • WP-CLI is a WordPress plugin that enables you to interact with your WordPress installations via the command line (Unix). • WP-CLI lets you do a ton of things - create new sites, install plugins, import, export, batch create/delete posts, etc. • WP-CLI is extensible. You can easily write your own WP-CLI commands.
  5. 5. Why Use WP-CLI? • Doing certain tasks from within WP admin are very difficult… - Bulk deleting posts, bulk editing posts, bulk tagging posts, importing, exporting, etc.
  6. 6. Don’t be scared of the command line!
  7. 7. Installing WP-CLI • Installation is very easy! • Instructions here: http://wp-cli.org/
  8. 8. Tons of cool commands are built-in!
  9. 9. Search and replace wp search-replace <old-string> <new-string> … (Full list of command options at http://wp-cli.org/commands/search-replace/) Example: wp search-replace testsite.com livesite.com wp_posts
  10. 10. Generating Dummy Data • Ever wish you had a bunch of test data when building a new theme or plugin? wp generate posts --count=500 wp generate users --role=contributor
  11. 11. Regenerate Post Thumbnails • Easily regenerate post thumbnails without having to deal with plugins and WP admin: wp media regenerate (From http://wp-cli.org/commands/media/regenerate/)
  12. 12. Create Database Dumps • WP-CLI lets you easily create database dumps: wp db dump (From http://wp-cli.org/commands/db/)
  13. 13. Export wp export [--dir=<dirname>] [--skip_comments] … (From http://wp-cli.org/commands/export/) • Example wp export --dir=~/temp --max_file_size=5 -- start_date=2012-05-12 --end_date=2014-01-10
  14. 14. Import wp import <file> --authors=<authors> … (From http://wp-cli.org/commands/import/) • Example wp import import.xml --authors=create
  15. 15. Global Command Parameters --user=set the current user --url=set the current URL --path=set the current path to the WP install --require=load a certain file before running the command --versionprint WP-CLI version • These parameters can be supplied to any WP-CLI command
  16. 16. Global Command Parameters • The --path argument is especially useful. You can call the “wp” command from anywhere in the file system and specify an instance of WordPress to interact with. • This is helpful if you are writing a crontab file, for example.
  17. 17. WP-CLI and Multisite • WP-CLI works great with multisite! The global url parameter mentioned earlier lets you specify on which site to run the command. For example wp --url=http://mysite.com/site2 post delete 3
  18. 18. Command and Subcommands • WP-CLI has this notion of commands and subcommands. wp post delete 476 • “post” is the command. “delete” is the subcommand
  19. 19. Community Commands • There is a large community of people developing WP-CLI commands: http://wp-cli.org/package-index/
  20. 20. Creating New Commands • Sometimes we need to create new WP-CLI commands because the built-in ones don’t do what we want and an HTTP request just isn’t cutting it.
  21. 21. Creating Commands • You can create a new WP-CLI command in a plugin or a theme. For a theme, this code can be added to functions.php, for a plugin, any PHP file that is parsed: <?php if ( defined( 'WP_CLI' ) && WP_CLI ) { include( 'includes/class-prefix-cli-utils.php' ); } Note: We don’t want to include our CLI class on normal HTTP requests which is why we check if WP_CLI is defined.
  22. 22. Creating Commands • Remember how I mentioned commands and sub- commands? I generally create one command per plugin or theme and group everything as subcommands under it.
  23. 23. Creating Commands • Here is includes/class-prefix-cli-utils.php: <?php /** * Utility commands for my theme/plugin */ class Prefix_CLI_Utils extends WP_CLI_Command { ... } WP_CLI::add_command( 'prefix-utils', 'Prefix_CLI_Utils' ); Note: We prefix classes and everything that is in a shared namespace so we don’t get any conflicts.
  24. 24. Creating Commands • Now that our base command is scaffolded. Let’s build something cool.
  25. 25. A Problem to Solve • Let’s imagine we are running a WordPress website with ~15,000 posts. We’ve decided we want to start making better use of post formats, specifically gallery. We want every post that has a [gallery] shortcode to have a post format of gallery. Is there anyway to do this without going through each post manually?
  26. 26. Yes!! WP-CLI!
  27. 27. Creating Our Command <?php /** * Utility commands for my theme/plugin */ class Prefix_CLI_Utils extends WP_CLI_Command { /** * Mark posts as gallery format if a gallery shortcode is in the post. * * ###OPTIONS * * [—-post_type=<post_type>] * : Restrict processing to certain post types * * [--status=<status>] * : Only process posts with this status * * @subcommand format-gallery-posts * @synopsis [—-post_type=<post_type>] [--status=<status>] */ public function format_gallery_posts( $args, $assoc_args ) { } } WP_CLI::add_command( 'prefix-utils', 'Prefix_CLI_Utils' );
  28. 28. <?php /** * Utility commands for my theme/plugin */ class Prefix_CLI_Utils extends WP_CLI_Command { /** * Mark posts as gallery format if a gallery shortcode is in the post. * * ###OPTIONS * * [--post_type=<post_type>] * : Restrict processing to certain post types * * [--status=<status>] * : Only process posts with this status * * @subcommand format-gallery-posts * @synopsis [--post_type=<post_type>] [--status=<status>] */ public function format_gallery_posts( $args, $assoc_args ) { $status = 'publish'; if ( ! empty( $assoc_args['status'] ) ) { $status = $assoc_args['status']; } $post_type = 'post'; if ( ! empty( $assoc_args['post_type'] ) ) { $post_type = $assoc_args['post_type']; } $page = 1; $posts_changed = 0; $posts_processed = 0; WP_CLI::line( 'Start processing posts...' ); while ( true ) {
  29. 29. $query_args = array( 'status' => $status, 'post_type' => $post_type, 'cache_results' => false, 'posts_per_page' => 50, 'paged' => $page, 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'gallery' ), 'operator' => 'NOT IN' ), ) ); $query = new WP_Query( $query_args ); if ( ! $query->have_posts() ) { break; } while ( $query->have_posts() ) { global $post; $query->the_post(); $posts_processed++; if ( strpos( $post->post_content, '[gallery' ) !== false ) { set_post_format( $post, 'gallery' ); $posts_changed++; } } $page++; } wp_reset_postdata(); WP_CLI::success( $posts_processed . ' posts processed. ' . $posts_changed . ' posts changed.' ); } }
  30. 30. Another Problem • We’ve been running a blog for a few years with around ~4000 posts. We decide that we want to restructure our categories. For the last few years we have prepended “Breaking News: “ to important news posts. We want to categorize all these posts into a “News” category. We don’t want to go through each post manually.
  31. 31. WP-CLI!
  32. 32. Creating Our Command <?php /** * Utility commands for my theme/plugin */ class Prefix_CLI_Utils extends WP_CLI_Command { /** * Tag posts that have titles starting with "Breaking News: " with the * category "News" * * ## EXAMPLES * * wp prefix-utils tag-news-posts * * @alias tag-news * @subcommand tag-news-posts */ public function tag_news_posts( $args, $assoc_args ) { } } WP_CLI::add_command( 'prefix-utils', 'Prefix_CLI_Utils' );
  33. 33. public function tag_news_posts( $args, $assoc_args ) { $page = 1; $posts_changed = 0; $posts_processed = 0; WP_CLI::line( 'Start tagging posts...' ); while ( true ) { $query_args = array( 'status' => 'publish', 'post_type' => 'post', 'cache_results' => false, 'posts_per_page' => 50, 'paged' => $page, ); $query = new WP_Query( $query_args ); if ( ! $query->have_posts() ) { break; } while ( $query->have_posts() ) { $query->the_post(); $posts_processed++; if ( preg_match( '#^Breaking News:#i', get_the_title() ) !== false ) { wp_set_object_terms( get_the_ID(), array( 'News' ), 'category', true ); $posts_changed++; } } $page++; } wp_reset_postdata(); WP_CLI::success( $posts_processed . ' posts processed. ' . $posts_changed . ' posts changed.' ); }
  34. 34. Questions? Twitter: tlovett12 Email: taylor.lovett@10up.com

×