网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

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

GG网络技术分享 2025-03-18 16:12 2


问题描述:

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功能,但似乎无法正常工作。 该文件位于根目录中。 请帮帮我</ p>

</ div>

网友观点:

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];

}

?>

###

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.

###

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

标签:

提交需求或反馈

Demand feedback