Title: Calling wp-blog-header.php from inside a PHP function
Last modified: August 19, 2016

---

# Calling wp-blog-header.php from inside a PHP function

 *  [markusku](https://wordpress.org/support/users/markusku/)
 * (@markusku)
 * [17 years ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/)
 * Heyas, I’m trying to get lastest WP blog posts into my site’s front page using
   wp-blog-header.php.
 * The problem is that I get this error if I have the code inside a function:
    `
   Fatal error: Call to undefined method stdClass::set_prefix() in /wordpress/wp-
   settings.php on line 268`
 * If I put the same code straight into some empty file, it works, returning the
   news. If I put the same code inside a function, it fails. So, basically it only
   works if it’s in the “root” of the php file. The main problem of course is that
   the site I’m working with is made so that everything is basically being run inside
   a function call.
 * The full PHP code:
 *     ```
       loadItAll();
   
       function loadItAll() {
       	require_once('wp-blog-header.php');
       	$myposts = get_posts('numberposts=5&offset=1&category=1');
       	foreach($myposts as $post) :
       	?>
       		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
       	<?php endforeach;
       }
       ```
   

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [philnelson](https://wordpress.org/support/users/philnelson/)
 * (@philnelson)
 * [16 years, 12 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128637)
 * Currently having this same problem. Ever find a solution?
 *  [peter_b](https://wordpress.org/support/users/peter_b/)
 * (@peter_b)
 * [16 years, 12 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128642)
 * I’m having the same problem and ended up querying the database directly to extract
   posts. A fix would be appreciated – it seems very brittle integration, given 
   the number of results when searching the web for this error message!
 *  [philnelson](https://wordpress.org/support/users/philnelson/)
 * (@philnelson)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128660)
 * Yeah, very weird that nobody seems to care too much.
 *  [philnelson](https://wordpress.org/support/users/philnelson/)
 * (@philnelson)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128663)
 * This is a really frustrating problem to not have an answer to.
 *  [der](https://wordpress.org/support/users/der/)
 * (@der)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128665)
 * I was having the same problem you guys are having. After pulling my hair off 
   for a few hours, I got to solve my problem, here it goes…
 * I was sending some information via AJAX to the functions.php file, under my TEMPLATEPATH
   directory. On the top of the functions.php file, I was including a function that
   checked if there was something set in the $_POST variable, then executed a specific
   action.
 * And guess what?, the error was the same you guys are having, so I started to 
   get deep into the problem.
 * For some reason, if you use any php file used by wordpress internally, as the
   POST action for your AJAX request or form, it doesn’t load the WordPress environment
   correctly, so I decided to create a separate php file, unrelated to the theme
   itself, called it **postdata.php** and added the following code:
 *     ```
       // Available actions
       $actions = Array('request_gallery_slot');
   
       if (count($_POST)) {
       		if ( in_array($_POST['action'], $actions) ) {
       			// Initialize WordPress
       			$wp_root = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
       			$wp_root = $wp_root[0];
       			chdir($wp_root);
       			if (!function_exists('add_action')) {
       				if (file_exists('wp-load.php')) {
       					require_once('wp-load.php');
       				} else {
       					require_once('wp-config.php');
       				}
       			}
   
       			// Determine the action
       			switch ( $_POST['action'] ) {
       				case 'request_gallery_slot':
       					$slot = $_POST['slot'];
       					get_gallery_slot($slot);
       			}
       		}
       	}
       ```
   
 * That is working perfectly, hope this helps.
 *  [Harmeet Gabha](https://wordpress.org/support/users/scorpion380/)
 * (@scorpion380)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128708)
 * I am having the same trouble. I can call the functions from external file and
   that works. The problem I have is I run three blogs and want to get latest posts
   from each blog and display them on a landing page.
 * [Check out this post.](http://wordpress.org/support/topic/357977?replies=8)
 *  [erikperik](https://wordpress.org/support/users/erikperik/)
 * (@erikperik)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128709)
 * This problem occured for me when I included wp-blog-header.php from within a 
   function (class method actually). This caused the global variables that WP sets
   to not be global for real, thus not being accessible by calling `global $var`.
   You get the error because `$wpdb` is global, but not really. I solved this by
   adding:
 * `global $wpdb`
 * Beneath the line: (in wp-settings.php on the line 269)
 * `require_wp_db();`
 * So the result looks like this:
 *     ```
       require (ABSPATH . WPINC . '/compat.php');
       require (ABSPATH . WPINC . '/functions.php');
       require (ABSPATH . WPINC . '/classes.php');
   
       require_wp_db();
       global $wpdb; // Look look! Over here! line 270 in wp-settings.php
   
       if ( !empty($wpdb->error) )
       	dead_db();
       ```
   
 * This however **did not solve the problem**. The same _kind of_ error popped up
   elsewhere because of dependencies of other global variables in other places.
 * I just added the include(‘blog/wp-blog-header.php’); in my index.php outside 
   of any functions (this is not the best solution for me because it means I have
   to include WP in all my page requests even when WP is not needed.)

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Calling wp-blog-header.php from inside a PHP function’ is closed to new
replies.

## Tags

 * [external](https://wordpress.org/support/topic-tag/external/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [set_prefix](https://wordpress.org/support/topic-tag/set_prefix/)
 * [variable-scope](https://wordpress.org/support/topic-tag/variable-scope/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 6 participants
 * Last reply from: [erikperik](https://wordpress.org/support/users/erikperik/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function/#post-1128709)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
