WP-CLI

WP-CLI는 WordPress Command Line Interface의 약자로, 웹 브라우저의 관리자 화면 대신 터미널(명령줄)에서 워드프레스를 관리할 수 있는 공식 도구입니다. WP-CLI 문서는 https://make.wordpress.org/cli/ 에서 확인할 수 있습니다.

  • 일반적인 방법 : 워드프레스 관리자 페이지에 로그인해서 클릭
  • WP-CLI 방법 : SSH로 서버에 접속해서 명령어 입력

설치 방법

WP-CLI는 공식 문서를 참고하여 설치할 수 있습니다.

제가 사용중인 카페24 서비는 단독으로 사용하는 호스트가 아니기 때문에 WP-CLI 설치방법대로 처리할 수 없었습니다.
php 경로가 등록되어 있지 않았고, /usr/local/bin/ 는 접근이 안되는 상황입니다.
이에 따라 alias와 path를 수정하여 다음과 같이 처리하였습니다.

wp-cli.phar 다운로드

[~]$ mkdir -p ~/bin
[~/bin]$ cd ~/bin
[~/bin]$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
[/bin]$ chmod +x wp-cli.phar

path 및 alias 등록

[~]$ vi ~/.profile
export PATH=/usr/local/php84/bin:$PATH
export PATH=/byul124/bin:$PATH
alias ll='ls -alF'
alias vi='vim $*'
alias h='history'
alias wp='/usr/local/php84/bin/php /byul124/bin/wp-cli.phar'
export PS1="[\[\e[32;1m\]\w\[\e[0m\]]\\$ "

[~]$ vi ~/.bash_profile
-- 중략 --
if [ -f ~/.profile ]; then
    . ~/.profile
fi
-- 중략 --

WP-CLI 확인

[~]$ wp --info
OS:	Linux 4.18.0-553.124.1.el8_10.x86_64 #1 SMP Tue May 12 16:44:08 UTC 2026 x86_64
Shell:	/bin/bash
-- 중략 --
WP-CLI version:    2.12.0

기본 명령어

일반적인 유지보수를 위한 명령어들은 다음과 같습니다.
참고로 ~/www/blog는 wordpress가 설치된 디렉토리입니다.

# wp-cli 업데이트
[~/www/blog]$ wp cli update
Success: WP-CLI is at the latest version.

# 워드 프레스 버전 확인
[~/www/blog]$ wp core version
7.0

# 플러그인 리스트
[~/www/blog]$ wp plugin list
+---------------+----------+--------+---------+---------------+----------------+
| name          | status   | update | version | update_versio | auto_update    |
|               |          |        |         | n             |                |
+---------------+----------+--------+---------+---------------+----------------+
| wp-add-mime-t | active   | none   | 3.2.0   |               | off            |
| ypes          |          |        |         |               |                |
| akismet       | active   | none   | 5.7     |               | off            |
| blocksy-compa | active   | none   | 2.1.45  |               | off            |
| nion          |          |        |         |               |                |
| code-snippets | active   | none   | 3.9.6   |               | off            |
| default-featu | active   | none   | 1.8.2   |               | off            |
| red-image     |          |        |         |               |                |
| easy-table-of | active   | none   | 2.0.85  |               | off            |
| -contents     |          |        |         |               |                |
| sem-external- | active   | none   | 6.8.1   |               | off            |
| links         |          |        |         |               |                |
| easy-fancybox | active   | none   | 2.3.20  |               | off            |
| jetpack       | active   | none   | 15.9    |               | off            |
| media-cleaner | inactive | none   | 7.1.1   |               | off            |
| regenerate-th | inactive | none   | 3.1.6   |               | off            |
| umbnails      |          |        |         |               |                |
| google-site-k | active   | none   | 1.180.0 |               | on             |
| it            |          |        |         |               |                |
| yet-another-r | active   | none   | 5.30.11 |               | off            |
| elated-posts- |          |        |         |               |                |
| plugin        |          |        |         |               |                |
| wordpress-seo | active   | none   | 27.8    |               | off            |
| advanced-cach | dropin   |        |         |               | off            |
| e.php         |          |        |         |               |                |
+---------------+----------+--------+---------+---------------+----------------+

# 모든 플러그인 업데이트
[~/www/blog]$ wp plugin update --all
Success: Plugin already updated.

# 모든 테마 업데이트
[~/www/blog]$ wp theme update --all
Success: Theme already updated.

# 테마 리스트
[~/www/blog]$ wp theme list
+---------------+----------+--------+---------+---------------+----------------+
| name          | status   | update | version | update_versio | auto_update    |
|               |          |        |         | n             |                |
+---------------+----------+--------+---------+---------------+----------------+
| blocksy       | active   | none   | 2.1.45  |               | off            |
| twentytwentyf | inactive | none   | 1.5     |               | off            |
| ive           |          |        |         |               |                |
+---------------+----------+--------+---------+---------------+----------------+

# 테마 상태
[~/www/blog]$ wp theme status
2 installed themes:
  A blocksy          2.1.45
  I twentytwentyfive 1.5
Legend: A = Active, I = Inactive

최근에 서버환경을 PHP7.4에서 8.4로 변경하면서 워드프레스의 테마와 플러그인 오류가 발생하여 유지보수가 불가능한 상황이 발생했습니다. 이때 WP-CLI를 사용하여 트러블슈팅 및 유지보수를 진행할 수 있습니다.

# DB 백업 및 복원
[~/www/blog]$ wp db export backup.sql
[~/www/blog]$ wp db import backup.sql

# 모든 플러그인 비활성화
[~/www/blog]$ wp plugin deactivate elementor

# 기본 테마 설치
[~/www/blog]$ wp theme install twentytwentyfive

# 기본 테마 활성화
[~/www/blog]$ wp theme activate twentytwentyfive

코드 블록 변환

오래전에 텍스트큐브와 구텐베르크 편집기에서 정형화된 블럭으로 작성된 코드들이 남아 있어서 WP-CLI를 사용하여 코드 블럭으로 변환을 진행하였습니다.

- AS-IS : <pre class="wp-block-performatted">코드</pre>
- TO-BE : <pre class="wp-block-code"><code>코드</code></pre>


php로 변경할 로직을 정의하고, WP-CLI로 실행하였습니다.

[~/www/blog]$ vi convert-preformatted.php
<?php
$posts = get_posts([
    'post_type'      => 'any',
    'post_status'    => ['publish', 'draft', 'private'],
    'posts_per_page' => -1,
    's'              => 'wp:preformatted',
]);

foreach ($posts as $post) {
    $content = $post->post_content;

    $content = str_replace('<!-- wp:preformatted -->', '<!-- wp:code -->', $content);
    $content = str_replace('<!-- /wp:preformatted -->', '<!-- /wp:code -->', $content);

    $content = preg_replace(
        '/<pre class="wp-block-preformatted">(.*?)<\/pre>/s',
        '<pre class="wp-block-code"><code>$1</code></pre>',
        $content
    );

    wp_update_post([
        'ID'           => $post->ID,
        'post_content' => $content,
    ]);

    echo "Updated: [{$post->ID}] {$post->post_title}\n";
}

echo "Done.\n";


[~/www/blog]$ wp eval-file convert-preformatted.php --path=/byul124/www/blog --allow-root
Updated: [8155] ES/AM/Kodi 부팅 선택기
Updated: [8127] 2인용 나무상자 게임기
Updated: [8084] 블로그 수정하였습니다.
-- 중략 --
Updated: [306] Ajax Study #2 - innerHTML
Updated: [304] Ajax Study #1 - XMLHttpRequest
Done.

댓글 남기기