Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Integration With Wordpress

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

like image 987
BananaNeil Avatar asked Feb 14 '12 13:02

BananaNeil


People also ask

Can you use Ruby on Rails with WordPress?

There's one scenario where you can use both WordPress and Ruby on Rails. Since ROR is a web application framework, you can create both front-end and back-end with it. You can create a web app with ROR and power the front-end with WordPress.

Is Ruby on Rails good for web development?

It's been around 24 years since the Ruby was launched and approximately 15 years since the Rails framework, or RoR, was launched. Evolving from then to now, Ruby on Rails is still one of the best web development choices. Ruby on Rails is well-known for its capabilities, ease of use and robustness.

Does Shopify still use Ruby on Rails?

Shopify builds most of its services with Ruby on Rails, as the company has noted for years. That makes sense because back when Shopify was founded in 2006, Ruby would have been one of the most appealing languages around, given its fast runtime and solid developer experience.

Is Ruby on Rails a web server?

Ruby on Rails also includes its own built-in web server, WEBrick. WEBrick is for development use and is not recommended for production.


2 Answers

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
like image 92
Ari Gesher Avatar answered Sep 27 '22 22:09

Ari Gesher


The Museum of Modern Art had a WordPress JSON API plugin built for this exact purpose: https://github.com/dphiffer/wp-json-api

This allowed them to build a RoR-based front-end layer while maintaining a WordPress-driven back-end layer.

like image 40
Kevin C. Avatar answered Sep 27 '22 22:09

Kevin C.