2009-08-10 85 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)。