LoginSignup
16

More than 5 years have passed since last update.

FuelPHPからWordpressの関数を利用する

Last updated at Posted at 2015-03-12

CodeIgniterからWordpressを呼び出す際((wordpressのthemeとかを使わずに呼び出す)
に苦労したから、FuelPHPからはどうだろうと思いやってみた。とりあえずということでまずは投稿された記事などを呼び出せるか試してみました。

まずは、フォルダの構成。今回あまり深く考えず、ドキュメントルートにwordpressを置いてみた。

ドキュメントルートのはwpplus(適当)
wpplus (root)
 |---docs
 |----fuel
 |---public
 |---wordpress

この状態でwordpressのインストールを実行。

次に、 /fuel/app/bootstrap.php の編集を行う。
ポイントは「 require '../wordpress/wp-load.php'; 」の部分。
「require COREPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php'; 」の部分より、
前に呼び出す必要がある。

bootstrap.php

require '../wordpress/wp-load.php';

// Load in the Autoloader
require COREPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php';
class_alias('Fuel\\Core\\Autoloader', 'Autoloader');

// Bootstrap the framework DO NOT edit this
require COREPATH.'bootstrap.php';
Autoloader::add_classes(array(
// Add classes you want to override here
// Example: 'View' => APPPATH.'classes/view.php',
));

// Register the autoloader
Autoloader::register();

/**
* Your environment. Can be set to any of the following:
*
* Fuel::DEVELOPMENT
* Fuel::TEST
* Fuel::STAGING
* Fuel::PRODUCTION
*/
Fuel::$env = (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : Fuel::DEVELOPMENT);

// Initialize the framework with the config file.
Fuel::init('config.php');

次に、Controllerの作成。
ポイントは「 require_once '../wordpress/wp-blog-header.php'; 」の部分。
後はWordpressの関数を呼び出すことで記事の一覧を取得出来る。

test.php
<?php

class Controller_Test extends Controller
{
    public function action_index()
    {
        require_once '../wordpress/wp-blog-header.php';

        // クエリ
        $the_query = new WP_Query('post_type=any');

        // ループ
        while ( $the_query->have_posts() ) : $the_query->the_post();
            echo '<li>';
            //何かする
            the_permalink();
            echo '</li>';
        endwhile;

        // 投稿データをリセット
        wp_reset_postdata();

    }
}

というわけで、codeigniterに比べると非常に簡単でした。
(ちなみにcodeigniterでは解決できず、頓挫しましたorz)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16