Alpha Wordpress Plugin for Delicious with Zend Framework’s Zend_Service_Delicious

I have Wordpress set up on PHP5. While Wordpress is largely a PHP4 entity, I wanted to see if it could play nicely with the Zend Framework, which is a PHP5-only entity. With some careful fiddling, it works pretty well.

With my Zend Framework code set up in my PHP include path, the first thing I needed to do to create a Wordpress Plugin was to enable my plugin to call the Zend Framework classes. First I tried this towards the end of my wp-config.php file:

PHP:
  1. require_once 'Zend/Loader.php';
  2. function __autoload($class)
  3. {
  4. Zend_Loader::loadClass($class);
  5. }

This caused Wordpress to freak out like a chihuahua on speed. Not good. I figured we'd have to do something special because we don't want ALL the classes to autoload, just the Zend ones. So I scrapped my previous __autoload and tried this instead:

PHP:
  1. require_once 'Zend/Loader.php';
  2. function __autoload($class)
  3. {
  4. $pos = strpos($class, 'Zend');
  5. if ($pos !== false)
  6. {
  7. Zend_Loader::loadClass($class);
  8. }
  9. }

Ahh, success. Sweeter then a brownie sundae with extra chocolate sauce.

From there, the rest is easy. Here's some basic code for the rz_delicious plugin, a basic PHP file you can drop into your Wordpress Plugins dir. It uses Zend_Cache and Zend_Service_Delicious to put your del.icio.us bookmarks onto your Wordpress blog. It first checks to see if there's a cached version already, if not, it uses the del.icio.us API to fetch your latest bookmarks. It does some list formatting there too because I didn't feel like handling that in my theme.

PHP:
  1. <?php
  2. /*
  3. Plugin Name: RZ Delicious
  4. Plugin URI: http://www.boringuys.com/
  5. Description: YADelicious Plugin
  6. Author: Rich Zygler
  7. Version: 1.0
  8. Author URI: http://www.boringguys.com/
  9. */
  10.  
  11. /*
  12.     called from theme via:
  13.     get_rz_delicious()
  14. */
  15.  
  16. function get_rz_delicious()
  17. {
  18.     $output = '';
  19.     $username = 'username';
  20.     $password = 'password';
  21.     $tag      = 'tagname';
  22.     $numPosts      = 15;
  23.     $cacheTime     = 3600;      // seconds
  24.     $cacheDir      = '/tmp/';
  25.  
  26.     $frontendOptions = array(
  27.        'lifetime' => $cacheTime,                  // in seconds
  28.        'automatic_serialization' => false  // this is default anyway
  29.     );
  30.  
  31.     $backendOptions = array('cache_dir' => $cacheDir);
  32.  
  33.     $cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);
  34.  
  35.     // we pass a unique identifier to the start() method
  36.     if(!$cache->start('rz_delicious'))
  37.     {
  38.         try
  39.         {
  40.             $delicious = new Zend_Service_Delicious($username, $password);
  41.             $posts = $delicious->getRecentPosts($tag,$numPosts)//$tag
  42.  
  43.             if (count($posts)> 0)
  44.             {
  45.                 $output .= '<ul id="delicious">';
  46.             }
  47.             foreach ($posts as $post)
  48.             {
  49.                 $output .= '<li><a href="' . $post->getUrl() . '" title="'$post->getNotes() . '">';
  50.                 $output .= $post->getTitle() . '</a> ';
  51.  
  52.                 $strTags = '<br />( tags: ';
  53.                 foreach ($post->getTags() as $tag)
  54.                 {
  55.                    if ($tag != 'boringguys.com')
  56.                    {
  57.                         $strTags .= '<a href="http://del.icio.us/' . $username . '/' . $tag . '" title="' . $tag . '">';
  58.                        $strTags .= $tag . '</a> | ';
  59.                    }
  60.                 }
  61.                 $strTags = trim($strTags, '| ');
  62.                 $output .= $strTags . ' )</li>';
  63.  
  64.             }
  65.             if (count($posts)> 0)
  66.             {
  67.                 $output .= '</ul>';
  68.             }
  69.         }
  70.         catch (Zend_Service_Delicious_Exception $e)
  71.         {
  72.                 // largely ignore the delicious service error
  73.                 $output .= '<ul id="delicious">';
  74.                 $output .= '<li>del.icio.us service unavailable</li>';
  75.                 $output .= '';
  76.         }
  77.         echo $output;
  78.         $cache->end(); // the output is saved and sent to the browser
  79.     }
  80.  
  81. }
  82.  
  83.  
  84. ?>

  1. 1 Trackback(s)

  2. Aug 5, 2008: Adolfsson’s blog » Blog Archive » Bookmarks for August 2, 2008 through August 5, 2008

Post a Comment

Close
E-mail It