dsshsta97935
2018-11-13 13:58
浏览 215
已采纳

在自定义的php文件中调用WordPress内置函数

I am trying to use WordPress functions on a custom file but doesn't seems to be working. The file is placed in root directory. Please help me out

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在自定义文件上使用WordPress功能,但似乎无法正常工作。 该文件位于根目录中。 请帮帮我

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douciwang6819 2018-11-13 15:28
    已采纳

    In any custom page you can call below three lines to pull up WordPress stack and then use any WordPress functionality

    <?
        //Imp to include
        include('wp-load.php');
        define('WP_USE_THEMES', false);
        require('wp-blog-header.php');
    
        // check is user is logged - if yes then print its role
        if(is_user_logged_in() ) {
            $user = wp_get_current_user();
            $role = ( array ) $user->roles;
            echo "role is ".$role[0];
    
        }
    ?>
    
    打赏 评论
  • dongpo5264 2018-11-13 14:03

    Just include wp-load.php file at the top of the custom PHP file

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

    Note: add slashes and dots ../../../../ as per the custom PHP file exists.

    打赏 评论
  • 我在西湖1 2018-11-13 14:57

    You can access any wordpress inbuilt function in any external file by adding wp-load.php file. If your file is in the root directory. Then, you can just add following line of code on top.

    require_once("wp-load.php");
    

    For more details and example, you can visit How to access Wordpress functions in external file

    打赏 评论

相关推荐 更多相似问题