Forums

Calling wp-blog-header.php from inside a PHP function (6 posts)

  1. markusku
    Member
    Posted 4 months ago #

    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;
    }
  2. philnelson
    Member
    Posted 3 months ago #

    Currently having this same problem. Ever find a solution?

  3. Peter_B
    Member
    Posted 3 months ago #

    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!

  4. philnelson
    Member
    Posted 2 months ago #

    Yeah, very weird that nobody seems to care too much.

  5. philnelson
    Member
    Posted 2 months ago #

    This is a really frustrating problem to not have an answer to.

  6. der
    Member
    Posted 2 months ago #

    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.

Reply

You must log in to post.

About this Topic