php Ruby on Rails 与 Wordpress 的集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9277531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails Integration With Wordpress
提问by BananaNeil
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.
我有一个客户要求我为他们建立一个网站,以一种非常用户友好的方式来更新内容。他们表示熟悉 wordpress,并表示有兴趣能够使用 wordpress 前端来更新他们的内容。
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.
我原本打算为他们建立一个简单的管理页面,他们可以在其中创建帖子或添加其他类型的内容......但似乎 wordpress 已经拥有大部分功能。
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.
主要问题是我是一名 RoR 开发人员。我更喜欢在我做的每一件事上都使用 haml,并且可以 100% 完全控制网站的工作方式。
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?
所以我希望有人能想出一种方法,我仍然可以使用 rails 和 haml 构建站点,但仍然允许我的客户使用 wordpress 进行更新。我想也许我可以访问 wordpress api,然后提取内容并按照我想要的方式显示它?或者也许我应该使用另一个 CMS .. 像Refinery?
Honestly, I just really dont want to have to touch PHP, and preferably use haml, rather than html. O_o
老实说,我真的不想接触PHP,最好使用haml,而不是html。o_o
采纳答案by Michael Schmitz
The older answers are not relevant anymore. WordPress now provides a Rest API which can be accessed here: https://developer.wordpress.org/rest-api/
较旧的答案不再相关。WordPress 现在提供了一个 Rest API,可以在这里访问:https: //developer.wordpress.org/rest-api/
1) You will probably want to integrate all routing (by taking the "slug" from the articles) in your rails app to serve the articles correctly and present them with a nice "show" view.
1) 您可能希望在 Rails 应用程序中集成所有路由(通过从文章中获取“slug”)以正确提供文章并以漂亮的“显示”视图呈现它们。
2) If you want to store data in the rails system (e.g. for routing and to increase speed) you could create a database table called wp_articles, simply read the full article list or update relevant articles, then present them similar to your normal code.
2) 如果你想在 Rails 系统中存储数据(例如用于路由和提高速度),你可以创建一个名为 wp_articles 的数据库表,只需阅读完整的文章列表或更新相关文章,然后以类似于你的正常代码的方式呈现它们。
I've looked at the non-maintained MOMA gem (not required anymore, not maintained), checked the above answer with direct database access (huge effort, slower, outdated) and read about a slighlty complex direct javascript based solution here (http://marydickson.com/how-to-use-the-wordpress-rest-api-in-rails/), but I think that simply copying the relevant info into your system and then presenting them with the normal MVC process is the easiest way.
我查看了非维护的 MOMA gem(不再需要,不再维护),通过直接数据库访问(巨大的努力,速度较慢,过时)检查了上述答案,并在此处阅读了一个基于 javascript 的简单复杂的直接解决方案(http: //marydickson.com/how-to-use-the-wordpress-rest-api-in-rails/),但我认为简单地将相关信息复制到您的系统中,然后用正常的 MVC 流程呈现它们是最简单的道路。
Disadvantages: Some additional WP-Plugins provide more database fields and other information and its not clear whether you can always access those via the API. So you might have slightly limited functionality.
缺点:一些额外的 WP-Plugins 提供了更多的数据库字段和其他信息,并不清楚您是否总是可以通过 API 访问这些信息。因此,您的功能可能略有限制。
回答by Ari Gesher
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.
这似乎对我有用(我从 Wordpress 作为辅助数据库加载,因此establish_connection()
调用和覆盖table_name
。这应该可以大部分方式,使您可以访问 Wordpress 数据作为 ActiveRecord 对象。我还没有写Posts ( WPPost
)的包装器使它们从 API 的角度更加用户友好,但这应该适用于基于 Rails 的 Wordpress 数据显示。
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:
然后我将类别包装在一个简单的 ruby 对象中,以便轻松访问数据:
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):
这是我的 database.yml(确保您的 db 用户对 wordpress db 具有只读访问权限以避免任何 ActiveRecord 事故):
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
回答by Kevin C.
The Museum of Modern Art had a WordPress JSON API plugin built for this exact purpose: https://github.com/dphiffer/wp-json-api
现代艺术博物馆为此专门构建了一个 WordPress JSON API 插件: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.
这使他们能够构建基于 RoR 的前端层,同时维护 WordPress 驱动的后端层。
回答by davidb
You could install Wordpress then reproduce the wordpress database as Model
s and add the associations like wordpress uses them. Then you would be able to access the data using rails that were entered in the wordpress frontend. I did something like this in the past but not as a permanent solution but as a datasource for migration to another solution. Its possible, its not nice but it works.
您可以安装 Wordpress 然后将 wordpress 数据库复制为Model
s 并添加像 wordpress 使用它们一样的关联。然后您就可以使用在 wordpress 前端输入的 rails 访问数据。我过去做过类似的事情,但不是作为永久解决方案,而是作为迁移到另一个解决方案的数据源。它可能,它不是很好,但它有效。
But one question: Why are you using wordpress for a thing its not mighty enough?! Its a CMS not a framework for challenging tasks. If it doesnt fit the needs of the costumer its simply not the right thing to use. You could rather build a similar GUI using rails then fiddling with wordpress.
但有一个问题:你为什么要使用 wordpress 来处理它不够强大的事情?!它是一个 CMS,而不是一个挑战任务的框架。如果它不适合客户的需要,它根本就不是正确的使用方法。你宁愿使用 rails 构建一个类似的 GUI,然后摆弄 wordpress。
回答by ksol
Concerning HAML, you can still write your views in haml, and then use haml input.haml output.html
on the command line. A bit boring, but you don't have to write html.
关于HAML,你仍然可以用haml写你的视图,然后haml input.haml output.html
在命令行上使用。有点无聊,但你不必编写html。
回答by nana
use MAMP and install wordpress. open the page in your localhost. then you can use the firefox tool to see the html code.
使用 MAMP 并安装 wordpress。在本地主机中打开页面。那么就可以使用firefox工具查看html代码了。