个性化阅读
专注于IT技术分析

如何将WordPress模板与CodeIgniter集成

点击下载

如何将CodeIgniter和WordPress集成在一起, 以便将WordPress博客的外观/模板延续到CodeIgniter创建的页面上?


#1


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

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

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

然后, 你可以在视图内部使用以下功能:

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

其他帮助程序功能也可以在WordPress文档中找到, 可以帮助你集成设计。


#2


当我在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中调用。


#3


这是在你的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函数冲突。你将需要更改在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项目中绘制模板。


#4


我正在使用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ômeJaglale(http://jeromejaglale.com/doc/php/codeigniter_i18n)的CI i18n库时也可以使用。


#5


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

赞(0)
未经允许不得转载:srcmini » 如何将WordPress模板与CodeIgniter集成

评论 抢沙发

评论前必须登录!