A-  A  A+ RSS Feed

Deep Thoughts by Robert Felty

thoughts on wordpress, latex, cooking et alia

Archive for the 'wordpress' Category

Monday, August 24th, 2009

Writing your own custom filters for postie

In version 1.3.1 of postie, it is now easy to add custom filters to the content of your posts. Best of all, by using filters, you can easily write your own plugin, which will not be affected by upgrades to postie (as opposed to modifying the postie functions directly). There are two examples now included in version 1.3.1 of postie, in the filterPostie.php file. Here is how to write your own filter.

  1. Copy the filterPostie.php into your plugins directory
  2. Change the metadata at the top of the file, such as the author and plugin name
  3. Modify the example functions to your liking, or create your own
  4. Activate the plugin

A few more details. After modifying the headers, you might have something like the following:

/* Plugin Name: My own Postie Filter Plugin
URI: http://mywebsite
Description: Adds a custom field to each post called postie
Version: 0.1 Author: Joe Bloe
Author URI: http://mywebsite.com
*/

Just as with any filter for wordpress, you have 2 parts.

  1. A function which receives an argument, makes some modifications (filtering) and returns a result
  2. A function call to add this new function to the list of filters for a certain action

As you can see in the example file, I have created two functions, filter_title, and filter_content. After creating these functions, I then “hook” them into postie using the add_filter function.

add_filter('postie_post', 'filter_title');
add_filter('postie_post', 'filter_content');

Now lets create our new function to add a custom field

function add_custom_field($post) {
  //this function appends "(via postie)" to the title (subject)
  add_post_meta($post['ID'], 'postie', 'postie');
  return ($post);
}

Finally, we let wordpress know that this function should filter any post that is sent via postie

add_filter('postie_post', 'add_custom_field');

Currently our function is really not that useful, since the key and value of our custom field are the same. However, there is really very little limit to what we can do with filters. Perhaps a more useful function would be an auto- tagger. Suppose I often write about cooking, wordpress, and latex (which I do). But when I am posting via e-mail, I frequently forget to add tags. We could write a little filter function to read the body of the post, and automatically add tags depending on the content. Here is what it might look like:

function auto_tag($post) {  
  // this function automatically inserts tags for a post  
  $my_tags=array('cooking', 'latex', 'wordpress');  
  foreach ($my_tags as $my_tag) {    
    if (stripos($post['post_content'], $my_tag)!==false)      
      array_push($post['tags_input'], $my_tag);  
  }  
  return ($post);
}
add_filter('postie_post', 'auto_tag');

I’m sure that users will come up with many other interesting filters. If you develop any you would like to share, please post them on the postie forum

Tuesday, August 18th, 2009

Blogging with LaTeX

The first question on reader’s mind must be — why use LaTeX to blog?

Well, I have a pretty specific instance in mind, but I can imagine that others might be interested as well. This fall I am teaching a course on computational corpus linguistics at CU Boulder. I like to have some materials online for the students, such as the syllabus, course notes, etc. I thought about setting up a simple webpage, but then I decided instead to use wordpress, because it would give me extra functionality, such as auto-generating rss feeds, and with some plugins, would allow me to notify students via e-mail when I post lecture notes or slides, or homework tips. The one drawback I could see is that it would be hard to update the syllabus throughout the semester, as I tend to change the calendar some throughout the semester. Then I thought that maybe I could hack up a quick xml-rpc solution to the problem, and about an hour later, I had it done. All I needed was the WordPress::API perl module from CPAN.

Now I have a handy little publish script which first compiles my syllabus as pdf, then compiles it as html using plasTeX. Then I run it through tidy for formatting and hack in a few more things. Then I update the syllabus page on the course blog via xml-rpc. Finally, I rsync the pdf to the server. And with the post-notification plugin for wordpress, students will get an e-mail when the syllabus is updated.

Here are the scripts:

publish

#!/bin/bash
# this script compiles a pdf version, an html version, and then copies it to my
# webserver

# compile as pdf
pdflatex syllabus && pdflatex syllabus

# compile as html
plastex  -c syllabus.cfg syllabus
# clean up code a bit
tidy --show-body-only true --ascii-chars true  -asxhtml --input-encoding utf8 --output-encoding ascii -wrap 0 -indent --tab-size 4 syllabus/index.html > syllabus/syllabus.tmp

# add in link to pdf version in the html version
cat pdflink syllabus/syllabus.tmp > syllabus/syllabus.html

# update the syllabus page via xml-rpc
./updateSyllabusPage.pl

# upload the newest version of the pdf
rsync -avzu syllabus.pdf robfelty:/var/www/html/robfelty/teaching/ling5200Fall2009/ling5200Fall2009-syllabus.pdf

updateSyllabusPage.pl

#!/usr/bin/perl -w
use WordPress::API;

# get new syllabus content
my $contentFile = 'syllabus/syllabus.html';
open(CONTENT, $contentFile);

# slurp in content
$content = do {local ( $/ ); <CONTENT> };

my $w = WordPress::API->new({
   proxy => 'http://robfelty/teaching/ling5200Fall2009/xmlrpc.php',
   username => 'myusername',
   password => 'mypassword',
});

my $page = $w->page(3);

$page->description($content);
$page->save;

Finally, if you want to see how I customize content for pdf and html separately, you can check out the LaTeX source file

Friday, August 14th, 2009

Postie 1.3.0 released

postie_200x200_7After more than a month of fixing bugs and adding features, I have decided it is time to release Postie 1.3.0, the wordpress plugin which adds many features to post to your blog via e-mail. 1.3 offers many new features over the 1.2 line of postie, including better attachment handling, better language and character set support, and easier configuration. Here is a list of the new features and bug fixes from 1.3.beta to 1.3.0.

  • Features
    • Added mpeg4 to default list of videotypes
    • Added support for KOI8-R character set (cyrillic)
    • Added support for iso-8859-2 character set (eastern european)
    • Added option to include custom icons for attachments
    • Added option to send confirmation message to sender
    • Enhanced e-mails for unauthorized users
    • Added option to send unauthorized e-mail back to sender
    • Added option to only allow e-mails from a specified list of smtp
    • servers

    • Added option to use shortcode for embedding videos (works with the
    • videos plugin http://www.daburna.de/download/videos-plugin.zip

    • Better handling of comment authors (thanks to Petter for suggestion)
    • Simplified message options (now includes an advanced options section)
    • Added filter ability for post content
  • Bug fixes
    • No longer including wp-config.php
    • If tmpdir is not writable, try a different tmpdir
    • More subject encoding fixes
    • Updated image templates, which were causing problems for cron
    • Fixed in text captions
    • Fixed SQL problems when updating options
    • Fixed name clashes with other plugins
    • Fixed custom image field

I want to thank the many users who were willing to test out new features and bug fixes by trying out the development version. in particular, I want to thank Kyle and Hab at Abilene Christian University for offering me financial support to develop some additional features for their use of postie for a course at their university. I think that other postie users will enjoy some of these new features as well.

Friday, July 24th, 2009

On blog navigation

A few navigation choices after reading an article on a blog

A few navigation choices after reading an article on a blog

I was recently debating with my friend Danny how people navigate sites, especially blogs. As the author of 3 plugins designed to enhance site navigation (collapsing archives, categories, and pages), I of course had my opinions, but what is really compelling is data. So I finally decided to modify my links on my site a bit in order to figure out more exactly how people are navigating my site. There are several different ways in which people can navigate to a particular page on my site.

  • external
    • search
    • social network (digg, reddit, friendfeed, etc.)
  • internal
    • collapsing archives widget
    • collpsing categorries widget
    • collapsing pages widget
    • category page
    • archive page
    • search
    • index page link
    • prev page link
    • next page link

I added a ?nav query to each of these particular options, so that I could then find those values in the server logs. I wrote a little bash script which mostly does some grepping of the log. Here is the script, and the results for the last 12 hours or so. My plan is to collect about 30 days worth of data, and see what the data shows. I think the results will be informative.

#!/bin/bash
# calculate how people navigate my site

# first we filter out all spiders, any internal blog stuff, and requests from
# my own ip, and then pipe that to filter out page refreshes
grep -vEi '(Mediapartners-Google|bot|spider|crawler|slurp|scoutjet|httpunit|htmlparser|favicon|wp-cron.php|wp-content|wp-photos|wp-comments|feed|fail\.php|wp-login.php|POST|\/images|wp-includes|forum|71\.237\.102\.16)' experiment_log |grep -Ev '"GET (.*) .*\1' > nobots

# next we separate into internal and external referers
grep 'http://blog.robfelty.com' nobots > INTERNAL
grep -v 'http://blog.robfelty.com' nobots > EXTERNAL

EXTSEARCH=`grep -ciE '(yahoo|bing|google)' EXTERNAL`
SOCIAL=`grep -ciE '(fark|reddit|digg|slashdot|friendfeed|facebook|twitter)' EXTERNAL`
NOREFERER=`grep -c '"-"' EXTERNAL`
EXTREMAINING=`grep -viE '(yahoo|bing|google|fark|reddit|digg|slashdot|friendfeed|facebook|twitter)' EXTERNAL|grep -cv '"-"'`
COLLARCH=`grep -c '?nav=collapsing-archives' INTERNAL`
COLLCAT=`grep -c '?nav=collapsing-category' INTERNAL`
YEARLY=`grep -c '?nav=yearly' INTERNAL`
MONTHLY=`grep -c '?nav=monthly' INTERNAL`
CAT=`grep -c '?nav=category' INTERNAL`
COLLPAGE=`grep -c '?nav=collapsing-pages' INTERNAL`
SEARCH=`grep -c '?nav=search' INTERNAL`
INDEX=`grep -c '?nav=index' INTERNAL`
PREV=`grep -c '?nav=previous' INTERNAL`
NEXT=`grep -c '?nav=next' INTERNAL`
TOTAL=`wc -l nobots|cut -f1 -d ' '`
INTERNAL=`wc -l INTERNAL|cut -f1 -d ' '`
EXTERNAL=`wc -l EXTERNAL|cut -f1 -d ' '`
# Only firefox prefetches
PREFETCH=`grep -vE '\?nav=(collapsing-category|collapsing-archives|search|index|previous|next)' INTERNAL|grep -c 'Firefox'`
INTREMAINING=`grep -vE '\?nav=(collapsing-category|collapsing-archives|search|index|previous|next)' INTERNAL|grep -cv 'Firefox'`
echo "TOTAL PAGES = $TOTAL"
echo "  EXTERNAL= $EXTERNAL"
echo "    EXTSEARCH = $EXTSEARCH"
echo "    SOCIAL = $SOCIAL"
echo "    NOREFERER = $NOREFERER"
echo "    REMAINING = $EXTREMAINING"
echo "  INTERNAL= $INTERNAL"
echo "    COLLARCH = $COLLARCH"
echo "    YEARLY = $YEARLY"
echo "    MONTHLY = $MONTHLY"
echo "    COLLCAT = $COLLCAT"
echo "    CAT = $CAT"
echo "    COLLPAGE = $COLLPAGE"
echo "    SEARCH = $SEARCH"
echo "    INDEX = $INDEX"
echo "    PREV = $PREV"
echo "    NEXT = $NEXT"
echo "    PREFETCH = $PREFETCH"
echo "    REMAINING = $INTREMAINING"
TOTAL PAGES = 197
  EXTERNAL= 131
    EXTSEARCH = 51
    SOCIAL = 0
    NOREFERER = 43
    REMAINING = 37
  INTERNAL= 66
    COLLARCH = 3
    YEARLY = 0
    MONTHLY = 0
    COLLCAT = 4
    CAT = 0
    COLLPAGE = 0
    SEARCH = 1
    INDEX = 1
    PREV = 0
    NEXT = 0
    PREFETCH = 52
    REMAINING = 5
Wednesday, July 8th, 2009

BBpress forums and wordpress plugin development versions

Gentle reminders for forum users

Gentle reminders for forum users


I host a bbpress forum for each of my wordpress plugins, so that people can report bugs and ask for new features. Bbpress has many of the same features as wordpress, and it integrates very well with wordpress. It is also very fast, and as of version 1.0, now has the same admin interface as wordpress.

When people report bugs on the forum, I frequently try to fix them in the development version of the plugin, and ask them to test it, which requires downloading the development version from the wordpress.org site. Unfortunately, the development version is a bit hidden there, and I have gotten very tired of typing out the url in my replies, so I finally modified the topic.php file in my bbpress template to include a link to the development version (and a link to the faq too).

One downside of bbpress is that there is no documentation yet, so you have to search around for different functions in the code. I have done this several times now, so that it only took me about 20 minutes to figure out how to implement what I wanted. Here is the code I added (just before post_form())

<?php
$thisforum= bb_get_forum(get_forum_id(0));
  $development='http://wordpress.org/extend/plugins/' . $thisforum->forum_slug .
      '/download';
  $faq='http://wordpress.org/extend/plugins/' . $thisforum->forum_slug .
      '/faq';
?>
  <div class='alert'>
  Have you read the <a href='<?php echo $faq; ?>'>FAQ</a>?<br />
  Have you tried the latest <a href='<?php echo $development; ?>'>development version</a> (at the bottom)?
  </div>

I then added the following css to my theme to make it stand out:

.alert {border:1px solid #999;
        background-color: #FBB;
        font-size:1.1.em;
        padding:.2em;
       }