ワードプレスを管理するためのWP-CLIを導入する

スポンサーリンク

WordPressを管理するためのコマンドラインインターフェースのWP-CLIをインストールします。理由はデータベースの文字列を置換したいという事態に直面したからです。
これを可能にするプラグインやツールもありますが、Linux自体を扱っている人はこちらのコマンドラインインターフェースがおすすめです。SQLの構文を使用する方法もありますが、知識がないと困難です。
公式サイトに導入方法が記載されているので、それをコピペするだけです。

Command line interface for WordPress | WP-CLI

必要なファイルをダウンロードして、動作確認します。

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info

ファイルに実行権限を付与して、パスが通っているディレクトリにファイルを移動します。
どのディレクトリにいてもコマンドが正常に動作するようにそうします。

chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp

WP-CLIの情報と、アップデートがあるか確認します。

wp --info
wp cli update

せっかくなのでワードプレスの置換のテストをします。dry-runを付けると実際には置換せずテストができます。その際、ワードプレスのインストールディレクトリに移動しておきます。

cd /usr/share/nginx/html/example.com/
wp search-replace --dry-run '.life' '.com'
[root@ip-172-31-15-164 example.com]# wp search-replace  --dry-run '.life' '.com'
PHP Warning:  Undefined variable $wp_embed in /usr/share/nginx/html/example.com/wp-content/themes/cocoon-master/lib/settings.php on line 372
Warning: Undefined variable $wp_embed in /usr/share/nginx/html/example.com/wp-content/themes/cocoon-master/lib/settings.php on line 372
+----------------------------+-----------------------+--------------+------+
| Table                      | Column                | Replacements | Type |
+----------------------------+-----------------------+--------------+------+
| wp_actionscheduler_actions | hook                  | 0            | SQL  |
| wp_actionscheduler_actions | status                | 0            | SQL  |
| wp_actionscheduler_actions | args                  | 0            | SQL  |
| wp_actionscheduler_actions | schedule              | 0            | PHP  |
| wp_actionscheduler_actions | extended_args         | 0            | SQL  |
| wp_actionscheduler_groups  | slug                  | 0            | SQL  |
| wp_actionscheduler_logs    | message               | 0            | SQL  |
| wp_commentmeta             | meta_key              | 0            | SQL  |
| wp_commentmeta             | meta_value            | 0            | SQL  |
| wp_comments                | comment_author        | 0            | SQL  |
| wp_comments                | comment_author_email  | 0            | SQL  |
| wp_comments                | comment_author_url    | 0            | SQL  |
| wp_comments                | comment_author_IP     | 0            | SQL  |
| wp_comments                | comment_content       | 0            | SQL  |
| wp_comments                | comment_approved      | 0            | SQL  |
| wp_comments                | comment_agent         | 0            | SQL  |
| wp_comments                | comment_type          | 0            | SQL  |
| wp_links                   | link_url              | 0            | SQL  |
| wp_links                   | link_name             | 0            | SQL  |
| wp_links                   | link_image            | 0            | SQL  |
| wp_links                   | link_target           | 0            | SQL  |
| wp_links                   | link_description      | 0            | SQL  |
| wp_links                   | link_visible          | 0            | SQL  |
| wp_links                   | link_rel              | 0            | SQL  |
| wp_links                   | link_notes            | 0            | SQL  |
| wp_links                   | link_rss              | 0            | SQL  |
| wp_options                 | option_name           | 0            | SQL  |
| wp_options                 | option_value          | 6            | PHP  |
| wp_options                 | autoload              | 0            | SQL  |
| wp_postmeta                | meta_key              | 0            | SQL  |
| wp_postmeta                | meta_value            | 2            | PHP  |
| wp_posts                   | post_content          | 65           | SQL  |
| wp_posts                   | post_title            | 0            | SQL  |
| wp_posts                   | post_excerpt          | 0            | SQL  |
| wp_posts                   | post_status           | 0            | SQL  |
| wp_posts                   | comment_status        | 0            | SQL  |
| wp_posts                   | ping_status           | 0            | SQL  |
| wp_posts                   | post_password         | 0            | SQL  |
| wp_posts                   | post_name             | 0            | SQL  |
| wp_posts                   | to_ping               | 0            | SQL  |
| wp_posts                   | pinged                | 0            | SQL  |
| wp_posts                   | post_content_filtered | 0            | SQL  |
| wp_posts                   | guid                  | 54924        | SQL  |
| wp_posts                   | post_type             | 0            | SQL  |
| wp_posts                   | post_mime_type        | 0            | SQL  |
| wp_term_taxonomy           | taxonomy              | 0            | SQL  |
| wp_term_taxonomy           | description           | 0            | SQL  |
| wp_termmeta                | meta_key              | 0            | SQL  |
| wp_termmeta                | meta_value            | 0            | SQL  |
| wp_terms                   | name                  | 0            | SQL  |
| wp_terms                   | slug                  | 0            | SQL  |
| wp_usermeta                | meta_key              | 0            | SQL  |
| wp_usermeta                | meta_value            | 0            | PHP  |
| wp_users                   | user_login            | 0            | SQL  |
| wp_users                   | user_nicename         | 0            | SQL  |
| wp_users                   | user_email            | 0            | SQL  |
| wp_users                   | user_url              | 1            | SQL  |
| wp_users                   | user_activation_key   | 0            | SQL  |
| wp_users                   | display_name          | 0            | SQL  |
+----------------------------+-----------------------+--------------+------+
Success: 54998 replacements to be made.

データベースが大きく、URLの置換の際プラグインなのでは動作が途中で止まりましたが、こちらは問題なく意図したことができました。

タイトルとURLをコピーしました