问题:Wordpress 自定义钩子

问题在于我不了解 WordPress 的结构(这是我第一次使用 WP),应该紧急解决该任务。我开发了一个由非流行的 openID 提供商之一进行 openID 授权的插件。我开发了插件,就好像它在“init”钩子上调用主函数一样。因此,任何未经授权的用户都无法访问网站的任何页面。但任务是我的索引页面上必须有“授权”链接,并且必须在单击此链接后立即执行授权。我尝试使用自定义钩子解决问题。

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

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

比添加了这样的动作

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

这里mAuthenticate是一个重定向到 openID 提供者进行身份验证的函数。之后我用这个钩子创建了一个 php 脚本

<?php
m_authenticate();

但是在浏览器调用这个脚本时,会出现脚本找不到函数m_authenticate()的错误。如果我需要其中定义了m_authenticate()的脚本,那么它会与该脚本的其他行发生错误,例如cannot find function上线

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

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

解答

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

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

Logo

更多推荐