【问题标题】:wordpress plugin -> Call to undefined function wp_get_current_user()wordpress 插件 -> 调用未定义的函数 wp_get_current_user()
【发布时间】:2011-09-01 22:09:19
【问题描述】:

我正在尝试使用 func wp_get_current_user() 在我的插件中获取当前用户信息。但我越来越 Call to undefined function wp_get_current_user()

显然这是因为包含该函数的文件/wp-includes/pluggable 直到加载插件后才会加载。

有人对如何在我的插件中获取用户详细信息有任何想法吗?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    显然这是因为包含该函数的文件 /wp-includes/pluggable 直到加载插件后才会加载。

    确实如此。因此,将你正在做的任何事情都包装在一个函数中,并将其挂接到 plugins_loaded 或 init 钩子上。 (参见 wp-settings.php 文件)

    示例:

    add_action('init','do_stuff');
    function do_stuff(){
      $current_user = wp_get_current_user();
      // ...
    }
    

    【讨论】:

    • 就像一个魅力。我正在研究一个在构造上加载用户详细信息的类。所以这是其他人的代码。我现在有:`add_action('plugins_loaded','construct_my_class');函数construct_my_class(){ $controller = new myClass(); }
    • @Prakash 取决于您的插件架构。对于大多数 wordpress 开发,我想它会放在插件 index.php 文件中
    • @denis de Bernardy - 现在我要使用这个吗?我正在尝试将 add_meta_box 仅“限制”为管理员 ... include(ABSPATH . "wp-includes/pluggable.php") 为我工作,但你说它会破坏 WP ...
    【解决方案2】:

    你可以用这个,

    <?php
    if(!function_exists('wp_get_current_user')) {
        include(ABSPATH . "wp-includes/pluggable.php"); 
    }
    ?>
    

    这应该可以解决您的问题:)

    【讨论】:

    • @DenisdeBernardy 我想实施这个解决方案,但我想知道,这将如何破坏 WP?
    • @Lumo5:我已经很多年没有接触过 WP 了,但我突然想到了 a) WP 是否总是使用 include_once 包含该文件? b) 在添加可插入功能(特别是在进行正确身份验证之前与身份验证和权限管理相关的功能)时,您是否最终不会打开安全问题? c)OP想要做的事情有一个钩子。
    • @DenisdeBernardy 改用哪个钩子更好?
    【解决方案3】:

    请用这段代码解决我的问题

    include_once(ABSPATH . 'wp-includes/pluggable.php');
    

    【讨论】:

      【解决方案4】:

      在安装 wp 3.8 之后,我在使用 ajax 获得的页面上遇到了同样的问题。我用以下代码修复了它:

      if(!function_exists('wp_delete_user')) {
          include(ABSPATH . "wp-admin/includes/user.php.");
      }
      

      显然该函数已从 pluggable.php 移至 user.php。我仍然不明白为什么在我包含 wp-blog-header.php 后它不起作用。

      【讨论】:

        【解决方案5】:

        也尝试添加

        require_once('../../../wp-load.php');
        

        随着

        require_once(ABSPATH.'wp-includes/pluggable.php');
        

        【讨论】:

          【解决方案6】:

          更新 WP 后我收到了同样的错误消息。对我有用的修复方法既快捷又简单:

          在 wp-includes 目录中找到 capabilities.php (WP 3.8.x)。 在开头的 php 标记之后添加以下内容:

          require_once('pluggable.php');
          

          【讨论】:

          • 您不认为这违反了更改核心文件的最佳做法。
          • 在我的情况下它会破坏 WP :)
          【解决方案7】:

          不是wp-includes,而是:

          include_once(ABSPATH . "wp-admin/includes/plugin.php");
          

          【讨论】:

            【解决方案8】:

            这听起来很疯狂,但我的应用程序中的问题正在发生,因为我有一个名为 menu.php 的文件,其中我有一个类来创建 Wordpress 菜单。

            从字面上看,只需将文件的名称从 menu.php 更改为 nav-menu.php,即可解决问题。我将问题重复了 3 次,因为我无法相信 FILE 的名称可能是问题所在。

            以防万一有人想知道该文件中的内容,这里是:

            class OwNavMenu extends OwCpnt 
            {
                function __construct( $location, $args ) {
                    $show = $args['show'];
                    $span = $args['span'];   
            
                    if ( $show ) {
                        $this->menu( $location, $span );
                    }     
                }
            
                function menu( $location, $span ) {
                    if ( $location ) {
                        echo '<div id="ow-' . $location . '" class="ow-nav ow-' . $location . '">';
                            wp_nav_menu(
                                array(
                                    'theme_location'  => $location,
                                    'link_before'     => ( $span ) ? '<span>'  : '',
                                    'link_after'      => ( $span ) ? '</span>' : ''
                                )
                            );
                        echo '</div>';
                    }        
                }
            }
            

            【讨论】:

              【解决方案9】:

              尝试在插件 PHP 文件中使用它。

              if ( !function_exists('wp_get_current_user') ) {
                  include(ABSPATH . "wp-includes/pluggable.php"); 
              }
              
              $current_user = wp_get_current_user();
              $user=esc_html( $current_user->ID );
              

              【讨论】:

                【解决方案10】:

                快速修复include_once(ABSPATH . 'wp-includes/pluggable.php'); 在您的capabilities.php 中添加这一行

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-04-24
                  • 2020-04-24
                  • 2014-02-13
                  • 2013-08-20
                  • 1970-01-01
                  • 2023-03-07
                  • 2020-01-08
                  相关资源
                  最近更新 更多