Các hàm trong WordPress
<?php get_header( 'special' ); ?>
Đoạn mã trên trong tệp chủ đề sẽ tải tệp mẫu: header-special.php. Nếu không tìm thấy sẽ mặc định loading: header.php.
action
the_title( string $before = ”, string $after = ”, bool $display = true ): void|string
Hiển thị hoặc truy xuất tiêu đề bài viết hiện tại với đánh dấu tùy chọn.
<?php the_title( '<h3>', '</h3>' ); ?>
<h3>[tieu-de]</h3>
filter
add_filter( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 ): true
Thêm chức năng gọi lại vào móc lọc.
add_filter('get_the_archive_title', function($title){
if (is_category() || is_archive()) {
// Loại bỏ chữ "Danh mục: " khỏi tiêu đề
$title = single_cat_title('', false);
}
return $title;
});
Loại bỏ chữ "Danh mục: " khỏi tiêu đề
check
wp_is_mobile(): bool
Kiểm tra xem trình duyệt hiện tại có chạy trên thiết bị di động không (điện thoại thông minh, máy tính bảng, v.v.).
Hàm wp_is_mobile()
phát hiện thiết bị dựa trên giá trị của HTTP_USER_AGENT
(không liên quan đến kích thước màn hình).
wp_is_mobile
trả về true
hoặc false
dựa trên giá trị của $_SERVER['HTTP_USER_AGENT']
.
- Nếu
$_SERVER['HTTP_USER_AGENT']
chứa từ khóa Mobile (Android, iOS, ...) hoặc Tablet, thì hàm sẽ trả vềtrue
. - Nếu không chứa các từ khóa trên, hàm sẽ trả về
false
. - Nếu
$_SERVER['HTTP_USER_AGENT']
lànull
, hàm cũng trả vềfalse
.
<?php if ( wp_is_mobile() ) : ?>
/* Display and echo mobile specific stuff here */
<?php else : ?>
/* Display and echo desktop stuff here */
<?php endif; ?>
action
the_permalink( int|WP_Post $post )
Hiển thị liên kết cố định cho bài viết hiện tại.
get
get_the_permalink( int|WP_Post $post, bool $leavename = false ): string|false
Lấy liên kết cố định đầy đủ cho bài đăng hoặc ID bài đăng hiện tại.
update
update_term_meta( int $term_id, string $meta_key, mixed $meta_value, mixed $prev_value = ” ): int|bool|WP_Error
Cập nhật hoặc thêm mới giá trị meta cho thuật ngữ.
update
update_user_meta( int $user_id, string $meta_key, mixed $meta_value, mixed $prev_value = ” ): int|bool
Cập nhật hoặc thêm mới giá trị meta của người dùng.
update
wp_update_post( array|object $postarr = array(), bool $wp_error = false, bool $fire_after_hooks = true ): int|WP_Error
Cập nhật bài viết hiện có trong cơ sở dữ liệu.
sanitize
sanitize_key( string $key ): string
Làm sạch và chuẩn hóa dữ liệu key.
sanitize
sanitize_email( string $email ): string
Làm sạch và chuẩn hóa dữ liệu email.
sanitize
sanitize_text_field( string $str ): string
Làm sạch và chuẩn hóa dữ liệu văn bản (text).
insert
wp_insert_comment( array $commentdata ): int|false
Thêm một bình luận vào cơ sở dữ liệu.
insert
wp_insert_post( array $postarr, bool $wp_error = false, bool $fire_after_hooks = true ): int|WP_Error
Thêm bài viết mới vào cơ sở dữ liệu.
get
get_comment_meta( int $comment_id, string $key = ”, bool $single = false ): mixed
Lấy meta của bình luận.
get
get_sidebar( string $name = null, array $args = array() ): void|false
Lấy nội dung sidebar.
get
get_search_query( bool $escaped = true ): string
Lấy từ khóa tìm kiếm hiện tại.
get
get_feed_link( string $feed = ” ): string
Lấy URL RSS feed.
get
get_permalink( int|WP_Post $post, bool $leavename = false ): string|false
Lấy URL liên kết tĩnh của bài viết.
get
get_the_title( int|WP_Post $post ): string
Lấy tiêu đề bài viết.
get
get_posts( array $args = null ): WP_Post[]|int[]
Lấy danh sách bài viết theo tham số.
get
get_current_user_id(): int
Lấy ID của người dùng hiện tại.
get
get_transient( string $transient ): mixed
Lấy giá trị transient từ cơ sở dữ liệu.
get
get_comments( string|array $args = ” ): WP_Comment[]|int[]|int
Lấy danh sách bình luận của bài viết.
get
get_term_meta( int $term_id, string $key = ”, bool $single = false ): mixed
Lấy giá trị meta của thuật ngữ trong taxonomy.
get
get_user_meta( int $user_id, string $key = '', bool $single = false ): mixed
Lấy một giá trị meta của người dùng.
get
get_post( int|WP_Post|null $post = null, string $output = OBJECT, string $filter = ‘raw’ ): WP_Post|array|null
Lấy một bài viết từ cơ sở dữ liệu.