How to integrate WordPress template with CodeIgniter

前端 未结 5 2155
孤独总比滥情好
孤独总比滥情好 2020-11-28 21:37

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?

5条回答
  •  孤独总比滥情好
    2020-11-28 22:25

    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.

    Directory Structure:

    / - WordPress
    /ci/ - codeigniter
    

    /ci/index.php (Top of CI Index file)

    $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.

    /ci/application/helpers/MY_url_helper.php

    '.$title.'';
    }
    
    
    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 "".$title."";
        }
    
        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 "".$title."";
    }
    
    
    
    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.

提交回复
热议问题