Bootstrap WordPress

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Bootstrap WordPress

Robert Lusby
Howdy,

What do you guys consider as the current "best practise" way, to 'boot
strap' WordPress for use in another application/framework.

E.g., to give access to function suchs as "wp_insert_post", get_post and
the WPDB class, but without loading the theme?

Thanks,
Robert
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap WordPress

Otto-19
Include the wp-load.php file.

If you want just the basics and not all the various functions, you can
define SHORTINIT to true beforehand. But that's just the *very*
basics.

-Otto



On Thu, Apr 12, 2012 at 12:35 PM, Robert Lusby <[hidden email]> wrote:

> Howdy,
>
> What do you guys consider as the current "best practise" way, to 'boot
> strap' WordPress for use in another application/framework.
>
> E.g., to give access to function suchs as "wp_insert_post", get_post and the
> WPDB class, but without loading the theme?
>
> Thanks,
> Robert
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap WordPress

Muhammad Ali-10
<?php
require_once('../wp-load.php'); //Include wp-load.php, change to where
the file actually is relative.
//use Wordpress functions as standard.
?>

On 12 April 2012 19:53, Otto <[hidden email]> wrote:

> Include the wp-load.php file.
>
> If you want just the basics and not all the various functions, you can
> define SHORTINIT to true beforehand. But that's just the *very*
> basics.
>
> -Otto
>
>
>
> On Thu, Apr 12, 2012 at 12:35 PM, Robert Lusby <[hidden email]> wrote:
>> Howdy,
>>
>> What do you guys consider as the current "best practise" way, to 'boot
>> strap' WordPress for use in another application/framework.
>>
>> E.g., to give access to function suchs as "wp_insert_post", get_post and the
>> WPDB class, but without loading the theme?
>>
>> Thanks,
>> Robert
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap WordPress

Ryan McCue-3
In reply to this post by Otto-19
Otto wrote:
> Include the wp-load.php file.

This does come with problems if you're loading it inside a function, as
globals are set inside this without being explicitly declared as globals.

I find the following works:

        global $wp_the_query, $wp_query, $wp_rewrite, $wp;
        require_once(self::$wp_path . '/wp-load.php');

If you're running this on a mission critical site where you have to
reduce queries, I also recommend setting:

        define('DISABLE_WP_CRON', true);

This ensures that you're never caught up with running cron tasks while
trying to access anything.

--
Ryan McCue
<http://ryanmccue.info/>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap WordPress

Ryan McCue-3
Ryan McCue wrote:
> This does come with problems if you're loading it inside a function, as
> globals are set inside this without being explicitly declared as globals.

Just to expand on this a little further, here's a walkthrough of how one
piece of software we have links in with WordPress.

Each function/method that needs to use any WordPress feature calls
load_wp(). load_wp() looks like this:

        public static function load_wp() {
                static $is_loaded = false;
                if ($is_loaded) {
                        return;
                }

                if (empty(self::$wp_path)) {
                        throw new Exception('WordPress could not be loaded', 500);
                }

                global $wp_the_query, $wp_query, $wp_rewrite, $wp;

                define('DISABLE_WP_CRON', true);

                require_once(self::$wp_path . '/wp-load.php');

                $is_loaded = true;
        }



What this does is ensure that:

a) WordPress is only loaded when needed (since this software
incorporates other pieces of data from various systems)
b) WordPress is only loaded once regardless of how many times load_wp()
is called
c) Globals are set up correctly (although this does miss some. A better
solution would be an array_diff on get_defined_vars() before/after
loading, and then globalising these. An even better solution would be if
WordPress explicitly declared them as global to start with.
d) Cron is not loaded (this is for an API, so we want to avoid excess
queries and work where possible)

Hopefully this helps. :)

(If this seems badly written, or slightly insane, it's because I'm
slightly jetlagged, so apologies in advance.)

--
Ryan McCue
<http://ryanmccue.info/>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers