WordPress.org

Support

Support » Plugins and Hacks » Hacks » Integrating WordPress into a Yii based page

Integrating WordPress into a Yii based page

  • Ahlforn

    @ahlforn

    Im trying to integrate a WordPress blog into an existing website based on the Yii framework. I based my attempt on this article. My current website is located at the root of the domain and WP in a sub directory called “wp”.

    The WP site works. An integration test I made in a fresh php file works. But once I put the code into my Yii based code, I get the error “Error establishing a database connection”.

    Heres the code snippet I used in my Yii based code:

    <?php
    spl_autoload_unregister(array('YiiBase', 'autoload'));
    
    define('WP_USE_THEMES', false);
    require('wp/wp-blog-header.php');
    
    $posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
    foreach ($posts as $post) : start_wp(); ?>
    <?php the_date(); echo "<br />"; ?>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php
    endforeach;
    
    spl_autoload_register(array('YiiBase', 'autoload'));
    ?>

    To avoid the WP and Yii autoloader from colliding, I added the calls to spl_autoload_unregister and spl_autoload_register. I’ve tried closing the Yii framworks DB connection before integrating WP, but with no luck.

    My hope is that someone in this forum would have an idea as to how to solve this problem.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’ve used Yii a little(when i was looking into frameworks) and i really don’t think trying to load WordPress into the framework is your best approach.

    I think(personally) what would be easier(and better performance wise), would be to either query the WordPress database via Yii’s own DB method(i personally found it hard to follow – so can appreciate if you have that issue), or alternatively querying the WordPress feed URLs to pull the data you need and formatting that data as you need.

    Ahlforn

    @ahlforn

    You are right. My initial reason for including WP was to avoid the hassle of navigating the db as well as dealing with formatting of the output.

    I eventually figured out that the error was caused by program with the global keyword in php not functioning properly when I included WP within a function. So I ended up with a solution where I detect if the page requested needs WP and then include WP even before starting the Yii application. It’s not a very elegant solution but it works and the performance is decent enough for the site I’m working on.

    But thanks for your response 🙂

    Happy to, you’re welcome, glad to hear you found the solution you were looking for.. 🙂

    Hai Ahlforn!!

    I an newbie to Yii and I am need of the same integration you did .. can you guide me a bit in detail.. I need this to be done by EOD tommorow ..

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Integrating WordPress into a Yii based page’ is closed to new replies.
Skip to toolbar