問題描述

如何整合 CodeIgniter 和 WordPress,以便將 WordPress 博客的外觀和模板轉移到 CodeIgniter-created 頁面?

最佳解決方案

第一步是將 CodeIgniter 和 WordPress 文件移動到自己的目錄中。

之後,將以下行放在 CodeIgniter 的 index.php 文件的頂部。根據需要將路徑更改為 wp-blog-header.php,以指向您的 WordPress 的根目錄。

<?php
    require('../wp-blog-header.php');

然後,您可以在視圖中使用以下功能:

<?php
    get_header();
    get_sidebar();
    get_footer();
?>

WordPress 的文檔中也可以找到其他幫助函數,可幫助您集成設計。

次佳解決方案

當我在 Codeigniter 的 index.php 頁面中包含文件 wp-blog-header.php 時,我遇到了一個問題,即 site_url() 是在 codeigniter 的 URL 幫助器和 WordPress 中定義的。我使用以下代碼解決了這個問題:

require('blog/wp-blog-header.php');

add_filter('site_url', 'ci_site_url', 1);

function ci_site_url() {
    include(BASEPATH.'application/config/config.php');
    return $config['base_url'];
}

header("HTTP/1.0 200 OK");

最後一行需要添加,因為 WordPress 文件正在向頭添加 HTTP 響應頭 「HTTP /1.0 404 Page not found」 。

現在可以使用 WordPress 函數調用 CodeIgntier 。

第三種解決方案

這是在您的代碼標識符項目中使用 WordPress 模板的另一種方式。這對我來説更好,所以我想分享它。用 WordPress 3.3.1 和 Codeigniter 2.1 進行測試。

目錄結構:

/ - WordPress
/ci/ - codeigniter

/ci/index.php(CI 索引文件頂部)

$wp_did_header = true;

if ( defined('E_RECOVERABLE_ERROR') )
    error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR |   E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);
else
    error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING);

require_once("../wp-config.php");

通過覆蓋默認的 codeigniter 版本來處理 site_url 功能的衝突。您將需要更改任何在 Codeigniter 中使用 site_url()的地方,以改用 ci_site_url()

/ci/application/helpers/MY_url_helper.php

<?php
function anchor($uri = '', $title = '', $attributes = '')
{
    $title = (string) $title;

    if ( ! is_array($uri))
    {
        $site_url = ( ! preg_match('!^w+://! i', $uri)) ? ci_site_url($uri) : $uri;
    }
    else
    {
        $site_url = ci_site_url($uri);
    }

    if ($title == '')
    {
        $title = $site_url;
    }

    if ($attributes != '')
    {
        $attributes = _parse_attributes($attributes);
    }

    return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
}


if ( ! function_exists('ci_site_url'))
{
    function ci_site_url($uri = '')
    {
        $CI =& get_instance();
        return $CI->config->site_url($uri);
    }
}

function current_url()
{
    $CI =& get_instance();
    return $CI->config->ci_site_url($CI->uri->uri_string());
}


function anchor_popup($uri = '', $title = '', $attributes = FALSE)
{
    $title = (string) $title;

    $site_url = ( ! preg_match('!^w+://! i', $uri)) ? ci_site_url($uri) : $uri;

    if ($title == '')
    {
        $title = $site_url;
    }

    if ($attributes === FALSE)
    {
        return "<a href='javascript:void(0);' onclick="window.open('".$site_url."', '_blank');">".$title."</a>";
    }

    if ( ! is_array($attributes))
    {
        $attributes = array();
    }

    foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
    {
        $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
        unset($attributes[$key]);
    }

    if ($attributes != '')
    {
        $attributes = _parse_attributes($attributes);
    }

    return "<a href='javascript:void(0);' onclick="window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');"$attributes>".$title."</a>";
}



function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
    if ( ! preg_match('#^https?://#i', $uri))
    {
        $uri = ci_site_url($uri);
    }

    switch($method)
    {
        case 'refresh'  : header("Refresh:0;url=".$uri);
            break;
        default         : header("Location: ".$uri, TRUE, $http_response_code);
            break;
    }
    exit;
}

您現在可以使用 WordPress get_header()和/或 get_footer()函數在 CI 項目中繪製模板。

第四種方案

我正在使用 Wordpress 來管理自定義 CI e-commerce 網站中的文章。 CI 是我的主要網站。目錄結構如下:

 /application (CI)
 /... (directories like javascript, stylesheets ...)
 /system (CI)
 /wordpress
 /.htaccess
 /index.php (CI)

我可以在我的 CI 控制器中使用 Wordpress 功能,當我將以下代碼添加到 CI 的 index.php 頂部時,我的 URL 不會被弄亂:

require_once './wordpress/wp-blog-header.php';

add_filter('site_url', 'ci_site_url', 1);

function ci_site_url($uri = '') {
    $CI =& get_instance();
    $uri = ltrim(str_replace($CI->config->base_url('wordpress/'), '', $uri),'/'); // "wordpress/" is in my case the name of the directory where I installed WordPress. See directory structure above.
    return $CI->config->site_url($uri);
}

使用 JérômeJaglale(http://jeromejaglale.com/doc/php/codeigniter_i18n) 使用 CI i18n 庫時也可以。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。