WordPress.org

Support

Support » Themes and Templates » [Resolved] Integrating WP in external PHP pages

[Resolved] Integrating WP in external PHP pages

  • I have several external pages that use the WP theming engine (see instructions at http://codex.wordpress.org/Integrating_WordPress_with_Your_Website) … the problem is when I upgraded to WP3.0, these pages now don’t work as expected.

    Instead of getting the WordPress header, sidebar and footer with my content in the middle, I’m getting what appears to be either a “Page not found” or the homepage (depending on the URL).

    Here is a test page that produces the 404:

    <?php
    	require('../../wp-blog-header.php');
    	get_header();
    ?>
    	<h1>This is a test</h1>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    This page worked fine under 2.9.2. Any ideas?

    Thanks &
    Cheers,

    Toby

Viewing 15 replies - 1 through 15 (of 21 total)
  • This worked fine for me.

    Note that this file was put in the same folder as the wp-admin folder

    <?php
    require('./wp-blog-header.php');
    	get_header();
    	$user_id = 1;
    $user = get_userdata($user_id);
    if ($user) {
     echo '<p>' . $user->first_name . ' ' . $user->last_name . '</p>';
    }
    ?>
    	<h1>This is a test</h1>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Wouldn’t your require statement fail since there is no “wp-blog-header.php” in the /wp-admin folder?

    I tried your test, changing the require() line to read thusly:

    require('../wp-blog-header.php');

    Which worked. But when I copied the file to the root of my site and changed the require back to:

    require('./wp-blog-header.php');

    I get a 404. Is this a permalinks thing going on?

    Wouldn’t your require statement fail since there is no “wp-blog-header.php” in the /wp-admin folder?

    I didn’t say in the wp-admin folder, I said ‘n the same folder as the wp-admin folder” meaning in my case the folder that holds wp-admin, wp-content, wp-includes.

    Okay, I’ve answered my own question (but still don’t have a fix). When I change my permalinks to the default (?p=###) the external pages work fine. But when I use the custom permalinks I normally use (which works great under WP2.9.2)

    /%year%/%monthnum%/%day%/%postname%/

    I get the 404 behavior on the external pages. I am running under IIS 5.0 (Windows 2000) using the ISAPI rewrite IIRF. I guess the permalink handling got changed significantly in 3.0?

    Sorry, I misunderstood — thanks for clearing it up. But it is still not working for me, still getting a 404. I even changed the code to the following (which I would expect to simply output the “This is a test”)

    <?php
    require('./wp-blog-header.php');
    //get_header();
    //$user_id = 1;
    //$user = get_userdata($user_id);
    //if ($user) {
    // echo '<p>' . $user->first_name . ' ' . $user->last_name . '</p>';
    //}
    ?>
    	<h1>This is a test</h1>
    
    <?php //get_sidebar(); ?>
    
    <?php //get_footer(); ?>

    Could be the H1.

    This works fine in 3.0 with the WordPress Twenty Ten theme the active theme.

    <?php
    require('./wp-blog-header.php');
    	get_header();
    ?>
    
    <h2>This is a test</h2>;
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Hmmm. This still doesn’t work for me (IIS 5.0/Windows 2000 Server using IIRF Rewrite engine) — the above code results in a 404. This worked fine in 2.9.2.

    I’m still digging into it but would love to hear any advice.

    Thanks &
    Cheers,

    Toby

    I have another host (running Ubuntu 8.04 LTS and Apache 2.2.8) and it seems to do the same thing, it displays the content wrapped in the 404 template (and the headers sent are 404 as well.)

    Can you verify that yours doesn’t send 404 headers?

    Not to spam this forum, but I’m slowing working through the issue and wanted to document what I’ve found so far (at least in my case on two different installs/platforms):

    • In WP 2.9.2, an external page like MichaelH’s above works exactly as expected
    • In WP 3.0, and external page renders but sends 404 headers and the title of the page is “Site » Page not found”

    In wp-includes/classes.php, in the function handle_404() one of the tests performed to determine whether or not to send a status 404 or 200 is to count the number of matching posts — in 2.9.2, the count of posts [count($wp_query->posts)] is equal to the total posts shown on a blog page (in my case, 10); in 3.0 the total is 0, hence the reason for the 404 headers and bogus title of the page.

    There is something different about the query run to find posts on these external pages, which I haven’t found yet.

    Thanks &
    Cheers,

    Toby

    I was wrong in my previous post (apples/oranges); … comparing 2.9.2 and 3.0 on the same install on the same platform, the number of posts returned is the same (0) in external pages; however, the 2.9.2 version of wp-includes/classes.php has a test in handle_404() — line 462:

    if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) )

    On external pages, $this->did_permalink evaluates to false, as does !empty($_SERVER[‘QUERY_STRING’]), so it falls through to send a status 200.

    In 3.0, the first test is changed to (on line 477)

    if ( ( 0 == count( $wp_query->posts ) ) && !is_404() && !is_robots() && !is_search() && !is_home() )

    On external pages, all of these evaluate to true, so it drops into the section to send a status 404.

    Not real sure what the fix might be so external pages send the proper headers. Any advice?

    Thanks &
    Cheers,

    Toby

    camper2020
    Member

    @camper2020

    Hi Toby.

    I have / had a similar problem. I am using custom pages that load via AJAX in a site I’m building. These only reload part of the page at a time, independently. This all worked fine in WP 2.9.2 but since upgrading to WP 3.0. my AJAX stopped working. After debugging, I realised the headers of the files were being sent as 404s.

    I’ve been looking for a fix and up until now, nothing. My only workaround was to tell my page to load the AJAX regardless of the 404 or 200 response. While this works, its not ideal, and technically, doesnt solve your problem.

    After seeing your later posts, and noticing that its definitely a change in the wp-includes/classes.php file, I’ve coded a mini (hopefully temporary) hack that seems to work, without modifying core WP files and it should work in all scenarios.

    By forcing the headers to send through a “200 ok” response, the pages load as they should. If there is a more elegant way to solve this, someone please tell me. In the meantime, add this code after including wp-blog-header.php:

    status_header(200);
    nocache_headers();

    Hope that helps,
    Regards.

    The way I overcame it was (hacking core) editing wp-includes/classes.php and changing line 477 (the first test in handle_404()) to the following:

    if ( ( 0 == count( $wp_query->posts ) ) && !is_404() && !is_robots() && !is_search() && !is_home() && (!defined('WP_EXTERNAL') || WP_EXTERNAL===false) ) {

    Then in my external page, I define WP_EXTERNAL at the top like this:

    define('WP_EXTERNAL', true);

    Hacking core stinks but this was the best solution for my purposes. I sent a message to [wp-hackers] but I haven’t seen it hit the list yet. Is that list moderated or something?

    Oooo, ooo, ooooooo! I think I have found the fix!!!! Since merging WP and WPMU, it appears the best way to use the WP theme engine in external pages is to load wp-load.php instead of wp-blog-header.php

    So instead of using:

    require('./wp-blog-header.php');

    You would use:

    require('./wp-load.php');

    I found the answer on the BBPress forum.

    Try that and see if it fixes it. If you reply here, I’ll mark the topic resolved.

    Cheers,

    Toby

    camper2020
    Member

    @camper2020

    Nice work Toby, I tested with the AJAX scenario as well, works like a dream. Just one thing I do differently because when I load pages, sometimes they’re just included with the template eg. category.php and sometimes they’re independent i.e. when the pages are loaded via AJAX. The relative path to wp-load.php changes depending on these two ways of loading so I have test for that. 🙂

    if (file_exists("./wp-load.php"))
    {
    	require_once("./wp-load.php");
    }
    else
    {
    	require_once("../../../wp-load.php");
    }

    or if case the above is overkill… 🙂

    if (file_exists("../../../wp-load.php"))
    {
    	require_once("../../../wp-load.php");
    }

    Could be pointless to mention all that, but you never know. Thanks again for all the hard work put into the research for this problem.

    Regards.

    anarchitecton
    Member

    @anarchitecton

    thanks Toby!

    After a lot of search I finally found your post. I was also using

    require('./wp-blog-header.php');

    in an external php page for ajax processing and your ‘fix’ worked like a charm!

    Thanks again man!

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘[Resolved] Integrating WP in external PHP pages’ is closed to new replies.