45

如何将 CodeIgniter 和 WordPress 集成,以便将 WordPress 博客的外观和感觉/模板转移到 CodeIgniter 创建的页面?

4

5 回答 5

32

第一步是将 CodeIgniter 和 WordPress 文件移动到它们自己的目录中。

之后,将以下行放在 CodeIgniterindex.php文件的顶部。根据需要将路径更改wp-blog-header.php为指向 WordPress 的根目录。

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

然后,您可以在视图中使用以下函数:

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

其他帮助功能也可以在 WordPress 的文档中找到,它们可以帮助您集成设计。

于 2009-08-10T09:29:32.483 回答
16

当我在 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 Page not found”添加到标头。

现在可以使用 WordPress 函数调用 CodeIgntier。

于 2010-02-16T12:52:14.277 回答
6

这是在您的 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");

通过覆盖默认的 codeigniter 版本来处理 site_url 函数冲突。您将需要更改您site_url()在 codeigniter 中使用的任何地方来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;
}

您现在可以使用 WordPressget_header()和/或get_footer()函数在 CI 项目中绘制模板。

于 2012-02-02T10:36:41.927 回答
3

我正在使用 Wordpress 来管理自定义 CI 电子商务网站中的文章。CI 是我的主要站点。目录结构如下:

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

将以下代码添加到 CI 的index.php顶部时,我可以在 CI 控制器中使用 Wordpress 函数,而不会弄乱我的 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ôme Jaglale ( http://jeromejaglale.com/doc/php/codeigniter_i18n ) 的 CI i18n 库时也可以使用。

于 2014-01-12T21:44:45.963 回答
0

如果您打算在代码中使用 code ignitor 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,因此,如果您已经向项目添加了大量链接和内容,它可能会对您有所帮助。

于 2011-07-23T18:07:21.537 回答