dtcpvz8162 2015-03-19 05:24
浏览 80

在Wordpress中加载CodeIgniter实例

The problem I had with using Wordpress was wanting to incorporate CodeIgniter functionality into Wordpress theme files. For example, there are a header and footer that I include in many of my CodeIgniter view files. That same header and footer also needed to be included in the Wordpress theme.

The idea is to load CI instance and use it on Wordpress. Any help would be appreciated!

I have tried this.

https://github.com/falexandrou/Codeigniter-Wordpress-Integration

The file path to load CI bootstrap is correct already. Its result was Error

Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php

the sites step:
1. Include this in the following template files at the top:

 - 404.php - index.php- archive.php- archives.php
 - links.php -page.php - search.php - single.php */

 <?php
  $wp_ci['return'] = true;
 require('codeigniter.php');
  1. put codeigniter file along the template dir
    This is supposed to load CI instance so it can be used in Wordpress.
    https://github.com/falexandrou/Codeigniter-Wordpress-Integration/blob/master/ci-wp-thedaylightstudio/codeigniter.php

CI index.php

 <?php
 $wp_did_header = true;         
 require_once dirname( __FILE__ ) .'/blog/wp-blog-header.php';
 define('WP_USE_THEMES', true);
 require_once BASEPATH.'core/CodeIgniter.php';
 ?>

UPDATE:

wp-blog-header

  if ( !isset($wp_did_header) ) {
$wp_did_header = true;
require_once( dirname(__FILE__) . '/wp-load.php' );
wp();
require_once( ABSPATH . WPINC . '/template-loader.php' );
 }

I successfully load the codeigniter view on wordpress but all my ci_site_url function returns website.com/blog/application when it should be website.com/application I put this code on one of WP theme index.php

  define('STDIN', TRUE);
  $_SERVER['argv'] = array();
  ob_start();
  require('../index.php');
  $GLOBALS['wp_ci_output'] = ob_get_contents();
  ob_end_clean();
  $GLOBALS['CI'] = get_instance();
  require_once(APPPATH.'helpers/wordpress_helper.php');

Code Reference:

http://thedaylightstudio.com/blog/2010/06/16/codeigniter-and-wordpress-integration

wordpress-helper.php

/**
* Print codeigniter view file in wordpress content
*
* @access   public
* @param    string view file name
* @param    array array of variables to be rendered
* @param    boolean true to return, false to return
* @return   mixed
*/

function wp_ci_load_view($view, $vars = array(), $return = FALSE){
 extract($vars);
  $file = APPPATH.'/views/'.$view.'.php';
  $contents = file_get_contents($file);
  //echo $contents;
  $CI =& get_instance();
  $check_funcs = array('ci_site_url(', '$this->');

  $replace_funcs = array('site_url_wp_safe(', '$CI->');
  $contents = str_replace($check_funcs, $replace_funcs, $contents);
  $contents = eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", 
  str_replace('<?=', '<?php echo ', $contents)).'<?php ');

   if ($return === TRUE){
   return $contents;
   }
 }

https://github.com/falexandrou/Codeigniter-Wordpress-Integration/blob/master/ci-wp-thedaylightstudio/wordpress_helper.php

The writer did say an issue: The only issue with this version is that it always renders the same CI page to get the CI object and you can’t manipulate which page it renders. Thanks.

MY_url_helper

function ci_site_url: to differ CI site_url and WP site url function

 if ( ! function_exists('ci_site_url')){
   function ci_site_url($uri = '')
   {
    $CI =& get_instance();
    return $CI->config->site_url($uri);
    }
  }
  • 写回答

1条回答 默认 最新

  • dsklzerpx64815631 2015-03-25 13:37
    关注

    look I have added below code in my helper file of codeigniter and add this helper in config/autoload.php file to autoload it automatically.

    /**
     * return CI header to home page cms for 
     */
    if (!function_exists('ci_header')) {
    
        function ci_header() {
            $CI = & get_instance();
            $header = $CI->load->view('includes/header.php', '', true);
    
            return $header;
        }
    
    }
    
    /**
     * return CI footer to home page cms for 
     */
    if (!function_exists('ci_footer')) {
    
        function ci_footer() {
            $CI = & get_instance();
            $footer = $CI->load->view('includes/footer_homepage.php', '', true);
    
            return $footer;
        }
    
    }
    

    and put below code in wordpress wp-content/themes/my_theme/header.php

    <?php
        echo ci_header();
    ?>
    

    and put below code in wordpress wp-content/themes/my_theme/footer.php

    <?php
    echo ci_footer();
    ?>
    

    you can use any helper function in wordpress theme templete directly as i am calling ci_header() and ci_footer();

    Go ahead... and summarize me what else you need to do for your project...

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口