WordPress Development Guide

WordPress 开发指引



curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

WP CLI 安装完后,oh-my-zsh 用户可以激活相关插件plugins=(wp-cli git [...]),获得命令行补全功能。 

常用命令 作用
wp --info 查看当前环境
wp cli update 升级 WP CLI
wp core version 确认 WordPress 版本

安装 WordPress

# download wordpress source code in working directory
wp core download --version 5.9

# generate config file
wp config create --dbname=wp_5.9 --dbuser=root --prompt=dbpass --dbhost=HOST

wp db create

wp core install --url=wp-5.9.x.local --title="wp-5.9.x" --admin_user=root --admin_password=root --admin_email=root@wp-5.9.x.local

升级 WordPress

wp core update --version=5.9.3
wp core update-db

# 完整升级
wp core update && wp plugin update --all && wp theme update --all && wp-cli core update-db --network && wp cli update


wp core update --minor
wp-cli core update-db --network
wp plugin update --all --patch
wp theme update --all


名称 说明
_s 纯 PHP/HTML/CSS/JS 入门主题模版
bootscore 父主题含 Bootstrap 支持,fork bootscore-child 添加网站样式制作子主题,Sass 支持


全局替换主题名及文件夹名后生效。载入自定义 CSS 和 JS 如下:

function THEME_scripts() {
wp_enqueue_style( 'CSS_NAME', get_template_directory_uri() . '/css/css_name.css', array(), VERSION );
wp_enqueue_script( 'JS_NAME', get_template_directory_uri() . '/js/js_name.js', array(), VERSION, true );
add_action( 'wp_enqueue_scripts', 'THEME_scripts' );


PhpStorm File Watch 配置

bootscore 自带 sass 支持,编译较慢时可配置 IDE 的 File Watch 功能,使 scss 文件更改时自动生成 css。开发机需要安装 sass 命令,数据库 options 表的 theme_mods_主题名 记录需要调整 css 生成时间戳至最大 bootscore_scss_modified_timestamp 为 9999999999。

brew install sass/sass/sass
<!-- 另存为 xml 文件可导入 PhpStorm -->
    <option name="arguments" value="-I $ProjectFileDir$/wp-content/themes/bootscore/scss main.scss:../css/main.css" />
    <option name="checkSyntaxErrors" value="true" />
    <option name="description" />
    <option name="exitCodeBehavior" value="ERROR" />
    <option name="fileExtension" value="scss" />
    <option name="immediateSync" value="true" />
    <option name="name" value="SCSS" />
    <option name="output" value="main.css:main.css.map" />
    <option name="outputFilters">
      <array />
    <option name="outputFromStdout" value="false" />
    <option name="program" value="sass" />
    <option name="runOnExternalChanges" value="true" />
    <option name="scopeName" value="Project Files" />
    <option name="trackOnlyRoot" value="true" />
    <option name="workingDir" value="$FileDir$" />
    <envs />



# 自定义菜单数据库详情
SELECT * FROM wp_posts WHERE post_type = 'nav_menu_item'



在 wp-config.php 中配置:

/* Multisite */
define( 'WP_ALLOW_MULTISITE', true );
/* Tools > Network 安装后补充 */
define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', true );
define( 'DOMAIN_CURRENT_SITE', 'moha.online' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );
/* That's all, stop editing! Happy publishing. */

禁用 cron

define( 'DISABLE_WP_CRON', 1 );


# macOS OK
# Upstream to abstract backend connection(s) for php
upstream php {
        # server unix:/tmp/php-cgi.socket;
        # server unix:/usr/local/var/run/php82/php-fpm/www.sock;

server {
        ## Your website name goes here.
        server_name wp-5.9.x.local;
        listen 80;

        ## Your only path reference.
        root WEB_ROOT;

        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
Author: njun
njun's picture
Updated: 2023/09/23