• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Saving Time with WP-CLI
 

Saving Time with WP-CLI

on

  • 2,977 views

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.

Statistics

Views

Total Views
2,977
Views on SlideShare
518
Embed Views
2,459

Actions

Likes
1
Downloads
4
Comments
0

8 Embeds 2,459

http://taylorlovett.com 1256
http://www.taylorlovett.com 1114
https://twitter.com 74
http://tlovett1.wordpress.com 11
http://www.slideee.com 1
http://translate.googleusercontent.com 1
http://feedly.com 1
http://prlog.ru 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Saving Time with WP-CLI Saving Time with WP-CLI Presentation Transcript

    • Saving Time with WP-CLI
    • 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
    • We’re hiring!
    • 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.
    • 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.
    • Don’t be scared of the command line!
    • Installing WP-CLI • Installation is very easy! • Instructions here: http://wp-cli.org/
    • Tons of cool commands are built-in!
    • 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
    • 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
    • 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/)
    • Create Database Dumps • WP-CLI lets you easily create database dumps: wp db dump (From http://wp-cli.org/commands/db/)
    • 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
    • Import wp import <file> --authors=<authors> … (From http://wp-cli.org/commands/import/) • Example wp import import.xml --authors=create
    • 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
    • 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.
    • 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
    • Command and Subcommands • WP-CLI has this notion of commands and subcommands. wp post delete 476 • “post” is the command. “delete” is the subcommand
    • Community Commands • There is a large community of people developing WP-CLI commands: http://wp-cli.org/package-index/
    • 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.
    • 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.
    • 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.
    • 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.
    • Creating Commands • Now that our base command is scaffolded. Let’s build something cool.
    • 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?
    • Yes!! WP-CLI!
    • 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' );
    • <?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 ) {
    • $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.' ); } }
    • 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.
    • WP-CLI!
    • 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' );
    • 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.' ); }
    • Questions? Twitter: tlovett12 Email: taylor.lovett@10up.com