How to combine / integrate CodeIgniter and Wordpre

2019-04-02 22:06发布

问题:

My website having feature requirement of blogging. I have to make blog which would look same like my website appearance.

How to combine CodeIgniter and Wordpress blogging(only) functionality such that it should look like within same website?

I have seen this question: Wordpress template with codeigniter. But didn't got much clue.

回答1:

You do this you will need to create 2 files and modify 2 existing functions. One function is in CodeIgniter and the other is in Wordpress.

Here are the steps.

1.) Open your configs/hooks.php file and create a pre_controller hook as follows:

$hook['pre_controller'] = array(
    'class'    => '',
    'function' => 'wp_init',
    'filename' => 'wordpress_helper.php',
    'filepath' => 'helpers'
);

2.) Create a new file in your helpers directory called 'wordpress_helper.php', and add the following code to it:

/**
*
*/
function wp_init(){


    $CI =& get_instance();


    $do_blog = TRUE; // this can be a function call to determine whether to load  CI or WP


    /* here we check whether to do the blog and also we make sure this is a
    front-end index call so it does not interfere with other CI modules. 
    */
    if($do_blog 
        && ($CI->router->class == "index" && $CI->router->method == "index")
    )   
    {       

    // these Wordpress variables need to be globalized because this is a function here eh!
        global $post, $q_config, $wp;
        global $wp_rewrite, $wp_query, $wp_the_query;
        global $allowedentitynames;
        global $qs_openssl_functions_used; // this one is needed for qtranslate


        // this can be used to help run CI code within Wordpress.
        define("CIWORDPRESSED", TRUE);


        require_once './wp-load.php';

        define('WP_USE_THEMES', true);

        // Loads the WordPress Environment and Template 
        require('./wp-blog-header.php');

        // were done. No need to load any more CI stuff.
        die();


    }

}

3.) Open wp-includes/link-template.php and made the following edit:

if ( ! function_exists('site_url'))
{
    function site_url( $path = '', $scheme = null ) {
        return get_site_url( null, $path, $scheme );
    }

}

4.) Copy url_helper.php from the CodeIgniter helper folder to your APPPATH helper folder and make the following edit:

if ( ! function_exists('site_url'))
{
    function site_url($uri = '', $scheme = null)
    {

        // if we are in wordpress mode, do the wordpress thingy
        if(defined('CIWORDPRESSED') && CIWORDPRESSED){

            return get_site_url( null, $path, $scheme );

        }else{

            $CI =& get_instance();
            return $CI->config->site_url($uri);
        }
    }

}

The steps above will allow you to dynamically load either your CI app or your WP site based on some simple filtering. It also gives you access to all CI functionality within WP of that is something you can use.



回答2:

Seems like a bit of overkill.

Why not use a Restful service like json_api to retrieve your posts, then copy over the css file(parts)?