0

I use wp-load() to load the basic WP environment and then read some info from the DB without any problem.

But now i need to render the full header for a page, including the Yoast SEO plugin. After calling wp-load() and getting my data from the DB, i call wp_head() to render the header, but the output is basically empty as the normal WP environment has not been loaded. How can load the WP environment to the point that all the plugins without any problems.

I did try various aproaches

  • calling wp('p=83'); to init the environment with the data of the correct page (ID = 83)
  • initializing the $wp_query and $post vars manually with the same data they would get in a normal page call
  • trying to find more global data structs that are set in a normal call but not with wp_load

but no love.

EDIT:

So the exact situation is this:

/test.php
/wordpress/{all the wp stuff}

in my test.php I include the wp_load.php

require_once("wordpress/wp-load.php");

then i tried various versions, including the two solutions given by fischi,

  • using wp_head directly in test.php
  • using the get_header() directly in test.php
  • placing it in a template file and call that via get_header('justthehead')

But nothing works!

My problem was not that i couldn't execute the wp_head() function, that was working from the beginning, but that apparently the wp_load is not enough for some plugins to work correctly.

In my case i try to get the Yoast SEO plugin to work, but it does not show me the same output as when called in 'the normal way' inside the template for page p=83.

So i DO get the wp_head output, but it is not complete as the data from the plugin is missing.

3

You are looking for the function get_header().

wp_head() is the internal function of WordPress for doing things inside the head section (listing scripts, styles, metadata etc.).

The real HTML output comes from the function get_header(), which includes your theme's header.php.

I suppose that you do not really want just this part of the header, as it leaves a lot of tags open, so my suggestion would be to create a file in your theme folder: header-justthehead.php

You can the call this file with a simple line:

get_header( 'justthehead' );

and you are in full control of the output for your custom header, and you do not have to mess with the header of your website itself. The only thing you still have to take care of, is that the right post is loaded. Define the $args for your query_posts(), and this should work for you. Never forget the wp_reset_query() - even if you do not need it in this case.

include( 'wp-load.php' ); // loads WordPress Environment

query_posts( $args ); // load your desired Post/Page

if ( have_posts() ) : while ( have_posts() ) : the_post(); // Setup the Post/Page

    get_header( 'justthehead' ); // Get your custom header-justthehead.php, containing your wp_head() call

endwhile; endif; // quit the loop

wp_reset_query(); // clean it up
|improve this answer|||||
  • Thanks for the extensive answer, but it does not work. I need the output of the plugin, which uses the hook wp_head. So i the template file 'justthehead' i'd have exactly that call to wp_head (do_action('wp_head')). The problem is that the plugin is missing some data that is not initialized, i only see the same output with your approach than with the direkt call to wp_head. The question is how can i initialize the whole wp environment so that the plugin has all it needs! – Larzan Apr 24 '15 at 21:07
  • I added another solution. If this one does not work for you, I need you to explain your setup in detail (URLs, Template structure etc.) to make this work. – fischi Apr 25 '15 at 5:43
0

Another way to go would be to make a switch in your template file.

Let's say you have got your page.php calling the get_header() function.

<?php get_header(); ?>

    //Template Stuff

If you put the switch before this call, you can make it work like that, setting a parameter defining which header you want to include.

<?php
    if ( $_GET['justtheheader'] == 'true'] ) {
        get_header( 'justtheheader' );
    } else {
        get_header();
    }
?>

In your header-justtheheader.php you initialize the header and quit qith a die() (important, do not use the wp_die(), as this delivers additional HTML output.

You can call every page for the header with yourdomain.com/pagetoshow?justtheheader=true.

|improve this answer|||||
  • You should edit your other answer with this entry. – Christine Cooper Apr 27 '15 at 14:10
  • This is a second solution :) – fischi Apr 27 '15 at 14:11
  • It is basically the same as above and the same that i already tried, please see my edit to the question. I DO get the output from wp_head, have so from the start, but as i said some plugins do not work correctly that way (namely Yoast SEO) – Larzan Apr 28 '15 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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