dsshsta97935
2018-11-13 13:58 阅读 141
已采纳

在自定义的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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    douciwang6819 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 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.

    点赞 评论 复制链接分享
  • dongshuanggu4333 我在西湖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

    点赞 评论 复制链接分享

相关推荐