Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Wordpress Core into own Scripts

I'm trying to "Import" the Wordpress core into an own script to use the functionality such as wp_query etc. I've created an script in a subdirectory (own framework) and want to extend it by wordpress, but everytime the script throws an error:

Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/taxonomy.php on line 333

such as (when I remove the add_action( 'init', 'create_initial_taxonomies', 0 )):

Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/post.php on line 1006

The non-object is the $wp_rewrite-object. I've echo'ed something and figured out that first $wp_rewrite is valid and at the next call not. I've changed nothing at the WP core files.

I try to include the core by calling:

    require_once(BASE_PATH . 'wp-load.php');

Has anybody some ideas for me?

thanks

like image 253
Denis Avatar asked Feb 01 '12 18:02

Denis


People also ask

Why should we never make a change to WordPress core or plugins?

Don't edit the core!When the WordPress core gets updated, it overwrites the core installation with any new updates included in the release. If the core has been chopped up and modified beforehand, it'll wipe out those changes. That means big sections of the installation will just stop working.

When should you added core WordPress files?

1- Wordpress Core Files are the files that are combined together to make Wordpress work and run on an environment. These files should not be modified or deleted in any Case. Complete Wordpress installation or instance is based on these files.

How do I manually replace WordPress core?

You can reinstall your core files in the WordPress dashboard if you are still able to log in. Log into your WordPress site at example.com/wp-login.php. Navigate to the Dashboard > Updates page. On the next page, click the Re-install version x.x.x button.


2 Answers

Short answer, do this:

define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');

Long answer, it's a subtle gotcha around importing scripts with PHP.

If you define a local variable, outside of all functions, then it can be retrieved inside a function using 'global'. If you have a local variable inside a function, it cannot be retrieved later using global, unless it is defined as being global there and then.

The script 'wp-settings.php' is where the issue lies. It is included via your call to include 'wp-load.php'.

The variables defined there are not stated as being global; instead this is presumed because the script is always run outside of any functions, and so are automatically global. i.e.

$wordpress = 'foo';

function wordpressFunction() {
    global $wordpress;
}

Because you are importing the script within a function, they now become local variables. You are essentially doing:

function myFramework() {
    $wordpress = 'foo';

    function wordpressFunction() {
        global $wordpress;
    }
}

So the fix is to define them as global yourself before importing the script. Now $wp_query, and the others defined as global, are correctly found.

like image 123
JL235 Avatar answered Oct 25 '22 10:10

JL235


The easiest way to access everything wordpress has programmed in is to use the following:

require_once('/../../../wp-blog-header.php'); // Use actual root path to wp-blog-header.php
header("HTTP/1.0 200 OK");

Using the above code you'll get all functions you would normally get using a template with in WordPress. I've tried all the other methods listed above and this one is by far the best.

like image 34
Intracta Avatar answered Oct 25 '22 12:10

Intracta