elektroelch.net – blog

Ideas for pagebar v3 – Part 1: Direct page access

It’s time to think about version 3 of pagebar. In this series I try to explain what I am planning for this new major version and invite you to discuss my plans.

Part 1: Direct page access

I always wanted to add a fast and direct page selection. The easiest way would be to add a selection box containing all page numbers. This might work if you’re having about 10 pages but if your blog is really, really large, say about 300 pages, a select box with 300 numbers would be quite clumsy: Continue reading Ideas for pagebar v3 – Part 1: Direct page access

New version of pagebar released (v2.60)

As you can see in the title the new version of pagebar is not version 3.0 that I anticipated to release by this summer. Procrastination at its best!

1.

Instead I created another minor version with some features which are quite easy to implement. First there is a new default theme based on CSS3:

To activate the new layout you have to copy the included file pagebar.css to your current theme.

2.

I took the advice of Pippin and added some actions:

  • pagebar_before/pagebar_after (This action is executed before and after every kind of pagebar)

Additionally every bar has an action of its own:

  • postbar_before/postbar_after, commentbar_before/commentbar_after, multipagebar_before/multipagebar_after

3.

One of the main problems of the automagic insertion of postbbar was a second query in the sidebar displaying “Recent posts” that is used by many themes. Version 3.3 introduced a new function called “is_main_query” which enables the plugins to display the postbar only after the main main query. I hope this works for all themes because @toscho thinks that the function has some issues.

4.

Finally there is a new translation for pagebar: Romanian (ro_RO). I received the translation ages ago, excuses go to Web Geek Science

Links:

WordPress: Publishing posts in the past

There may be situations where you want to publish a post with a date in the past. On first sight WordPress does not have such a function. If you look closer you find on the page “Add new post” in the metabox “Publish” the following entry:

If you click on Edit a field appears in which you can enter the desired publishing date:

You can not only choose a date in the future but also a date in the past. After you have entered the date you should not forget to OK because otherwise the new date isn’t used when you publish the post:

If you press Publish the post will be published with the date set above:

Update: Monika Thon-Soun advised me that this practice may not be suitable if you’re focusing on search engine ranking since the subsequent entry may confuse Google et al.

WordPress: Get IDs of *ALL* post tags

WordPress contains a function for getting the tags associated with a post (get_the_tags) but none for retrieving the IDs of all site wide available tags.

The solution to this problem is quite easy if you remember that post tags are nothing else but pre-defined taxonomies and that you can use the function get_terms to retrieve the terms of a taxonomy:

$tag_ids = get_terms( 'post_tag', array('fields' => 'ids', 'get' => 'all') );

Funny enough there is a function for getting all category IDs (get_all_category_ids).

Yet another rant about bad WordPress plugin programming habits

The other day I came across a plugin that provided some useful functions to WordPress but is so badly coded that I can’t recommend it to anyone to use it. Though there are a lot of blog posts ranting about people “doing it wrong” this not be named plugin does so many things wrong in one place that I’m simply feeling the urge to write about a few of them.
Continue reading Yet another rant about bad WordPress plugin programming habits

My favorite Greasemonkey-Script

Greasemonkey is a fine Firefox-Addon that allows you to execute some Javascript on the page to change the content or do other stuff to enhance the experience.
My all-time favorite is Jasper’s Google Reader subscribe. What does it do?
Every blog, almost every news site, and many other web sites provide RSS feeds. They are a handy thing but some sites make it hard to find the corresponding link or don’t display it at all. The only chance to find the link would be to browse the source code of the page. Scanning the code while you’re in front of a computer? Ridiculous! Let’s use the machine and that’s what this script does.
It looks for links containing the strings “rss”, “atom”, or “rdf” and displays a small icon at the top right of your browser:

If you hover the mouse cursor over the icon all found feeds will be displayed:

As you see not only the main feed is found but also the comment feed and possible others. With this script installed you can simply click on this icon and subscribe to feeds without frantically scanning the page.

The selectbox mystery of WordPress

The other day I wanted to use a selectbox with 5 elements in my plugin’s option page. No problem I thought:

<select size="5">
  <option "test1" />
  <option "test2" />
  <option "test3" />
  <option "test4" />
  <option "test5" />
  <option "test6" />
</select>

What everybody including me expects is something like this:

But I actually got this:

The mystery lies in the style sheet for the admin area:

#wpcontent select {
	padding: 2px;
	height: 2em;
	font-size: 12px;
}

This forces every selectbox to display only one element. I have no idea why this is implemented. There is also a ticket in the WP tracking system discussing this behaviour since two years.

The solution to this problem is quite easy:

<select style="height:auto" size="5">

Seems easy but costed me over 1 hour of my lifetime.

WordPress: Shorten title

A frequently asked question in WordPress forum is how to shorten the title of a post to a predefined number of characters and to append a “…”.

The problem is solved with a few lines of code:

1
2
3
4
5
6
7
8
9
add_filter( 'the_title', 'short_title' );
function short_title( $title ) {
  $chars = 20;
  if ( strlen( $title ) > $chars ) {
    $title = substr( $title, 0, $chars );
    $title .= " &hellip;";
  }
  return $title;
}

The variable $chars has to be set to the number of characters you want to have in your title and in line 6 you have to define the text to replace the omitted part of the title (&hellip; is the typographically correct version of three points, called ellipse).

It’s a bit disadvantageous that the title could be cut in the middle of a word. If you like you can extend the function accordingly:

1
2
3
4
5
6
7
8
9
10
add_filter( 'the_title', 'short_title' );
function short_title( $title ) {
  $chars = 20;
  if ( strlen( $title ) > $chars ) {
    $title = substr( $title, 0, $chars );
    $title = substr($title, 0, strrpos( $title, ' ' ) );		    $title .= " &hellip;";
  }
  return $title;
}

If you copy the function into the file functions.php of your theme all calls of the_title will return the shortened title.