Title: Calling wordpress from within a function
Last modified: August 18, 2016

---

# Calling wordpress from within a function

 *  [knipknap](https://wordpress.org/support/users/knipknap/)
 * (@knipknap)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/calling-wordpress-from-within-a-function/)
 * Hello,
 * I am trying to embed a blog into an existing webpage. I tried this in the following
   way:
 * The existing webpage is implemented using Smarty, a template engine. Since I 
   am trying to give template authors the possibility to embed a blog into whatever
   template they choose, the only way is to register a function to Smarty, which
   is then made accessible to the template.
 * The bottom line is: A function needs to be passed to Smarty that returns the 
   complete blog.
    However, doing something like this:
 * ———-
    define(‘WP_USE_THEMES’, true);
 * function show_blog() {
    require(‘wordpress/wp-blog-header.php’); }
 * $s = &new Smarty();
    $s->register_function(“blog”, “show_blog”); ———-
 * produces the following error message:
 * —————–
    Fatal error: Call to a member function on a non-object in /home/www/wordpress/
   wp-includes/functions.php —————–
 * When calling require() in global scope it works, but this will obviously not 
   integrate with the template engine. Is there an easy way to achive something 
   similar?
 * (note for the Smarty-experienced folks:
    “{php}require(‘wordpress/wp-blog-header.
   php’);{/php}” produces the same problem; the snipped is called from within a 
   function)
 * Thanks,
    -Samuel

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

 *  Thread Starter [knipknap](https://wordpress.org/support/users/knipknap/)
 * (@knipknap)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/calling-wordpress-from-within-a-function/#post-256176)
 * Alright, I worked out a solution:
 * `
    function _show_blog() { global $cache_userdata, $wpdb, $wp_query, $wp_rewrite,
   $post, $category_cache, $request, $posts_per_page, $wpdb, $max_num_pages; define('
   WP_USE_THEMES', true); require('wordpress/wp-blog-header.php'); }
 * Works for me with WP1.5. Now, someone please sack the WP developers for horrible
   abuse of global variables.
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/calling-wordpress-from-within-a-function/#post-256177)
 * “_Now, someone please sack the WP developers for horrible abuse of global variables._“
 * Can’t, because we didn’t really hire them. They just wandered in one day and 
   started coding. For free.
 * I’m glad you figured out your problem, because I was stumped by it.
 *  [tomhanna](https://wordpress.org/support/users/tomhanna/)
 * (@tomhanna)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/calling-wordpress-from-within-a-function/#post-256180)
 * A multi author blogging solution using the Smarty template system…my friend, 
   you are reinventing the wheel.
 * [http://mu.wordpress.org/](http://mu.wordpress.org/)
 *  [lb9](https://wordpress.org/support/users/lb9/)
 * (@lb9)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/calling-wordpress-from-within-a-function/#post-256361)
 * I saw the wordpress mu link and that would definitely do the trick, but I already
   started with the regular version of wordpress. Do you know if I can safely upgrade
   from wordpress 1.5 to wordpressmu and keep my posts and all of the coding I worked
   so hard to create in tact? I dont want to have to do all of that work again and
   if I could just use a simple echo function that would work (ie, allow me to include
   my posts on a separate webpage that was already created and resides outside the
   blog folder and main page), that would be great. I was thinking something more
   along the lines of this code I saw in another post:
 * <?php define(‘WP_USE_THEMES’, false);
    require(‘./wp-blog-header.php’); get_header();?
   > <div id=”content” class=”narrowcolumn”><h1 style=”color:#000;”>Welcome to my
   site</h1> Hello and welcome to my site. You can go to my web page about how to
   improve your shoots with your digital camera; or you can go to my blog <h2>Lastest
   posts in my blog:</h2><?php # Here starts Mini Loop trick: You can use this code,
   but _at your own risk_ # If you want to improve this code, you’re welcome 😉 
   $how_many=7; //How many posts do you want to show ?> <ul class=”whats-new”> <?
   $news=$wpdb->get_results(“SELECT ID,post_title FROM $wpdb->posts WHERE post_status
   = \”publish\” ORDER BY ID DESC LIMIT “.$how_many); foreach($news as $np){ printf(”
    - 
    - “, $np->ID,$np>post_title);
      }?></div><?php get_sidebar(); get_footer(); ?>
    - I tried it but also produces ‘Fatal error: Call to a member function on a 
      non-object’ output error. I think there may be something wrong in the syntax
      but seems like it should work otherwise.

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

The topic ‘Calling wordpress from within a function’ is closed to new replies.

## Tags

 * [function](https://wordpress.org/support/topic-tag/function/)
 * [global](https://wordpress.org/support/topic-tag/global/)
 * [require()](https://wordpress.org/support/topic-tag/require/)
 * [smarty](https://wordpress.org/support/topic-tag/smarty/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 4 participants
 * Last reply from: [lb9](https://wordpress.org/support/users/lb9/)
 * Last activity: [20 years, 3 months ago](https://wordpress.org/support/topic/calling-wordpress-from-within-a-function/#post-256361)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
