How to include PHP file in WordPress that is outside the WordPress Environment

I am using a custom CMS for a website that deals with 90% of the site’s pages and content.

However, for the news/blog section of the site, I am using WordPress.

Read More

I’m able to include the necessary WordPress files to pull in posts to the main site, but cannot do the reverse.

I want to be able to call a php file into the header of WordPress so I have access to the CMS’s classes, primarily for navigation.

I’ve tried a search but can’t seem to find an answer.

Additional Information:

  • WordPress is installed in site.com/wordpress
  • The file I want to include is in site.com/includes

Clarification:

I don’t want to create a custom template or include WordPress functionality outside of WordPress. I want to pull external classes from a CMS, which is a level above WordPress, into WordPress so that I can use it throughout the WordPress installation.

Related posts

3 comments

  1. You should go with custom page template.

    Its an easy way to include php file in wordpress because in thisway you can easily include wordpress theme header and footer.

    To create a custom page template.

    You have to simply create new .php page and at start of page just include this code.

    <?php
    /*
    Template Name: My custom page template name
    */
    ?>
    // your code 
    

    To read in detail about WordPress custom page template click here

    I hope it will help you.

  2. Method 1 :

    <?php
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');
    ?>
    

    Method 2 :

    <?php
    define('WP_USE_THEMES', false);
    require('./wp-load.php');
    ?>
    
  3. I managed to fix this by altering the include line to reference the folder above the WordPress root.

    So, the line became require_once "../path/to/file" instead of just require_once "/path/to/file"

Comments are closed.