Sam Burdge – Smarter Than The Average Blog

WordPress attachments hack

09.06.2009 (3:19 pm) – Filed under: blog, development, scripts, wordpress

While working on a WordPress site recently I came up against a problem that had never occurred to me before. The client wanted each post excerpt on the homepage to include an image which when clicked would link to the main post (permalink) itself. The built in functions of wordpress when inserting an image allow you to link to either the 'File URL' (i.e. the full size version of the image) or to the 'Post URL' which is actually an attachment page in wordpress which displays the image, but does not include the text of the actual post itself. However, without either editing the html of each post (not an option for this client) or inserting the image without a link, and then adding the permalink to the image (too long winded) there was no simple way to create the image links in the way they wanted them.

My solution to this was to create a very basic hack which would allow them to use the 'Post URL' link when inserting images, but instead of linking to the attachment page, the link would automatically be updated to link to the post's permalink itself.

Here is the basic php function that you would need to insert into your theme's functions.php file:

function lose_attachment($content){
return preg_replace('/<a(.*?)href="(.*?)\/attachment\/(.*?)"/i', '<a$1href="$2"', $content);
}

add_filter('the_excerpt', 'lose_attachment',2);

This will remove the 'attachment/name-of-image-file' part from the URL making it link to the post's permalink itself.  I hope someone out there finds this useful!

WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages

29.01.2009 (11:47 pm) – Filed under: development, scripts, wordpress

I noticed in the WordPress forums that a lot of people were asking how to remove the title="example" attribute from the links generated by the WordPress template tags wp_list_categories and wp_list_pages. The title attribute is what generates the little text box with something like "View all posts filed under <category>" when you hover over a link.

I had previously devised a 'hack' for removing the dreaded title attribute for a theme I worked on. The key is to set the 'echo' parameter of the function to '0' and then to use preg_replace code to remove the title attribute. Here is the code:

wp_list_categories:

<?php
$cool_cats = wp_list_categories('echo=0');
$cool_cats = preg_replace('/title=\"(.*?)\"/','',$cool_cats);
echo $cool_cats;
?>

wp_list_pages:

<?php
$clean_page_list = wp_list_pages('echo=0');
$clean_page_list = preg_replace('/title=\"(.*?)\"/','',$clean_page_list);
echo $clean_page_list;
?>

Personally I don't mind having the titles on my site navigation, and I use the NAVT plugin to generate my nav, so the template tags don't apply for me.

WordPress - Change Default Email Address Plugin

20.02.2008 (12:04 am) – Filed under: development, plugins, wordpress

This WordPress plugin changes the default email address that all notifications sent from your blog are addressed from. The default address for all emails sent by your blog is currently wordpress@ yoursite.com.

I had seen some tutorials / forum entries previously where people suggested editing the core WordPress file 'pluggable.php' to overide this default. This method does work, however you would have to repeat the hack every time you upgrade to a new version of WordPress so it is not an ideal solution.

Faced with this problem for a site I was working on recently, I wrote this basic plugin which allows you to configure your own email address in the format: Your Name <yourname@yoursite.com>.

Installation:

  1. Upload the file wp_change_default_email.php to your wp-content/plugins folder
  2. Activate the plugin from the Plugins page in WordPress

Usage:

  1. In the Plugins page in WordPress click the edit button next to the plugin in the list.
  2. Scroll down until you see a note saying 'Configure it'
  3. Enter your name and email as shown
  4. If you don't enter a new address the default 'wordpress@' will remain

Download:


Al Fingers Website

20.11.2007 (3:28 pm) – Filed under: portfolio

Al Fingers Site 1Al Fingers Site 2

This site, built for DJ / Remix artist Al Fingers, is 100% ninja! It's powered by wordpress, believe it or not, but the theme is like no other wordpress theme I have ever seen. It uses iframes to load all the different categories into seperate lozenges on the home page. It is also backed up by the more conventional wordpress search and display single posts on a page. For this site I also created a unique 'View Tracklist' function for the Mixtapes category, which opens the tracklist for each mixtape in a pop-up window. The site utilises my monthly and reverse nav scripts which can be found here:

{{post id="wordpress-monthly-navigation" text="Wordpress Monthly Navigation" target="_self"}} | {{post id="wordpress-post-navigation-hacks" text="Wordpress Post Navigation Hacks" target="_self"}}

This is truly the Millenium Falcon of wordpress themes, and a must for all music lovers.
Check it out: www.alfingers.com

wordpress post navigation hacks

24.10.2007 (2:34 pm) – Filed under: development, scripts, wordpress

This article covers using the global parameter $paged and the query $wp_query->max_num_pages; to create custom navigation links for previous and next posts. For some reason wordpress treats posts in the past as "next" and posts in the future as "previous" as explained in this exerpt from the WordPress website:
more »