To use any core functions of WordPress such as database query, theme, and hook we need to write code in a theme’s template file or in a plugin. There are cases developers have to use a separate PHP file or a framework alongside WordPress to do certain things. That’s why we need a way to access WordPress functions from outside.
It is kind of simple to add this feature to a PHP file. What you need to do is just include the wp-load.php
file at the start of the file.

Let’s create a file called external.php
in the root folder of WordPress. Here is its content:
<?php include('wp-load.php'); //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>"; } ?>
After adding include('wp-load.php')
, you can call any functions like you are wiring a WordPress plugin.