【问题标题】:Wordpress custom hookWordpress 自定义钩子
【发布时间】:2013-05-01 00:18:35
【问题描述】:

问题在于我不了解 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 wordpress action hook


    【解决方案1】:

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

    <?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

    &lt;?php if(class_exists('MyAuthHookClass')) MyAuthHookClass::m_authenticate(); ?&gt;

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多