45 phút
Giới thiệu WordPress
WordPress là gì?
WordPress là CMS (Content Management System) phổ biến nhất, chiếm hơn 40% website toàn cầu. Được viết bằng PHP và MySQL.
Hai loại WordPress
- WordPress.org (Self-hosted): Bạn tự host, có thể chỉnh sửa code, cài plugin/theme tùy ý
- WordPress.com: Hosting managed, giới hạn tính năng
Cài đặt WordPress
Yêu cầu
- PHP 7.4+
- MySQL 5.7+ hoặc MariaDB 10.3+
- Apache/Nginx
Cài đặt nhanh với wp-env (Docker)
npm install -g @wordpress/env
mkdir my-wp && cd my-wp
wp-env start
# Admin: http://localhost:8888/wp-admin (admin/password)
# Site: http://localhost:8888
Local by Flywheel / LocalWP
# Download từ localwp.com
# Tạo site mới với giao diện trực quan
Cài đặt thủ công
# Download WordPress
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
mv wordpress mysite
cd mysite
# Configure wp-config.php
cp wp-config-sample.php wp-config.php
# Sau đó mở http://localhost/mysite để chạy installer
WP-CLI
# Install WP-CLI
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
# Common commands
wp core download
wp core install --url=example.com --title="My Site" --admin_user=admin --admin_password=pass --admin_email=admin@example.com
wp plugin list
wp theme list
wp post list
wp user list
Cấu trúc thư mục
wordpress/
├── wp-admin/ # Admin dashboard
├── wp-content/ # Custom code
│ ├── themes/ # Themes
│ ├── plugins/ # Plugins
│ ├── uploads/ # Uploaded files
│ └── mu-plugins/ # Must-use plugins
├── wp-includes/ # Core code
├── wp-config.php # Configuration
└── .htaccess # Apache rules
wp-config.php
<?php
// Database
define('DB_NAME', 'wordpress');
define('DB_USER', 'root');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', '');
// Authentication keys
define('AUTH_KEY', 'put-your-unique-phrase-here');
define('SECURE_AUTH_KEY', 'put-your-unique-phrase-here');
define('LOGGED_IN_KEY', 'put-your-unique-phrase-here');
define('NONCE_KEY', 'put-your-unique-phrase-here');
define('AUTH_SALT', 'put-your-unique-phrase-here');
define('SECURE_AUTH_SALT', 'put-your-unique-phrase-here');
define('LOGGED_IN_SALT', 'put-your-unique-phrase-here');
define('NONCE_SALT', 'put-your-unique-phrase-here');
$table_prefix = 'wp_';
// Debug
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
// Memory
define('WP_MEMORY_LIMIT', '256M');
// Disable file edit from admin
define('DISALLOW_FILE_EDIT', true);
// Auto-updates
define('WP_AUTO_UPDATE_CORE', 'minor');
if (!defined('ABSPATH')) {
define('ABSPATH', __DIR__ . '/');
}
require_once ABSPATH . 'wp-settings.php';
Hooks cơ bản
Actions
// Thêm code khi theme setup
add_action('after_setup_theme', function () {
add_theme_support('post-thumbnails');
});
// Thêm vào footer
add_action('wp_footer', function () {
echo '<p>Custom footer content</p>';
});
Filters
// Sửa title
add_filter('the_title', function ($title) {
return strtoupper($title);
});
// Sửa content
add_filter('the_content', function ($content) {
return $content . '<p>Thanks for reading!</p>';
});
Bài tập thực hành
Hãy cài đặt WordPress và tạo child theme đầu tiên!