今天运气挺背的,上午收到一个骗子的短信,说是包裹到了,我一看广州的,还真以为是广州邮政呢,因为我们学校收到的邮件都是叫你去拿的。结果打过去他说什么云南昆明的包裹有什么违禁物品,听到这里我叫了一声“违你没!”就挂了。哎,现在骗子太多,傻逼显然是不够用了啊。
以前看到wordpress的钩子来开发插件很神奇,今天就来看看,add_filter等几个函数到底是怎么回事。
wordpress的钩子技术的核心无非就是php对回调函数的使用,想了解php回调函数的使用看这篇日志:
php编程技术之回调函数
首先来看看wp-includes下面的plugin.php,我们看到函数add_filter的定义:
function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { global $wp_filter, $merged_filters; $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); unset( $merged_filters[ $tag ] ); return true; }
可以看到该函数是把我们要hook的函数的一些变量存储在global $wp_filter这样一个全局数组中的,包括$tag,即我们要hook的函数,$priority即hook的顺序,还有就是$idx这个就是我们自定义函数的名称了。
值得注意的是如果我们在使用add_filter这个函数的时候并未指定顺序的话,那么默认的就是10了,这个在插件开发的时候值得注意一下,如果不想和其它插件产生冲突的话。
好了,加入到了这个之后又是怎么执行的呢,例如我们调用the_content()来显示文章内容,我们找到这个函数看看:
function the_content($more_link_text = null, $stripteaser = 0) { $content = get_the_content($more_link_text, $stripteaser); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content; }
是的,你看到了,它用get_the_content函数获取到文章内容后,将内容放到apply_filter函数中进行所谓的”过滤”,即是对文章内容做一些改变,然后显示返回的内容。
那么我们就知道了在add_filter函数把我们自定义的函数加入之后,就需要apply_filter里面按循序执行了,我们来看看这个函数:
function apply_filters($tag, $value) { global $wp_filter, $merged_filters, $wp_current_filter; $args = array(); $wp_current_filter[] = $tag; // Do 'all' actions first if ( isset($wp_filter['all']) ) { $args = func_get_args(); _wp_call_all_hook($args); } if ( !isset($wp_filter[$tag]) ) { array_pop($wp_current_filter); return $value; } // Sort if ( !isset( $merged_filters[ $tag ] ) ) { ksort($wp_filter[$tag]); $merged_filters[ $tag ] = true; } reset( $wp_filter[ $tag ] ); if ( empty($args) ) $args = func_get_args(); do { foreach( (array) current($wp_filter[$tag]) as $the_ ) if ( !is_null($the_['function']) ){ $args[1] = $value; $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); } } while ( next($wp_filter[$tag]) !== false ); array_pop( $wp_current_filter ); return $value; }
你又看到了原来保存我们hook函数信息的全局变量$wp_filter了,该函数已经很明显的说明是利用call_user_func_array()这个可以回调我们自己构造的函数的函数来使我们的函数执行了,只不过做了一些循环,使得回调循序等符合我们的要求罢了。
至于remove_filter不用看也知道了,就是把$wp_filter中的一组数据删了而已了,你可以自己去看看,用的就是unset($GLOBALS[‘wp_filter’][$tag][$priority])
另外大家很奇怪add_action和add_filter有什么区别,我们来看看add_action函数:
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { return add_filter($tag, $function_to_add, $priority, $accepted_args); } function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { return remove_filter($tag, $function_to_remove, $priority, $accepted_args); }
哈哈,看完这个我就觉得有点受骗的感觉,尼玛add_action就是调用add_filter啊,这两个函数根本就木有什么区别哦!!!!网上的其他说明两者不同的说法真的若爆了,有木有?这两个函数就一样的啦!此为3.1.2版本的,不知道其它版本有木有不同的。
如无特别说明,本博客文章皆为原创。转载请说明,来自吵吵博客。
原文链接:http://chaochaoblog.com/archives/956
吵吵微信朋友圈,请付款实名加入:
啊,那这种以包裹的形式来骗人的把戏还真要注意下啊!
不是吧 我在微博有发过这个 好多人都收到了这些短信 但是都是直接无视的