php 如何将 WordPress 模板与 CodeIgniter 集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1253906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to integrate WordPress template with CodeIgniter
提问by Click Upvote
How can CodeIgniter and WordPress be integrated such that the look and feel/template of the WordPress blog is carried over to the CodeIgniter-created pages?
如何将 CodeIgniter 和 WordPress 集成,以便将 WordPress 博客的外观/模板转移到 CodeIgniter 创建的页面?
采纳答案by Click Upvote
First step is to move CodeIgniter and the WordPress files in their own directory.
第一步是将 CodeIgniter 和 WordPress 文件移动到它们自己的目录中。
After that, put the following line at the top of your CodeIgniter's index.phpfile. Change the path to wp-blog-header.phpas needed to point to your WordPress's root directory.
之后,将以下行放在 CodeIgniterindex.php文件的顶部。wp-blog-header.php根据需要将路径更改为指向 WordPress 的根目录。
<?php
require('../wp-blog-header.php');
Then, you can use the following functions inside your views:
然后,您可以在视图中使用以下函数:
<?php
get_header();
get_sidebar();
get_footer();
?>
Other helper functions can also be found in WordPress's documentation which can assist you in integrating the design.
其他帮助函数也可以在 WordPress 的文档中找到,它们可以帮助您集成设计。
回答by sumanchalki
When I included the file wp-blog-header.php in Codeigniter's index.php page, I got a problem that site_url() is defined in both codeigniter's URL helper and WordPress. I solved this using the following code:
当我在 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");
Last line needs to be added as WordPress file was adding a HTTP response header 'HTTP/1.0 404 Page not found' to the header.
需要添加最后一行,因为 WordPress 文件将 HTTP 响应标头“HTTP/1.0 404 Page not found”添加到标头。
Now its fine to use WordPress functions to call in CodeIgntier.
现在可以使用 WordPress 函数调用 CodeIgntier。
回答by sschueller
Here is another way to use WordPress templates in your codeigniter project. This works better for me so I wanted to share it. Tested with WordPress 3.3.1 and Codeigniter 2.1.
这是在 codeigniter 项目中使用 WordPress 模板的另一种方法。这对我来说效果更好,所以我想分享它。使用 WordPress 3.3.1 和 Codeigniter 2.1 进行测试。
Directory Structure:
目录结构:
/ - WordPress
/ci/ - codeigniter
/ci/index.php (Top of CI Index file)
/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");
Deal with the site_url function collision by overriding the default codeigniter version. You will need to change any place you used site_url()in codeigniter to use ci_site_url()instead.
通过覆盖默认的 codeigniter 版本来处理 site_url 函数冲突。您需要更改您site_url()在 codeigniter 中使用的任何位置以使用ci_site_url()。
/ci/application/helpers/MY_url_helper.php
/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;
}
You can now use the WordPress get_header()and/or get_footer()functions to draw the template in your CI project.
您现在可以使用 WordPressget_header()和/或get_footer()函数在 CI 项目中绘制模板。
回答by Chris Aelbrecht
I'm using Wordpress for managing articles in a custom CI e-commerce website. CI is my main site. The directory structure is the following:
我正在使用 Wordpress 管理自定义 CI 电子商务网站中的文章。CI 是我的主要站点。目录结构如下:
/application (CI)
/... (directories like javascript, stylesheets ...)
/system (CI)
/wordpress
/.htaccess
/index.php (CI)
I'm able to use Wordpress functions in my CI controllers without my URLs being messed up when adding the following code to the top of CI's index.php:
将以下代码添加到 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);
}
Works also when using CI i18n library by Jér?me Jaglale (http://jeromejaglale.com/doc/php/codeigniter_i18n).
在使用 Jér?me Jaglale ( http://jeromejaglale.com/doc/php/codeigniter_i18n) 的CI i18n 库时也能工作。
回答by Eric
if you're planning on using the code ignitor site_url function in your code, or if you're doing a merge of an existing CI site and WP... this might be helpful:
如果您打算在您的代码中使用 code ignitor site_url 函数,或者您正在合并现有的 CI 站点和 WP...这可能会有所帮助:
at the top of CI index.php:
在 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);
}
effectively this allows you to use site_url in CI, so if you've already added a ton of links and content to your project it might help you out.
这实际上允许您在 CI 中使用 site_url,因此如果您已经向项目添加了大量链接和内容,它可能会对您有所帮助。

