通过wordpress钩子实现增删改文章后执行相应动作

How to use wordpress hook functions to follow the file change events

wordpress hook

hook是wordpress实现功能扩展的重要手段,详细的介绍:
hook functions is the important methods of extention of wordpress, reference to:
Plugin API

hook主要分两类:actions 和filters。
There have two catagory of hooks: actions add filters.
对应的注册函数分别为:add_action()和add_filter()。

目前支持的hook类型参见:
The details of hook cattagory refoerence to:
http://adambrown.info/p/wp_hooks

例子(Some examples)

  • 在模板的基础函数文件中(Modify the theme's style file):
    /data/wp/wordpress/wp-content/themes/premium-style/functions.php

  • 新增以下代码( Add some codes as bellow):

    function publish_post_extra($post_ID){
    // 1. init curl object
    $ch = curl_init();
    // 2. settings
    curl_setopt($ch, CURLOPT_URL, "https://xxx" . $post_ID);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // 3. execute the curl function
    curl_exec($ch);
    }
    add_action('publish_post', 'publish_post_extra');
    add_action('deleted_post', 'publish_post_extra');

以上的代码实现动作发生时, 通过curl访问一个特定url。
The uper codes show the publish and delete hook fuction that triger the specific http get request through the curl lib.