WordPress 自带的 RESTful API 内置的身份验证方法是 Cookie Authentication ,登录仪表板时,会生成cookie。为了防止CSRF,需要在请求的地址后面,加上一个 nonce 参数“_wpnonce”。
CSRF请参考:https://javascript.net.cn/article?id=683
nonce生成,参考:https://vtheme.cn/archives/136
如果想使用JWT(JSON Web Token),官方建议使用插件 https://wordpress.org/plugins/jwt-authent
WordPress 可以自由组织代码结构,这篇文章介绍我项目中路由相关的代码结构。
一,添加路由
相关函数
1、rest_api_init 钩子函数,注册接口的相关信息需要挂载到此钩子上。
2、register_rest_route 接口路由函数
add_action( 'rest_api_init', 'first_route_hook' );
function first_route_hook() {
register_rest_route( 'rangtuo/v1', 'test/(?P id [\d]+)', [
'methods' = 'GET',
'cal
在开发 WordPress RESTful API 时,用来返回结果集。可以修改 http 请求的状态码。$data = array( 'some', 'response', 'data' );
// Create the response object
$response = new WP_REST_Response( $data );
// Add a custom status code
$response- set_status( 201 );
// Add a custom header
$response- header( 'Location', 'http://exampl
WordPress 5.6 以后内置了RESTful API,并且建议用户一定不要关闭,否则会影响WordPress后台的某些功能。调用某些接口的时候,会提示{
code: "rest_cannot_edit",
message: "抱歉,您不能编辑此用户。",
data: {status: 401}
}
Cookie Authentication 是 WordPress 内置的身份验证方法。登录仪表板时,会生成cookie。 但是,REST API包含一种称为nonce的技术来避免CSRF的问题。使用内置Javascript API的开发人员,WordPress 会自动处理 no
WordPress的插件目录在 wp-content/plugins/ 下,一个插件一个目录,安装插件时可以把解压的插件上传到这个目录下或者在wordpress后台安装插件。plugins_url()echo plugins_url();
//输出:https://rangtuo.com/wp-content/plugins
echo plugins_url('',__FILE__);
//输出:https://rangtuo.com/wp-content/plugins/myplugin
echo plugins_url('js/myscript.js',__FILE__);
//输出:ht
什么是 WordPress 插件 ?插件( Plugins )是如何与 WordPress 交互的WordPress 为插件提供了多种丰富的 APIs。每一种 API( 应用程序接口 )使插件和 WordPress 以不同的方式交互。下面是 WordPress 提供的主要 APIs 以及他们的功能列表:插件: 给插件提供一系列的钩子( hooks )来使用 WordPress 的相关部分。WordPress 包含两种不同类型的钩子: 动作( Actions )和过滤器( Filters )。动作让你可以在运行时的特定时刻触发自定义的插件代码。例如,可以在用户在 WordPress 中注册了一个
当需要访问一个路径调用相应的模板时,可以使用下面的方法:function rt_rewrite_rules( $wp_rewrite ) {
$rt_rules = [
'orders(.*)$' = 'index.php?rt_custom_page=orders',
'users/([^/]*)/collections/?' = 'index.php?rt_custom_page=collections',
'users/([^/]*)/?' = 'index.php?rt_custom_page=user-center&
WordPress 循环读取文章数据的方法一般使用 query_posts(),但是 query_posts() 这个函数有一些缺点,可能会干扰那些用到的Loop(循环)的插件。
可能使一些 WordPress 条件标签失效。这时候最好的选择就是使用 WP_Query 类了。
用法:
$args = array('post_type'= 'post');
$the_query = new WP_Query( $args );
if ( $the_query- have_posts() ) {
echo ' ul ';
while ( $the_query- have_pos
1, bloginfo(‘template_url’)输出:http://localhost/wordpress/wp-content/themes/mytheme2, bloginfo(‘template_directory’)输出:http://localhost/wordpress/wp-content/themes/mytheme3, get_template_directory_uri()输出:http://localhost/wordpress/wp-content/themes/mytheme4, get_stylesheet_directory_uri()输出:http://l
sanitize_user( $username, $strict ) 简介清理用户名,去掉所有不安全的字符。删除 HTML 标签,8进制,HTML实体,如果 $strict 参数设置为 true,将删除所有非 ASCII 字符,只保留数字,字母,_,空格,.,-,@。参数$username (string) (required) 要清理的用户名。Default: None$strict (boolean) (optional) 如果设置为 true,限制 $username 为数字,字母,_,空格,.,-,@。Default: false返回值(string)The sanitized us
wp_signon()函数用于授权给用户登陆wordpress并可记住该用户名称。该函数取代了wp_login。WordPress 2.5版本起启用。验证成功返回WP_User信息,验证失败则返回WP_Error的信息,该函数用于开发WordPress 登录功能。函数结构wp_signon($credentials, $secure_cookie)
参数说明:$credentials – (数组)(可选),提交的登录信息,如用户名、密码,默认值为空数组array()。$secure_cookie – (字符串或布尔型)(可选),是否使用安全Cookie,默认值为空。如果$credentials
WordPress 默认调用头像的函数是 get_avatar(),在开发中可以使用这个函数完成自定义头像。add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 );
function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt) {
if ( ! empty( $id_or_email- user_id ) ) {
$avatar = get_bloginfo('template_directory') . '/assets/imag
一,根据查询字符串排序WordPress默认排序是按照文章发布时间的,有时候我们需要按照其他方式来排序,或者提供其他方式排序的功能。如果只是基本的排序,比如按照修改时间,或者按照评论数之类的,不需要做任何改动,直接在url加上orderby参数就可以的。比如就是http://test.com/?orderby=comment_count //按评论数量排序http://test.com/?orderby=modified //按修改时间http://test.com/?orderby=rand //随机排序http://test.com/?orderby=ID //ID大小按照浏览量 http
WordPress文章编辑页,设置文章置顶,还需要修改文章调用函数。
?php
$sticky_arr = get_option( 'sticky_posts' );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// rsort( $sticky ); // 文章数组逆向排序
// $sticky = array_slice( $sticky, 0, 3); // 限制只有3个置顶文章
$args = array(
'cat' = $cat,
'post__in' = $sticky_a
参考主题WordPress自带主题, twentytwentyone,在文章模版下面,有个函数 : ?php comments_template(); ?
这个函数会引用主题下的 comments.php,在该文件的底部有个函数 comment_form 函数,就是用来生成表单的。//Declare Vars
$comment_send = 'Send';
$comment_reply = 'Leave a Message';
$comment_reply_to = 'Reply';
$comment_author = 'Name';
$comment_email = 'E-Mail';
get_categories() 函数是 WordPress 提供的一个简单方法,用于获取所有分类(categories)的信息。这个函数返回一个包含分类对象的数组,每个对象都包含了分类的详细信息,如 ID、名称、描述、链接等。以下是如何使用 get_categories() 的一个基本示例:
?php
$categories = get_categories();
if ($categories) {
foreach($categories as $category) {
echo ' h2 a href="' . get_category_link( $cat
在页面中,常常需要对分类添加各种字段,使用下面方法,可以添加自定义字段。字段类型可以是text或者radio各种类型。 // 分类添加字段
function ems_add_category_field(){
echo ' div class="form-field"
label for="cat-keywords" 关键词 /label
input name="cat-keywords" id="cat-keywords" type="text" value="" size="40"
p 输入关键词 /p
/div ';
}
add_action('cate
为什么要使用favicon图标打开网页时,浏览器中标签中会显示favicon图标收藏网站时,收藏夹会显示favicon图标创建快捷方式时,favicon会作为网站快捷方式的桌面图标WordPress添加favicon图标方法图片制作好了以后,就可以上传到网站作为浏览器图标了,这里我们为大家准备了4种方法,你可以任意挑选一个自己喜欢的使用方法一:使用WordPress自带的自定义模块打开WordPress的主题自定义模块,仪表盘 – 外观 – 自定义选择 站点身份 – 站点图标,上传自己刚刚制作的favicon图标方法二,制作favicon.ico图标使用ico转换工具把设计好的图片直接转换成
WordPress 默认情况下,生成多个文章id,导致不连续,且数据库冗余。会造成连续的两篇文章,ID数值可能会相差很多wp-congfig.php添加define( 'AUTOSAVE_INTERVAL', false ); //禁用历史修订版本
define('WP_POST_REVISIONS', false ); //自动保存时间设置超过一天
使用 tag__in 可以调用有特定标签的文章。//获取一个标签的文章 根据标签别名获取有这个标签的文章:
$query = new WP_Query( 'tag=cooking' );
//根据标签 ID 获取有这个标签的文章:
$query = new WP_Query( 'tag_id=13' );
//获取多个标签的文章 根据标签的别名获取带有这几个标签其中一个的文章:
$query = new WP_Query( 'tag=bread,baking' );
//根据标签别名获取同时拥有几个标签的文章:
$query = new WP_Query( 'tag=bread+baking+
所有标签