WordPress的自定义钩子
问题描述:
这个问题发生在我不知道WordPress的解剖结构(这是我第一次使用WP),而且任务应该紧急解决。我开发了一个不受欢迎的openID提供者的openID授权插件。我开发了插件,就好像它在'init'钩子上调用main函数一样。因此,没有任何非授权用户可以访问任何网站页面。但任务是我必须在我的索引页面上有“授权”链接,授权必须在点击此链接后立即执行。我尝试用自定义钩子来解决问题。WordPress的自定义钩子
我添加以下到我的插件主文件(它是一个类函数)
public function m_authenticate(){
do_action('m_authenticate');
}
比增加像
add_action('m_authenticate', array(&$this, 'mAuthenticate'), 7);
这里mAuthenticate
的动作会被重定向到OpenID提供商的功能AUTH。 之后,我创建了一个PHP脚本与此挂钩
<?php
m_authenticate();
但在调用由浏览器这个脚本,它发生该脚本不能找到一个功能m_authenticate()
错误。如果我需要脚本,其中定义了m_authenticate()
,那么对于此脚本的其他行会发生错误,例如, cannot find function
on line
register_activation_hook(__FILE__, array('some_func', 'addNewWordPressAdmin'));
请帮助建议如何使用此openID功能制作单独的页面。任何帮助将不胜感激。
答
以下是解决此问题的一种方法。在这里,您有与类插件:
class MyAuthHookClass {
public function __construct() {
add_action('m_authenticate', array($this, 'mAuthenticate'));
}
public function mAuthenticate() {
echo "<h1>My Auth</h1>";
}
public static function m_authenticate(){
do_action('m_authenticate');
} }
new MyAuthHookClass();
而在主题结构我把header.php
钩:
<?php if(class_exists('MyAuthHookClass')) MyAuthHookClass::m_authenticate(); ?>