Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Based on an example found here and several others, I am trying to load a non wordpress page from a specific URL while having access to the wordpress core.

I have the following code (which seems to work as expected) to hook into the template_redirect action:

function templateRedirect() {
    global $wp;
    if ( $wp->query_vars['pagename'] == 'nlm-admin-thumbnail' ) {
        global $wp_query;
        // if we have a 404 status
        if ($wp_query->is_404) {
            // set status of 404 to false
            $wp_query->is_404 = false;
            $wp_query->is_archive = true;
        }
        // change the header to 200 OK

        header("HTTP/1.1 200 OK");
        //load our template
        include( dirname( __FILE__ ) . '/thumbnail.php' );
        exit;
    }
}

add_action('template_redirect', 'templateRedirect');

My problem comes when executing the code in thumbnail.php. This file is supposed to download a thumbnail of a webpage and output the raw image. The nlm-admin-thumbnail slug is used as the src for an image tag.

When I execute the thumbnail.php I keep getting undefined function some_wp_function(). I have included http.php and class-http.php from the wp-includes folder. Now that I have included those, the next error is undefined function apply_filters().

Does loading a php file in this way not give me access to the wp core, and should apply_filers() not accessable?

share|improve this question
    
Try loading wp-load.php –  Magenta Cuda Feb 23 '13 at 2:32
    
I thought by hooking into template redirect I would avoid that and all the problems that go with it. –  Chris Strutton Feb 23 '13 at 2:36
    
Calling add_action() justs adds some objects? to some array? it doesn't actually load any PHP files that I am aware of. –  Magenta Cuda Feb 23 '13 at 2:47
    
I am not sure exactly what you are trying to to but BackPress which is just the WordPress core available as a free download may be something toconsider. –  Magenta Cuda Feb 23 '13 at 3:06

1 Answer 1

up vote 2 down vote accepted

I do this pretty regularly and have no issue with using functions in WordPress core. You can hook a bit earlier though and skip the 404 and header bits. In functions.php:

function wpa88013_parse_query( $wp ){
    if ( 'nlm-admin-thumbnail' == $wp->query_vars['pagename'] ) {
        include( dirname( __FILE__ ) . '/thumbnail.php' );
        exit;
    }
}
add_action( 'parse_query', 'wpa88013_parse_query' );

And some example code in thumbnail.php:

<?php
$content = "foo\n\nbar";
echo apply_filters( 'the_content', $content );

$post_id = 1;
echo get_the_post_thumbnail( $post_id );

I've just tested this in the twentytwelve theme and it works as expected.

share|improve this answer
    
Thanks @Milo. Will hooking in here let me set my own headers? My script returns an image built in memory (the actual image, not a url to a file). –  Chris Strutton Feb 23 '13 at 3:28
    
hook the parse_request action instead, this happens before WordPress sends any headers. see the action reference for the order of actions. –  Milo Feb 23 '13 at 3:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.