常用函数参考
WordPress 提供了大量内置函数,本文档涵盖最常用的函数分类参考。
📚 函数分类
| 分类 | 说明 |
|---|---|
| 文章函数 | 文章相关操作 |
| 用户函数 | 用户相关操作 |
| 分类函数 | 分类和标签操作 |
| URL 函数 | URL 和链接操作 |
| 内容函数 | 内容显示和格式化 |
| 安全函数 | 安全和转义 |
📝 文章函数
获取文章信息
php
<?php
// 获取文章 ID
the_ID(); // 输出当前文章 ID
get_the_ID(); // 返回当前文章 ID
get_the_ID($post); // 返回指定文章 ID
// 获取文章标题
the_title(); // 输出当前文章标题
the_title($before, $after); // 带前后标签
get_the_title($post); // 返回标题
// 获取文章内容
the_content($more_link_text, $strip_teaser);
get_post_field('post_content', $post_id);
// 获取文章摘要
the_excerpt();
get_the_excerpt($post);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
文章元数据
php
<?php
// 获取特色图片
has_post_thumbnail($post);
the_post_thumbnail($size, $attr);
get_the_post_thumbnail($post, $size, $attr);
// 自定义字段
get_post_meta($post_id, $key, $single);
update_post_meta($post_id, $meta_key, $meta_value);
delete_post_meta($post_id, $meta_key);
add_post_meta($post_id, $meta_key, $meta_value);
// 文章日期
the_date($format, $before, $after, $echo);
the_time($format);
get_the_date($format, $post);
the_modified_date($format);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
👤 用户函数
用户信息
php
<?php
// 获取当前用户
$current_user = wp_get_current_user();
$current_user->ID; // 用户 ID
$current_user->user_email; // 用户邮箱
$current_user->display_name; // 显示名称
// 获取用户数据
get_userdata($user_id);
get_user_by($field, $value); // 'id', 'slug', 'email', 'login'
// 用户元数据
get_user_meta($user_id, $key, $single);
update_user_meta($user_id, $meta_key, $meta_value);
// 用户检查
is_user_logged_in();
current_user_can($capability);
current_user_can('edit_post', $post_id);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
用户角色和权限
php
<?php
// 用户角色
get_roles();
wp_get_current_user()->roles;
// 角色检查
user_can($user, $capability);
add_cap($role, $cap);
remove_cap($role, $cap);1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
🏷️ 分类函数
分类操作
php
<?php
// 获取分类
get_categories($args);
get_category($cat_id);
get_category_by_slug($slug);
get_the_category($post_id);
wp_get_post_categories($post_id, $args);
// 输出分类
the_category($separator, $parents, $post_id);
single_cat_title($prefix, $display);
// 判断函数
in_category($category, $post);
has_category($category, $post);
is_category($category);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
标签操作
php
<?php
// 获取标签
get_tags($args);
get_tag($tag_id);
get_the_tags($post_id);
wp_get_post_tags($post_id, $args);
// 输出标签
the_tags($before, $sep, $after);
single_tag_title($prefix, $display);
// 判断函数
has_tag($tag, $post);
is_tag($tag);1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
自定义分类
php
<?php
// 获取分类法项目
get_terms($taxonomies, $args);
get_term($term, $taxonomy);
get_the_terms($post, $taxonomy);
// 获取分类法名称
get_taxonomy($taxonomy);
get_taxonomies($args);1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
🔗 URL 函数
链接获取
php
<?php
// 站点 URL
get_site_url($blog_id, $path, $scheme);
get_home_url($blog_id, $path, $scheme);
site_url($path, $scheme);
home_url($path, $scheme);
// 文章链接
get_permalink($post);
the_permalink();
get_post_permalink($post_id, $leavename);
// 评论链接
get_comment_link($comment);
get_comments_link();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
URL 操作
php
<?php
// 获取当前 URL
curPageURL();
$_SERVER['REQUEST_URI'];
// 添动移除 URL 参数
add_query_arg($key, $value, $url);
remove_query_arg($key, $url);
// 重定向
wp_redirect($location, $status);
wp_safe_redirect($location, $status);1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
📄 内容函数
内容显示
php
<?php
// 格式化内容
wpautop($text, $br); // 自动添加段落
wptexturize($text); // 转换引号等
wpautop($foo, $br = true); // 自动换行
// 内容辅助
wp_trim_words($text, $num_words, $more);
wp_strip_all_tags($string, $remove_breaks);
strip_shortcodes($content);
// 内容过滤
apply_filters('the_content', $content);
wp_filter_oembed_result($result);1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
分页
php
<?php
// 文章分页
wp_link_pages($args);
posts_nav_link($sep, $prelabel, $nextlabel);
// 评论分页
paginate_comments_links($args);
get_the_comments_pagination($args);1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
🛡️ 安全函数
数据验证
php
<?php
// 文本验证
sanitize_text_field($str);
sanitize_textarea_field($str);
sanitize_file_name($filename);
sanitize_title($title);
sanitize_title_with_dashes($title);
sanitize_key($key);
sanitize_html_class($class);
// 邮箱验证
sanitize_email($email);
is_email($email);
// URL 验证
esc_url($url, $protocols, $_context);
esc_url_raw($url, $protocols);
esc_raw_url($url, $protocols);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
数据转义
php
<?php
// HTML 转义
esc_html($text);
esc_attr($text);
esc_js($text);
// URL 转义
esc_url($url);
esc_html__($text, $domain);
esc_attr_e($text, $domain);
// 内容转义
wp_kses($string, $allowed_html, $allowed_protocols);
wp_kses_post($string);
wp_strip_all_tags($string, $remove_breaks);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Nonce 验证
php
<?php
// 创建 nonce
wp_create_nonce($action);
wp_nonce_field($action, $name, $referer, $echo);
wp_nonce_url($url, $action, $name);
// 验证 nonce
wp_verify_nonce($nonce, $action);
check_ajax_referer($action, $query_arg, $die);
wp_referer_field($echo);1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
SQL 注入防护
php
<?php
global $wpdb;
// 准备语句
$wpdb->prepare($query, $arg1, $arg2);
// 占位符类型
// %s - 字符串
// %d - 整数
// %f - 浮点数
// %F - 浮点数(非本地)
// %b - 二进制(不转义)
$wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_title = %s AND ID = %d",
$title,
$id
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
📊 数据库操作
$wpdb 基础
php
<?php
global $wpdb;
// 选择查询
$wpdb->get_results($query, $output);
$wpdb->get_row($query, $output, $y);
$wpdb->get_col($query, $col_offset);
$wpdb->get_var($query, $x, $y);
// 插入数据
$wpdb->insert($table, $data, $format);
$wpdb->replace($table, $data, $format);
// 更新数据
$wpdb->update($table, $data, $where, $format, $where_format);
// 删除数据
$wpdb->delete($table, $where, $where_format);
// 直接查询
$wpdb->query($query);
// 引用
$wpdb->prepare();
$wpdb->escape($str);
$wpdb->escape_by_ref(&$str);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
🏗️ 辅助函数
选项 API
php
<?php
// 获取选项
get_option($option, $default);
get_site_option($option, $default);
// 更新选项
update_option($option, $newvalue);
update_site_option($option, $newvalue);
// 删除选项
delete_option($option);
delete_site_option($option);
// 自动加载选项
add_option($option, $value, $deprecated, $autoload);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
瞬态/缓存
php
<?php
// 设置获取删除瞬态
set_transient($transient, $value, $expiration);
get_transient($transient);
delete_transient($transient);
// 站点瞬态
set_site_transient($transient, $value, $expiration);
get_site_transient($transient);
delete_site_transient($transient);1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
国际化
php
<?php
// 翻译函数
__('Text', 'text-domain');
_e('Text', 'text-domain');
_x('Text', 'context', 'text-domain');
_ex('Text', 'context', 'text-domain');
// 复数
_n($single, $plural, $number, $domain);
_n_noop($singular, $plural, $domain);
// 转义翻译
esc_html__($text, $domain);
esc_html_e($text, $domain);
esc_attr__($text, $domain);
esc_attr_e($text, $domain);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
