WordPress插件学习笔记(一)

版权声明:本文由 高小调 创作,转载请带链接,侵权必究! https://blog.csdn.net/gaoben668/article/details/80011892

Part 零:前言

假设我们需要一个具有这样功能的插件:将评论信息保存至txt文件内.

那么需要做以下步骤的事情:

Part 一 :基础信息

/*
Plugin Name: gxd_comment
Plugin URI: http://gaoxiaodiao.com
Description: 评论加强,记录评论信息至txt文件.
Author: 高小调
Version: 1.0
Author URI: http://gaoxiaodiao.com
*/

Part 二:找动作钩子及其相关信息

动作钩子大全详见:https://codex.wordpress.org/Plugin_API/Action_Reference

在这里发现wp_insert_comment这个动作钩子比较符合我们的需求,进入其详细介绍页面:https://codex.wordpress.org/Plugin_API/Action_Reference/wp_insert_comment

我们发现,执行这个动作钩子的函数需要两个参数:$comment_id和$comment_object

$comment_id,一猜就知道是评论信息的id

$comment_object,是什么呢?

https://core.trac.wordpress.org/browser/tags/4.9.5/src/wp-includes/comment.php#L0

在1741-1800行,即可发现$comment_object里包含的东西....对本插件有用的信息有:

$comment_object->comment_post_ID //评论文章id

$comment_object->comment_content //评论文章内容

Part 三:写增强代码

function do_something($comment_ID, $comment_object){
		$fp = fopen(ABSPATH."/123.txt","a");
		fwrite($fp,"文章id:".$comment_object->comment_post_ID."\r\n");
		fwrite($fp,"昵称:".$comment_object->comment_author."\r\n");
		fwrite($fp,"内容:".$comment_object->comment_content."\r\n");
		fwrite($fp,"\r\n");
		fclose($fp);
}
add_action('wp_insert_comment','do_something',99,2);

猜你喜欢

转载自blog.csdn.net/gaoben668/article/details/80011892