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.

//Include the wp-load.php file
//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
// Edit your code below


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

get_footer(); ?

Last Update on:March 5th, 2023 at 5:19 pm

How to create dynamic robots txt file in WordPress without plugin
Previous post