Quantcast
Channel: Marion Newlevant: Web Developer » CodeIgniter
Viewing all articles
Browse latest Browse all 2

Integrating WordPress & CodeIgniter

$
0
0

27 Jan 2013: eta: See also excelent writeup here: http://wildmice.ca/en/blog-posts/how-to-use-wordpress-inside-codeigniter/ by Shanna Mi

Ok! I got CodeIgniter and WordPress more or less integrated. Maybe an unholy marriage, but at the moment I think it’s very cool. This got me started:
http://jidd.jimisaacs.com/post/wordigniter-wordpress-codeigniter/

The short description is to have CodeIgniter as a wordpress theme. WordPress starts, hands things to the theme (CodeIgniter). CodeIgniter routes things WordPress can handle to views that look very much like standard WordPress loop etc., and does what it wants with anything else.

  1. Set up WordPress. Turn on permalinks. Make a static page the front page, move the blog to page blog (http://codex.wordpress.org/Creating_a_Static_Front_Page). Should all work, with theme twentyten.
  2. set up CodeIgniter
    • unzip codeigniter (1.7.2) OR 2.0 in /wp-content/themes/codeigniter
    • set up CodeIgniter file structure w/ application at same level as system (2.0 is already like this), and top level assets dir (not necessary, but I like it this way): http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/
    • ditch the index.php in the url. Minor mod in config.php, and .htaccess.
    • Important: config/config.php: $config['uri_protocol'] = “AUTO”;

      REQUEST_URI does not work reliably. Worked locally (MAMP), failed remotely (dreamhost).
    • For CodeIgniter 2.0 (1.7.2 didn’t have this problem), fix $system_path and $application_folder in index.php:
      //	$system_path = "system";
      	$system_path = realpath(dirname(__FILE__)).'/'."system";
      
      //	$application_folder = "application";
      	$application_folder = realpath(dirname(__FILE__)).'/'."application";
      
  3. Theme stuff
    • create minimal style.css for theme codeigniter
    • create functions.php
      <?php
      function codeigniter_bootstrap() {
        // with this and $config['uri_protocol']	= "AUTO";
        // the 0th _GET will be the REQUEST_URI, which is what we want for our routing
        $_GET[$_SERVER['REQUEST_URI']] = null;
        end($_GET);
        // standard codeigniter index.php gets us going.
        require_once( 'index.php' );
      }
      // Add the action before any templates load, but after all other WordPress system actions run
      add_action( 'template_redirect', 'codeigniter_bootstrap' );
      /* ... followed by the rest of twentyten functions.php */
      
  4. Add routes (application/config/routes.php):
    $route['index.html'] = 'home';
    
    if (!is_404()) { /* wordpress handles stuff it knows about */
    /* giant if statement from template-loader.php */
    // The following is similar to WordPress's template-loader.php - Use that as reference for this logic
    
      if (is_robots()) {
          do_action('do_robots');
          return;
      } elseif (is_feed()) {
          do_feed();
          return;
      } elseif (is_trackback()) {
          include(ABSPATH . 'wp-trackback.php');
          return;
      } elseif (is_search()) {
          $route['.*'] = 'blog/search';
      } elseif (is_tax()) {
          $route['.*'] = 'blog/taxonomy';
      } elseif (is_home()) {
          $route['.*'] = 'blog/index'; // this is the 'news' link
      } elseif (is_attachment()) {
          remove_filter('the_content', 'prepend_attachment');
          $route['.*'] = 'blog/attachment';
      } elseif (is_single()) {
          $route['.*'] = 'blog/single';
      } elseif (is_page()) {
          $route['.*'] = 'blog/page';
      } elseif (is_category()) {
          $route['.*'] = 'blog/category';
      } elseif (is_tag()) {
          $route['.*'] = 'blog/tag';
      } elseif (is_author()) {
          $route['.*'] = 'blog/author';
      } elseif (is_date()) {
          $route['.*'] = 'blog/date';
      } elseif (is_archive()) {
          $route['.*'] = 'blog/archive';
      } elseif (is_comments_popup()) {
          $route['.*'] = 'blog/comments_popup';
      } elseif (is_paged()) {
          $route['.*'] = 'blog/paged';
      } else {
          $route['.*'] = 'blog/index';
      }
    }
    
  5. Set up application/controllers/blog.php:
    function index() {
        $data = array(
          'contentMain'=>$this->load->view('blog/default', array('pageTitle'=>'News'), true),
          'contentSideBar'=>$this->load->view('blog/sidebar', array(), true),
          'currentPage'=>'news'
        );
        $this->load->view('_templates/normal_page', $data);
    }
    	/* and the others. Some are custom, others just call a default */
      function paged() { $this->_defaultBlogPage('paged'); }
      
      function _defaultBlogPage($pageTitle) {
        $data = array(
          'contentMain'=>$this->load->view('blog/default', array('pageTitle'=>$pageTitle), true),
          'contentSideBar'=>$this->load->view('blog/sidebar', array(), true),
          'currentPage'=>'news'
        );
        $this->load->view('_templates/normal_page', $data);
      }
    
  6. and views for blog. I busted it up, because it’s CodeIgniter and I can. Look at the twentyten loop.php and other theme pages. You need global $wp_query; in the views.
  7. The default search form uses get instead of post so CodeIgniter won’t like it. Create a custom searchform.php in theme top level (has to be there so wordpress finds it):
    <form role="search" method="post" id="searchform" action="/blog/search" >
    	<div><label class="screen-reader-text" for="s">Search for:</label>
    	<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
    	<input type="submit" id="searchsubmit" value="<?php echo esc_attr__('Search'); ?>" />
    	</div>
    </form>
    

    The blog/search controller looks like this:

      function search() {
        $s = $this->input->post('s'); // get our search string
        // use our search string, and only return posts
        query_posts('s='.$s.'&post_type=post');
        $data = array(
          'contentMain'=>$this->load->view('blog/search', array(), true),
          'contentSideBar'=>$this->load->view('blog/sidebar', array(), true),
          'currentPage'=>'news'
        );
        $this->load->view('_templates/normal_page', $data);
      }
    

    We are using the post param (instead of get), and only searching posts.

  8. hook up assets. assets (css, js, img) are in codeigniter, but the path we are using thinks they are at the root. So,
    add this to /.htaccess BEFORE the BEGIN WordPress bit:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^assets/(.*)$ wp-content/themes/codeigniter/assets/$1 [L]
    </IfModule>
    
  9. And the admin preview was broken. Possibly other things as well. The problem is that it uses get, and so we have urls like this: .../?preview=true&preview_id=9&preview_nonce=d2df45a801. What I did was allow ?, &, and =. Change in config.php:
    $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-\?\=\&';
    

    So get params are working, and I may not have needed the search work-around after all. Though the search workaround does cleanly search only posts and not pages, which is a win for me. Allowing all those chars in the url upens a can of worms. YMMV.


Viewing all articles
Browse latest Browse all 2

Trending Articles