Skip to content

Instantly share code, notes, and snippets.

@abrkof
Forked from philbirnie/Instructions
Created August 13, 2018 17:19
Show Gist options
  • Save abrkof/8e3855cc360a87dfaca80549d28304c5 to your computer and use it in GitHub Desktop.
Save abrkof/8e3855cc360a87dfaca80549d28304c5 to your computer and use it in GitHub Desktop.
Steps to Combine Wordpress and Codeigniter when CI is in a subdirectory of Wordpress
Instructions
1. Add MY_url_helper.php to CI_directory/application/helpers
2. If the CI app is already built, convert any references to *site_url* in your CI application (aside from the system directory) to the new namespaced function, ci_site_url.
This step prevents Wordpress' site_url function from overwriting CIs. Because both functions are global and CI checks to make sure that site_url has not been set. Once we load the WP bootstrap file, it will have been defined, so CI's function wll never load.
3. Add Wordpress' bootstrap file into CI_directory/index.php right above CI's bootstrap file.
4. Update wp-includes/load.php *(this is necessary if you are using CI's sessions - Wordpress mangles CI's cookies using with magic quotes. (There may be an upgrade-proof way to do this.)
<?php
/** wp-includes/load.php */
/** Aproximately line 25 */
// Variables that shouldn't be unset
// Add whatever CI's cookie name or, '_SESSION'
$no_unset = array( 'GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix','ci_session' );
/** Approximately line 524 */
/** Add this function */
/**
* Applies Magic Quotes to the $_COOKIE global but ignores Codeigniter's Cookie
* @param string $value Value passed by array_walk function
* @param string $key Key passed by array_walk function
*/
function ci_ignore_magic_quotes($value,$key)
{
if($key != "ci_session")
{
stripslashes_deep($value);
}
}
/* Update this function */
/**
* Add magic quotes to $_GET, $_POST, $_COOKIE, and $_SERVER.
*
* Also forces $_REQUEST to be $_GET + $_POST. If $_SERVER, $_COOKIE,
* or $_ENV are needed, use those superglobals directly.
*
* @access private
* @since 3.0.0
*/
function wp_magic_quotes() {
// If already slashed, strip.
if ( get_magic_quotes_gpc() ) {
$_GET = stripslashes_deep( $_GET );
$_POST = stripslashes_deep( $_POST );
$_COOKIE = stripslashes_deep( $_COOKIE );
}
// Escape with wpdb.
$_GET = add_magic_quotes( $_GET );
$_POST = add_magic_quotes( $_POST );
array_walk($_COOKIE, 'ci_ignore_magic_quotes');
//$_COOKIE = add_magic_quotes( $_COOKIE );
$_SERVER = add_magic_quotes( $_SERVER );
// Force REQUEST to be GET + POST.
$_REQUEST = array_merge( $_GET, $_POST );
}
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_url_helper.php
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author Phil Birnie
* @copyright Copyright (c) 2013
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
// We need to overwrite some of CIs methods that use site_url and replace them with our
// new namespaced function ci_site_url
// ------------------------------------------------------------------------
/**
* Site URL
*
* Create a local URL based on your basepath. Segments can be passed via the
* first parameter either as a string or an array.
*
* @access public
* @param string
* @return string
*/
if ( ! function_exists('ci_site_url'))
{
function ci_site_url($uri = '')
{
$CI =& get_instance();
return $CI->config->site_url($uri);
}
}
// ------------------------------------------------------------------------
/**
* Anchor Link
*
* Creates an anchor based on the local URL.
*
* @access public
* @param string the URL
* @param string the link title
* @param mixed any attributes
* @return string
*/
if ( ! function_exists('anchor'))
{
function anchor($uri = '', $title = '', $attributes = '')
{
$title = (string) $title;
if ( ! is_array($uri))
{
$site_url = ( ! preg_match('!^\w+://! i', $uri)) ? 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>';
}
}
// ------------------------------------------------------------------------
/**
* Anchor Link - Pop-up version
*
* Creates an anchor based on the local URL. The link
* opens a new window based on the attributes specified.
*
* @access public
* @param string the URL
* @param string the link title
* @param mixed any attributes
* @return string
*/
if ( ! function_exists('anchor_popup'))
{
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>";
}
}
// ------------------------------------------------------------------------
/**
* Header Redirect
*
* Header redirect in two flavors
* For very fine grained control over headers, you could use the Output
* Library's set_header() function.
*
* @access public
* @param string the URL
* @param string the method: location or redirect
* @return string
*/
if ( ! function_exists('redirect'))
{
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;
}
}
/* End of file MY_url_helper.php */
/* Location: ./application/helpers/MY_url_helper.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment