WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Integrating WP in external PHP pages (22 posts)

  1. tsimmons
    Member
    Posted 4 years ago #

    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

  2. MichaelH
    Member
    Posted 4 years ago #

    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(); ?>
  3. tsimmons
    Member
    Posted 4 years ago #

    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?

  4. MichaelH
    Member
    Posted 4 years ago #

    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.

  5. tsimmons
    Member
    Posted 4 years ago #

    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?

  6. tsimmons
    Member
    Posted 4 years ago #

    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(); ?>
  7. MichaelH
    Member
    Posted 4 years ago #

    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(); ?>
  8. tsimmons
    Member
    Posted 4 years ago #

    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

  9. tsimmons
    Member
    Posted 4 years ago #

    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?

  10. tsimmons
    Member
    Posted 4 years ago #

    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

  11. tsimmons
    Member
    Posted 4 years ago #

    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

  12. camper2020
    Member
    Posted 4 years ago #

    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.

  13. tsimmons
    Member
    Posted 4 years ago #

    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?

  14. tsimmons
    Member
    Posted 4 years ago #

    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

  15. camper2020
    Member
    Posted 4 years ago #

    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.

  16. anarchitecton
    Member
    Posted 4 years ago #

    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!

  17. avahaf
    Member
    Posted 3 years ago #

    No I am not getting any result will any body help?????????

  18. unknownclubber
    Member
    Posted 3 years ago #

    Thanks for the answer tsimmons ;)

    I use WP 3.01 and tried to load (in ajax, with jQuery) content from an external page ... I used require('./wp-blog-header.php'); but i always got 404, even with the correct path !

    I just replaced with require_once("./wp-load.php"); and it works like a charm now !

    Thanks again

  19. cewyattjr
    Member
    Posted 3 years ago #

    This works quite well! Thanks for this tip. Anyone know how to forge a title for any page outside of WP that you include? Any sort of VAR that you could pass to get_header() or get_title() that would pass as a <title>My Blah Blah</title> page?

    Thanks,

    Chuck

  20. mountainporcupine
    Member
    Posted 3 years ago #

    Yes, that solution does work great - but I second the question of cewyattjr - does anyone know how to pass a title variable into the external pages?

  21. stevenmunro
    Member
    Posted 3 years ago #

    @mountainporcupine


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

    function assignPageTitle(){
    return "My Page Title Here";
    }
    add_filter('wp_title', 'assignPageTitle');
    ?>

  22. theBrettman
    Member
    Posted 3 years ago #

    This doesn't make sense? Why would I want the header, sidebar, or footer in my form action php file? All I want to do is use ajaxform() to call my action php file and maybe return something to a div somewhere on the page. I get 404 of course but I don't want the header being returned or anything else. I just want to email the form data. none of the wordpress form plugins will make tabular forms the way I want and I can't add what content I want where I want so I'm not using a plugin. just jquery ajaxform() and a simple php file.

    I tried require_once("./wp-load.php"); and it didn't work. My form looks like: <form id="form0" action="bbaction.php" method="post">
    bbaction.php looks like:

    <?php
    require_once("./wp-load.php");
    foreach($_POST as $name => $value) {
    	print "$name : $value<br>";
    }
    ?>

    (for now I'm just trying to print the results to a div on the page so I can style what the email will look like)
    and then my js looks like this:

    $(document).ready(function() {
        var options = {
            target: '#test'
        $('#form0').ajaxForm(options);
    });

    Anyone know what I'm doing wrong?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.