How to access WordPress functions in external file

WordPress functions are very useful and solve lots of issue. But normally, we can access it only inside the wordpress files like themes, plugins etc.

Wouldn’t it be great if we can access these in other files also that are not the part of usual wordpress file structure.

Then, you will be happy to know that we can easily do it. All we need to do is just include the wp-load.php file at the starting of our external php file.

Suppose, if your file is in the root directory (index.php) and WordPress install in others directory like (BLOG). Then, you just have to add the following line of code on top.

?php
//Include the wp-load.php file
include('wp-load.php');
//As this is external file, we aren't using the WP theme here. So setting this as false
define('WP_USE_THEMES', false);
//Optional, adding the WP blog header which will include the wordpress head in the file
require('wp-blog-header.php');
// Edit your code below

get_header();

if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        // Your loop code
    endwhile;
else :
    _e( 'Sorry, no posts were found.', 'textdomain' );
endif;

get_footer(); ?

Leave a Reply

Your email address will not be published.