一,WordPress 创建语言文件通常 WordPress 的语言文件在 ./wp-content/languages 下,文件的扩展名,是“mo”和“po”,po文件用程序msginit来分析pot文件,生成各语言对应的po文件,比如中文就是zh_CN.po,法语就是fr.po文件。PO是Portable Object(可移植对象)的缩写形式,它是面向翻译人员的、提取于源代码的一种资源文件。po文件可以用任何编辑器如poEdit,vi,Emacs,editplus打开,交给翻译人员来将其中的文字翻译成本国语言。mo文件用msgfmt将.po文件编译成mo文件,这是一个二进制文件,不能直接编
wpdb 类wpdb类封装了所有的数据库操作函数,它是基于开源的数据库操作类ezSQL进行修改的,使其更适合于WordPress,也使其仅适用于mySQL数据库。同时,WordPress还提供了一个全局变量$wpdb,并将其实例化为wpdb类的对象。这样我们就可以直接使用$wpdb来调用所有的数据库操作函数。注意 使用前 一定要 global $wpdb;query 函数最常用的函数,$query为SQL语句,提交给数据库执行,结果分两种情况:如果是insert、delete、update、replace, 返回受影响行数,在insert、replace这种情况下,该函数会用$this- in
一,WordPress 添加顶级菜单 add_menu_page()add_action('admin_menu', 'register_custom_menu_page');
function register_custom_menu_page(){
add_menu_page('菜单标题', '菜单名称', 'administrator', 'custompage', 'custom_menu_page', plugins_url('myplugin/images/icon.png'), 6);
}
function custom_menu_page(){
echo "A
一,函数介绍media_handle_upload( string $file_id, int $post_id, array $post_data = array(),array $overrides = array('test_form' = false) )
原型函数位于wp-admin/includes/media.php文件中。参数$file_id (string) (必须) 文件发送的 $_FILES 数组的索引。
$post_id (int) (必须) 要将媒体项目附加到的帖子的POST ID。必需的,但可以设置为0,创建与POST无关的媒体项。
$post_data (arr
简介WordPress户密码保存在wp_users数据表的user_pass字段,密码的形式是随机且不可逆,同一个明文的密码在不同时间,产生的密文也不一样。密码生成流程1、 随机产生一个salt 并将salt和password相加2、 进行了count次md5 然后和encode64的hash数值累加3、 最后得到一个以$P$开头的密码,这个密码每次产生的结果都不一样以下为在wordpress中调用密码生成的代码$password = '123';
global $wp_hasher;
if ( empty($wp_hasher) ) {
require_once( './wp-inc
一,WordPress 默认发送邮件的函数wp_mail( $to, $subject, $message, $headers = '', $attachments = array() )
参数:$to (string|array) (Required)收信人地址,多个收信人使用数组形式。$subject (string) (Required) 邮件主题$message (string) (Required) 邮件内容$headers (string|array) (Optional) 额外的头部。 Default value: ” $attachments (string|array) (O
主题激活时动作钩子 after_switch_theme为了实现一些主题的扩展功能,如创建数据表等操作。可以挂载函数到该钩子上。do_action( 'after_switch_theme', string $old_name, WP_Theme $old_theme )
如果旧主题仍然存在,则在主题切换后的第一个WP加载上触发。此操作多次触发,如果旧主题存在或不存在,则参数根据上下文的不同而不同。如果旧主题缺失,参数将是旧主题的片段。主题取消激活时动作钩子 switch_themedo_action( 'switch_theme', string $new_name, WP_Theme $n
一,WordPress 使用 register_nav_menus() 设置菜单位置导航菜单注册函数 register_nav_menus()在主题的 functions.php 中添加:if (function_exists('register_nav_menus')) {
register_nav_menus(array(
'header_main' = __('顶部菜单'),
'footer_bottom' = __('底部菜单')
));
}
上面注册了两个菜单位置,其中 ‘header_main’ 和 ‘footer_bottom’
一,使用 user_can() 函数验证角色权限user_can( $user- ID, 'edit_posts' );
user_can( $user- ID, 'edit_post', $post- ID );
user_can( $user- ID, 'edit_post_meta', $post- ID, $meta_key );
二,使用 current_user_can( $capability ) 验证当前用户权限current_user_can( 'edit_posts' );
current_user_can( 'edit_post', $post- ID );
curren
一,常用函数获取当前用户ID的方式:$user_ID = get_current_user_id();
通过邮箱获取用户信息的方式:$user = get_user_by( 'email', 'user@example.com' );
通过ID获取用户信息的方式:$user = get_user_by( 'id', '123' );
二,WP_User 类通过 WP_User 类获取用户信息$user = new WP_User($id);
方法:WP_User::get_data_by( string $field, string|int $value )
WP_User::exists()
服务端同时执行多条sql语句,保证业务逻辑完整性,需要用到数据库事务功能。WordPress数据库一般用的是MySql,数据库事务与原生用法大同小异,常用简单封装:/**
* 事务 - 开始
*/
function pury_custom_transaction_begin(){
global $wpdb;
return $wpdb- query("START TRANSACTION");
}
/**
* 事务 - 提交
*/
function pury_custom_transaction_commit(){
global $wpdb;
return $wpdb-
在 WordPress 主题模板制作的时候经常需要获取当前模板的文件夹服务器路径,因为经常忘的原因,所以在这里总结下几种方法,并且区分其之间的差别:1,bloginfo('template_url');http://localhost/wordpress/wp-content/themes/mytheme2,bloginfo('template_directory');http://localhost/wordpress/wp-content/themes/mytheme3,echo get_template_directory_uri(); http://localhost/wordpres
方法一:add_action( 'add_meta_boxes', 'vm_add_custom_box' );
add_action( 'save_post', 'vm_save_postdata' );
function vm_add_custom_box() {
//需要添加自定义字段的页面
$screens = array('post'); // 'post', 'page',
foreach ($screens as $screen) {
add_meta_box(
'html_meta_div_id',
__( '主题自定义字段', '
一, WordPress 创建语言文件通常 WordPress 的语言文件在 ./wp-content/languages 下,文件的扩展名,是“mo”和“po”,.po文件(1)用程序msginit来分析pot文件,生成各语言对应的po文件,比如中文就是zh_CN.po,法语就是fr.po文件。(2)PO是Portable Object(可移植对象)的缩写形式,它是面向翻译人员的、提取于源代码的一种资源文件。(3).po文件可以用任何编辑器如poEdit,vi,Emacs,editplus打开,交给翻译人员来将其中的文字翻译成本国语言。.mo文件(1)用msgfmt将.po文件编译成mo文件
WordPress 自带的 RESTful API 内置的身份验证方法是 Cookie Authentication ,登录仪表板时,会生成cookie。为了防止CSRF,需要在请求的地址后面,加上一个 nonce 参数“_wpnonce”。CSRF请参考:https://javascript.net.cn/article?id=683nonce生成,参考:https://verytheme.com/archives/136如果想使用JWT(JSON Web Token),官方建议使用插件 https://wordpress.org/plugins/jwt-authentication-for
add_theme_page()函数add_theme_page()函数会在后台“外观”菜单项下面新建一个菜单项,有助于我们自己修改主题时功能的扩展。语法结构add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function);
page_titile title标签的内容menu_title 显示在后台左边菜单的标题capability 访问这个页面需要的权限menu_slug 别名,需要独一无二哦自己命名function 执行的函数例子:# 加入主题的functions.php即可
function r
使用 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+
1,数据库相关表Wordpress 文章表中是没有文章点击次数这个字段的,所以把文章点击次数,保存在表 wp_postmeta 中。2, 通用函数/**
* 设置文章浏览次数
*/
function setPostViews($postID)
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($postID, $cou
简介Wordpress v4.4以后,已经内置了WP REST API。接口文档:https://developer.wordpress.org/rest-api/The WordPress REST API provides an interface for applications to interact with your WordPress site by sending and receiving data as JSON (JavaScript Object Notation) objects. It is the foundation of the WordPress Block
1、最直接的用法,在需要的位置放入下面的代码。 ?php
$args = array( 'numberposts' = 5, 'orderby' = 'rand', 'post_status' = 'publish' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?
li a href=" ?php the_permalink(); ? " ?php the_title(); ? /a /li
?php endforeach; ?
2、用query_posts生成随机文章