Wordpress自定义钩子

时间:2013-05-07 06:15:38

标签: php wordpress action hook

问题出现的原因是我不知道WordPress的解剖结构(这是我第一次使用WP),任务应该紧急解决。我为一个非流行的openID提供程序开发了一个openID授权插件。我开发了插件,好像它在'init'钩子上调用main函数。因此,没有任何非授权用户可以访问任何网站页面。但任务是我必须在我的索引页面上“授权”链接,并且必须在点击此链接后立即执行授权。我尝试使用自定义钩子解决问题。

我在我的插件主文件中添加了以下内容(它是一个类函数)

public function m_authenticate(){
    do_action('m_authenticate');
}

比添加了那样的行动

add_action('m_authenticate', array(&$this, 'mAuthenticate'), 7);

此处mAuthenticate是一个重定向到autid的openID提供程序的函数。 之后我用这个钩子创建了一个php脚本

<?php
m_authenticate();

但是在通过浏览器调用此脚本时,会发生一个错误,即脚本无法找到函数m_authenticate()。如果我需要定义m_authenticate()的脚本,那么它与该脚本的其他行发生错误,例如cannot find function在线

register_activation_hook( __FILE__, array('some_func', 'addNewWordPressAdmin')); 

请帮助建议如何使用此openID功能创建单独的页面。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是解决此问题的一种方法。这里有一个类的插件:

<?php /* Plugin Name: WP Test */

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(); ?>

相关问题