2009-08-10 61 views

回答

30

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

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

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

然後,您可以使用以下功能的意見裏面:

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

其他的輔助功能也可以WordPress的文檔,可以 幫助您在設計中集成了中發現的。

+0

它的工作原理absoultely完美感謝的人 – 2012-07-31 04:57:49

14

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

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未找到網頁」到報頭被添加。

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

0

如果你打算在你的代碼中使用的代碼點火器SITE_URL功能,或者如果你正在做一個現有的CI網站和WP的合併......這可能是有益的:

頂部CI的index.php:

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

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

function ci_site_url($url, $path, $orig_scheme, $blog_id) { 
    $CI =& get_instance(); 
    $new_path = str_replace("YOURSITEURLGOESHERE", "", $url); 
    return $CI->config->site_url($new_path); 
} 

有效,這允許您在使用CI SITE_URL,因此,如果您已經添加一噸的鏈接和內容到您的項目它可能會幫助你。

4

這是另一種在codeigniter項目中使用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"); 

處理通過重寫默認笨版本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項目。

2

我使用WordPress的在一個自定義的CI的電子商務網站管理的文章。 CI是我的主要網站。目錄結構如下:

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

添加以下代碼CI的的index.php的頂部時,我能夠使用WordPress的功能在我的CI控制器,而我的網址被搞砸了:

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使用CI i18n庫時也有效(http://jeromejaglale.com/doc/php/codeigniter_i18n)。