Skip to main content

How to Access WordPress Functions in an External PHP File

Many developers love WordPress because it is relatively easy to use and customize. However, there are some limitations to what you can do with WordPress without writing code.

To be able to use any of WordPress’s core functions, such as database query or theme customization we need code in either the template file for our site (if it is a single page) and/or plugins.

There are some cases where developers will have to write a separate PHP script alongside WordPress codes that manage different tasks – that’s why having an external way into those capabilities becomes important so you don’t get locked out from doing anything because another developer decided they wanted more control over how things worked!

By creating a custom function that accesses the WordPress core, we can extend the functionality of WordPress and make it even more powerful.

The solution is to include wp-load.php in the PHP file to be able to call WordPress methods.


//query user
if(is_user_logged_in() ) {
    $user = wp_get_current_user();
    echo "<p><b>Loggedin user:</b> {$user->data->user_login}</p>";
//query comments
global $wpdb;
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments"));
echo "<h3>Comments</h3>";
foreach ($comments as $key => $comment) {
    echo "<p>{$comment->comment_content}</p>";        

By continuing to use the site, you agree to the use of cookies.