Ruby on Rails Integration With Wordpress

BananaNeil picture BananaNeil · Feb 14, 2012 · Viewed 23k times · Source

I have a client who has requested for me to build them a website with a very user friendly way to update content. They have expressed familiarity with wordpress, and expressed interest in being able to use the wordpress front-end to update their content.

I had originally intended to build them a simple admin page, where they can create posts, or add other types of content.. but it seems like wordpress has most of the functionality already in place.

The main problem is that i am a RoR developer. I prefer to use haml for every thing I do, and have 100% full control over how the site works.

So i was hoping someone out there would have an idea of a way i could still build the site using rails and haml, but still allow my client to update using wordpress. I thought maybe i could access the wordpress api, and just pull the content and display it the way i want? or maybe i should go with another CMS.. like Refinery?

Honestly, I just really dont want to have to touch PHP, and preferably use haml, rather than html. O_o

Answer

Ari Gesher picture Ari Gesher · Aug 1, 2012

This seems to be working for me (I'm loading from Wordpress as secondary database, hence the establish_connection() calls and overriding table_name. This should get most of the way there, giving you access to the Wordpress data as ActiveRecord objects. I haven't yet written the wrapper around Posts (WPPost) to make them a bit more user friendly from an API perspective, but this should work well for Rails-based display of Wordpress data.

class Term < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_terms"

   has_one :term_taxonomy
end


class TermTaxonomy < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_term_taxonomy"

   belongs_to :term
   has_many :term_relationship
end

class TermRelationship < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_term_relationships"

   belongs_to :post, :foreign_key => "object_id"
   belongs_to :term_taxonomy
   has_one :term, :through => :term_taxonomy
end

class Post < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_posts"

   has_many :term, :through => :term_relationship
   has_many :term_relationship, :foreign_key => "object_id"
   has_one  :postmeta

   # we only care about published posts for notifications
   default_scope where("post_type = 'post' and post_status = 'publish'")
end

class Postmeta < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_postmeta"

   belongs_to :post
end

I then wrap up the category in a simple ruby object that makes accessing the data easy:

class WPCategory
   attr_accessor :id
   attr_accessor :name
   attr_accessor :description
   attr_accessor :term

   def self.categories()
      categories = Term.all()
      categories = categories.select{|term| term.term_taxonomy.taxonomy == "category"}
      return categories.map{|term| WPCategory.new(term)}
   end

   def self.category(id=nil)
      if id
         term = Term.find(id)
         if term.term_taxonomy.taxonomy == "category"
            return WPCategory.new(term)
         end
      end
      return nil
   end

   def initialize(term)
      @id = term.term_id
      @name = term.name
      @description = term.term_taxonomy.description
      @term = term
   end

   def to_s
      return "Wordpress Category: '#{@name}' (id=#{@id})"
   end

end

Here's my database.yml (make sure that your db user has read-only access to the wordpress db to avoid any ActiveRecord mishaps):

test:
        adapter: mysql2
        encoding: utf8
        database: test-rails
        pool: 5
        username: test
        password: XXXXXX
        socket: /var/lib/mysql/mysql.sock

wordpress-test:
        adapter: mysql2
        encoding: utf8
        database: test-wordpress
        pool: 5
        username: test
        password: XXXXXXX
        socket: /var/lib/mysql/mysql.sock

wordpress-development:
        adapter: mysql2
        encoding: utf8
        database: wordpress
        pool: 5
        username: dev
        password: XXXXXX
        socket: /var/lib/mysql/mysql.sock

development:
        adapter: mysql2
        encoding: utf8
        database: dev
        pool: 5
        username: dev
        password: XXXXXX
        socket: /var/lib/mysql/mysql.sock