Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total

Use the contents of a WordPress database in your Rails app

These two models can be used to access the posts and associated comments of a WordPress database.

class WpBlogComment < ActiveRecord::Base

  # if wordpress tables live in a different database (i.e. 'wordpress') change the following
  # line to set_table_name "wordpress.wp_comments"
  # don't forget to give the db user permissions to access the wordpress db
  set_table_name "wp_comments"
  set_primary_key "comment_ID"

  belongs_to :post , :class_name => "WpBlogPost", :foreign_key => "comment_post_ID"

  validates_presence_of :comment_post_ID, :comment_author, :comment_content, :comment_author_email
  
  def validate_on_create
    if WpBlogPost.find(comment_post_ID).comment_status != 'open'
      errors.add_to_base('Sorry, comments are closed for this post')
    end
  end

end 


class WpBlogPost < ActiveRecord::Base

  set_table_name "wp_posts"
  set_primary_key "ID"

  has_many :comments, :class_name => "WpBlogComment", :foreign_key => "comment_post_ID"

  def self.find_by_permalink(year, month, day, title)
    find(:first, 
         :conditions => ["YEAR(post_date) = ? AND MONTH(post_date) = ? AND DAYOFMONTH(post_date) = ? AND post_name = ?", year.to_i, month.to_i, day.to_i, title])
  end
end 


Display In 2-Column Table

The below can be applied to MT, EE, etc, and will display the content in a 2 column table. The EE tags below are just an example, it will work equally well if you replace it with MT, Wordpress, etc, tags.

<? $set_table="0"; ?>

<table cellpadding="5" border="0">
{exp:gallery:categories gallery="{gallery_name}"}
<? 
$set_table = $set_table +1;
if ($set_table == "1") {echo"<tr>";} ?>


Insert other tags here.


<? if ($set_table == "2") {echo"</tr>";  $set_table="0";} ?>
{/exp:gallery:categories}
</table>

redirecting wordpress xml links to typo in lighttpd

Update: go for the simplest regex possible...

$HTTP["host"] =~ "awesomerailsapp.com" {
  server.error-handler-404 = "/dispatch.fcgi"
  server.document-root = "/awesomerailsapp/public/"
  url.redirect = (
    "^/feed/atom" => "http://awesomerailsapp.com/xml/atom/feed.xml",
    "^/feed" => "http://awesomerailsapp.com/xml/rss/feed.xml",
    "^/wp-rss2\.php" => "http://awesomerailsapp.com/xml/rss/feed.xml",
    "^/wp-atom\.php" => "http://awesomerailsapp.com/xml/atom/feed.xml",
    # my wp article format to typo's
    "^/(\d{4})/(\d{2})/(\d{2})/([\w-_]+)" => "/articles/$1/$2/$3/$4"
  )

  # bring on the rest of the config
}

WordPress with clean urls on Lighttpd

This single line seems to work for my WP install. I needed to add a couple of redirects and rewrites to handle my RSS feed, since I use Feedburner, but this line does all the heavy lifting...

Place it after server.document-root, either in the main section or in your virtual host sub-sections...

server.error-handler-404 = "/index.php?error=404"


and use this as your clean urls template

/%year%/%monthnum%/%day%/%postname%/
« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total