CodeIgniter + Wordpress 集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17829990/
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
CodeIgniter + Wordpress integration
提问by Marcandria
My website is designed with Wordpress. Some theme pages have a custom made PHP script for booking things, which I want to refactor in CodeIgniter, to get more flexibility. Questions :
我的网站是用 Wordpress 设计的。一些主题页面有一个定制的 PHP 脚本来预订东西,我想在 CodeIgniter 中重构它,以获得更大的灵活性。问题 :
1) how to use CI functions in WP with CI's routing system ? Do I have to make a CI page index.php/controller/page1/ and then call it in Wordpress ?
1) 如何在 WP 中使用 CI 功能与 CI 的路由系统?我是否必须制作一个 CI 页面 index.php/controller/page1/ 然后在 Wordpress 中调用它?
2) Do I have to use CodeIgniter "views" system or Wordpress "theme pages" to get my result ?
2) 我是否必须使用 CodeIgniter“视图”系统或 Wordpress“主题页面”来获得我的结果?
Thanks
谢谢
回答by Marcandria
I used the following.
我使用了以下内容。
My goal is to use CodeIgniter for some pages, and leave the other pages in Wordpress untouched. I only have a few steps to do it :
我的目标是在某些页面上使用 CodeIgniter,而让 Wordpress 中的其他页面保持不变。我只有几个步骤来做到这一点:
Copy CI folder at the Wordpress root
复制 WordPress 根目录下的 CI 文件夹
Modify the ? index.php ? of CI to add an include which will add WP functions to CodeIgniter :
修改 ? 索引.php ? 的 CI 添加一个包含将 WP 功能添加到 CodeIgniter :
@require '../wp-load.php';
require_once BASEPATH.'core/CodeIgniter.php';
Once this line added, Wordpress functions will be usable in CodeIgniter ; they will be used mainly for views.
添加此行后,Wordpress 函数将可以在 CodeIgniter 中使用;它们将主要用于视图。
Modify the .htaccess of WP to not rewrite CI's URLs : After this line :
修改 WP 的 .htaccess 以不重写 CI 的 URL:在此行之后:
RewriteRule ^index\.php$ - [L]
Add this line :
添加这一行:
RewriteCond %{REQUEST_URI} !^/(codeigniter_folder|codeigniter_folder/.*)$
Then CI views can use WP functions.
然后 CI 视图可以使用 WP 功能。
回答by PAdrian
If you have an CI application that uses login session you have to modify the "load.php" file (from "wp-includes" WP folder). To do that follow the PhilB's answer from this link. For a complete explanation follow this post.
如果您有一个使用登录会话的 CI 应用程序,您必须修改“load.php”文件(来自“wp-includes”WP 文件夹)。要做到这一点,请按照此链接中的 PhilB 回答进行操作。有关完整说明,请参阅此帖子。
回答by James Melo
WordPress and CodeIgniter both are the most popular framework in PHP. CodeIgniter is a lightweight PHP framework that can help in developing applications that can be integrated with WordPress.While some users prefer developing the CodeIgniter and WordPress elements separately, we will go through an example where the primary navigation and footer are similar on both platforms, because it doesn't make much sense to update the navigation and footer twice.
Here we learn about two things using WordPress and CodeIgniter
- How to integrate already running WordPress website to integrate CodeIgniter
How to integrate already running Codeigniter website to integrate WordPress
Integrate CodeIgniter into WordPress It comes as a meager shock that numerous site proprietors overseers still lean toward WordPress as their essential Content Management System (CMS), and you can't point the finger at them considering the far-reaching highlights that WordPress brings to the table.
Okay, let's start to integrate CodeIgniter into WordPress here, WordPress is our primary application and we will use its functionality within CodeIgniter while keeping the CodeIgniter app in a sub-directory.
Step 1: Adding the WordPress Bootstrap File WordPress's wp-load.php file immediately preceding the call to Codeigniter bootstrap file in CI's index.php file
Before adding any WordPress functionality to CI' file.we will testing to make sure everything works fine.The first page of our app looked fine, but that was about it.
Every URL within our app, including form handlers, were redirected to the root directory (i.e. WordPress' directory), resulting in 404 errors. We discovered that both WordPress and Codeigniter have a “site_url” function in global scope. By loading the WordPress bootstrap file before CodeIgniter, we were getting WordPress site_url.Codeigniter function “failed” silently because it was checking to see if a site_url function existed.
Step 2: Extending Codeigniter URL Helper Our solution was to ‘extend' the URL helper of CodeIgniter. By using ci_site_url, we can resolve the site_url issue. The My_url_helper.php (helper) file should be in the application directory of CodeIgniter.
Step 3: Change site_url to ci_site_url in the CodeIgniter App. Replace all site_url file references with ci_site_url into your applications, controllers, models, and directories.
Once we completed these steps, our CI app worked …
Step 4: Stop WordPress from managing our cookies! Our site that still was not working was the admin panel that we built in Codeigniter.our credentials have been accepted but, still redirecting to login page.
I think you know about CodeIgniter will create cookies for every active database session when you log in. This can lead to WordPress bootstrap file rectifying the issue. This happens when WordPress loops via the $_COOKIE global and implements add_magic_quotes functionality to all elements, except the CodeIgniter cookie.
- Integrate WordPress into CodeIgniter IF you have already created website in CodeIgniter and you have added blog page then WordPress is the best option so, First of all, install WordPress in a folder called the blog on your website. It would be located in the top level directory in your CodeIgniter website. So it would be alongside the system folder of the CI installation. If you already have a DB for your site, have WordPress add tables to that same DB with a prefix to make things simple.
After, that add the following line in the at the top of your CodeIgniter's index.php file.Change the path to wp-blog-header.php as needed to point to your WordPress root directory.
All done!! This code allows to WordPress blog without leaving CodeIgniter site. More helper function explains in details in WordPress documentation.
WordPress 和 CodeIgniter 都是 PHP 中最流行的框架。CodeIgniter 是一个轻量级的 PHP 框架,可以帮助开发可以与 WordPress 集成的应用程序。虽然有些用户更喜欢分别开发 CodeIgniter 和 WordPress 元素,但我们将通过一个示例来说明两个平台上的主导航和页脚相似,因为两次更新导航和页脚没有多大意义。
在这里我们学习使用 WordPress 和 CodeIgniter 的两件事
- 如何集成已经运行的WordPress网站来集成CodeIgniter
如何集成已经运行的 Codeigniter 网站来集成 WordPress
将 CodeIgniter 集成到 WordPress 令人震惊的是,许多网站所有者的监督者仍然倾向于将 WordPress 作为他们必不可少的内容管理系统 (CMS),考虑到 WordPress 带来的深远影响,您不能指责他们桌子。
好的,让我们在这里开始将 CodeIgniter 集成到 WordPress 中,WordPress 是我们的主要应用程序,我们将在 CodeIgniter 中使用它的功能,同时将 CodeIgniter 应用程序保留在一个子目录中。
第 1 步:添加 WordPress 引导文件 WordPress 的 wp-load.php 文件紧接在调用 CI 的 index.php 文件中的 Codeigniter 引导文件之前
在向 CI 文件添加任何 WordPress 功能之前,我们将进行测试以确保一切正常。我们应用程序的第一页看起来不错,但仅此而已。
我们应用程序中的每个 URL,包括表单处理程序,都被重定向到根目录(即 WordPress 的目录),导致 404 错误。我们发现 WordPress 和 Codeigniter 在全局范围内都有一个“site_url”函数。通过在 CodeIgniter 之前加载 WordPress 引导程序文件,我们发现 WordPress site_url.Codeigniter 函数“失败”了,因为它正在检查是否存在 site_url 函数。
第 2 步:扩展 Codeigniter URL Helper 我们的解决方案是“扩展”CodeIgniter 的 URL Helper。通过使用 ci_site_url,我们可以解决 site_url 问题。My_url_helper.php (helper) 文件应该在 CodeIgniter 的应用程序目录中。
第 3 步:在 CodeIgniter App 中将 site_url 更改为 ci_site_url。将所有 site_url 文件引用替换为 ci_site_url 到您的应用程序、控制器、模型和目录中。
一旦我们完成了这些步骤,我们的 CI 应用程序就可以工作了……
第 4 步:阻止 WordPress 管理我们的 cookie!我们仍然无法正常工作的网站是我们在 Codeigniter 中构建的管理面板。我们的凭据已被接受,但仍重定向到登录页面。
我想您知道 CodeIgniter 会在您登录时为每个活动的数据库会话创建 cookie。这可能会导致 WordPress 引导文件纠正问题。当 WordPress 通过 $_COOKIE 全局循环并为除 CodeIgniter cookie 之外的所有元素实现 add_magic_quotes 功能时,就会发生这种情况。
- 将 WordPress 集成到 CodeIgniter 如果您已经在 CodeIgniter 中创建了网站并添加了博客页面,那么 WordPress 是最佳选择,因此,首先,将 WordPress 安装在您网站上名为博客的文件夹中。它位于您的 CodeIgniter 网站的顶级目录中。所以它会在 CI 安装的系统文件夹旁边。如果您的站点已经有一个数据库,请让 WordPress 使用前缀将表添加到同一个数据库中,以使事情变得简单。
之后,在 CodeIgniter 的 index.php 文件顶部添加以下行。根据需要将路径更改为 wp-blog-header.php 以指向您的 WordPress 根目录。
全部完成!!此代码允许在不离开 CodeIgniter 站点的情况下访问 WordPress 博客。更多辅助功能在 WordPress 文档中有详细说明。
View more: How to Integrate CodeIgniter with WordPress