WordPress attachments hack

Date: 9th June 2009 at 3:19 pm | Filed under: blog, development, scripts, wordpress | Author: Sam Burdge | Tags: , , , , , ,

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!

7 Responses to “WordPress attachments hack”

  • Comment by akshay
    Date: June 14th, 2009 at 3:27 pm

    really good post….tutorial is very helpful for me …thanks a lot for giving out such good tutorials

  • Comment by Alex Holsgrove
    Date: June 29th, 2009 at 8:54 am

    This was a great help. Thanks for sharing the code

  • Comment by Abeon
    Date: July 13th, 2009 at 10:26 pm

    This is a great little hack :)
    Simple but effective!

  • Comment by Nate
    Date: August 12th, 2009 at 1:21 pm

    I can’t wait to try this out.

  • Comment by Betaclick SEO Company
    Date: September 17th, 2009 at 7:32 pm

    Thanks for the code dude, i need to try this out on my wordpress blog! As im learning advanced use of it.

  • Comment by Webbando
    Date: April 12th, 2010 at 10:51 am

    Hi Sam. Thanks for your post, really useful. But I would have another thing:
    I want change permalink of attachment file from

    http://site.ext/postname/postname-post_id.html/attachment-name

    to

    http://site.ext/photos/attachment-name

    Is it possible?

    Thanks a lot for your support

  • Comment by William Lindley
    Date: March 2nd, 2011 at 1:43 am

    You might also try the AutoNav plugin, which will automatically generate thumbnails and links to child pages, to posts in a category or by slug or author, or by page/post ID:

    http://wordpress.org/extend/plugins/autonav/

Leave a Comment

WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages

Date: 29th January 2009 at 11:47 pm | Filed under: development, scripts, wordpress | Author: Sam Burdge | Tags: , , ,

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.

27 Responses to “WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages”

  • Comment by pressitfor.me
    Date: January 29th, 2009 at 11:54 pm

    WordPress Tips - Removing The Title Attribute From wp_list_categories & wp_list_pages | Sam Burdge…

    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 lit…

  • Comment by tom
    Date: February 10th, 2009 at 3:49 pm

    Where would I place the code snippet for wp_list_pages?

  • Comment by Sam Burdge
    Date: February 10th, 2009 at 4:56 pm

    Hi Tom

    The code snippet above is meant to replace the existing wp_list_pages function used in your theme.

    wp_list_pages is usually called in either header.php, footer.php or sidebar.php of your theme.

    More info about wp_list_pages here:
    http://codex.wordpress.org/Template_Tags/wp_list_pages

    Sam

  • Comment by Sumit Khanna
    Date: April 1st, 2009 at 3:39 pm

    Thanks for the code snippit! I had started using nicetitles for http://chattanoogaswing.org and then noticed that wp_list_pages added tiles to all the links!

    The calendar I was using doesn’t add a class for the dates I wanted the tooltips on. If it did, I could just modify the nicetitles javascript to only select that class of element.

    Using your little regex to remove the titles form wp_list_pages is much easier!

    Also, cool theme you’ve got going on here. I like the green-on-black console look.

  • Comment by Designs Genius
    Date: May 19th, 2009 at 10:15 pm

    thanx a lot for this tip !.
    I have another question for everybody : How can I remove a hyperlink from wp_list_pages, but only from the first ul in the heirachy ?
    thx for all who will consider my question ! :)

    Sébastien from France

  • Comment by Frank Karlstrøm
    Date: June 17th, 2009 at 8:30 pm

    Yet another thank you from me. Have been looking for a plugin to do this, but this is even better. Do you think it’s better for me to write a private plugin implementing this small little code? to avoid the snippet to be removed when I upgrade WP.

  • Comment by Robert Carro
    Date: July 6th, 2009 at 5:41 pm

    W9xinK great tips. I enjoyed reading this,

  • Comment by coda
    Date: July 29th, 2009 at 11:49 pm

    Exactly what I was looking for, thanks a lot!

  • Comment by norton
    Date: September 10th, 2009 at 2:59 pm

    Thanks a lot . I needed to remove the “title-attribute” because of a firefox(macOs)-bug that would always reload the dropdown after stoping to move the mouse on it.
    This would not happen if there was no “title-attribute” anymore. To figure out this already took forever. This code finally make my dropdowns work like they should.

  • Comment by JR
    Date: September 11th, 2009 at 2:49 pm

    What happens if it isn’t hard coded into the theme and WP seems to be generating them of its own accord? What file do I need to edit?

  • Comment by Matt Furtado
    Date: September 16th, 2009 at 8:16 pm

    For the wp_list_categories hack, how would I get the category name to display instead of “View all posts filed under “? Is there a simple hack that I’m not aware of?

  • Comment by Matt Brewster
    Date: September 18th, 2009 at 1:53 pm

    Great tip - many thanks :)

  • Comment by Will Stern
    Date: October 8th, 2009 at 4:19 pm

    You rock. Great post.
    To answer JR, and anybody else wondering, open your header.php and look for replace it with his code.
    If there are specifics in your brackets, make sure to include them in his code in addition to echo=0
    Mine looks like this: $clean_page_list = wp_list_pages(’title_li=&depth=2&echo=0′);

  • Comment by Will Stern
    Date: October 8th, 2009 at 4:20 pm

    meant to say look for the php calling wp_list_pages() …the php code got cut out. :(

  • Comment by fatihturan
    Date: November 29th, 2009 at 1:08 pm

    Good tip. Thanks.

  • Comment by Tim Holt
    Date: January 4th, 2010 at 4:03 pm

    Those who aren’t comfortable editing their theme files can use the Remove Title Attributes plugin to solve this problem.

  • Comment by Tom
    Date: January 31st, 2010 at 8:07 pm

    Tasty… RegExp is not my bag!

  • Comment by kevin
    Date: February 23rd, 2010 at 9:21 pm

    Cool exactly what i needed. Thanks!

  • Comment by Khaled Hakim
    Date: June 25th, 2010 at 5:50 pm

    Thank you for this awesome snippet of code. One of my picky clients did not want the titles to appear on hover. I was using the wp_listpages function, of course and I am glad I came across this site.

    What this code does, for those who are not sure, is remove the title that appears when the mouse is over (hover) an item in the list that is generated through the use of the wp_listpages function.

    Cheers.

  • Comment by Vidhu
    Date: July 12th, 2010 at 4:43 am

    Wow… This is what i was looking for… Now its working finely in my website… Thanks for the useful tip…

  • Comment by Nathan
    Date: July 28th, 2010 at 12:42 pm

    I used this code and it removed the titles, but it added the text “Pages” before all of the actual pages and it is messing up my menu, how can I get rid of that text?

  • Comment by Sam
    Date: November 13th, 2010 at 11:42 pm

    Guys can anyone put little bit more information here please!!!

    where in the world we should put that piece of code?

  • Comment by althahabi
    Date: December 15th, 2010 at 9:59 pm

    sorry , but where to find the two functions wp_list_categories
    wp_list_pages

    in the theme twentyten !

    thanx

  • Comment by alex chousmith
    Date: January 27th, 2011 at 3:52 pm

    thanks for the tip, it totally works

  • Comment by Ian
    Date: March 14th, 2011 at 6:25 pm

    Found this via Google leading me to the WP forums, works great, better than editing core, thanks!

  • Comment by Ian
    Date: March 14th, 2011 at 6:27 pm

    @ Nathan, try this:

    wp_list_pages(’echo=0&title_li=’);

    note the title_li=

    that makes the list title equal to nothing! Cheers

  • Comment by marion
    Date: March 22nd, 2011 at 3:21 pm

    Thank you, this is perfect! Your solution worked for my categories and custom taxonomies. I wish you can also write a code to remove the parent category links =D

Leave a Comment

WordPress - Change Default Email Address Plugin

Date: 20th February 2008 at 12:04 am | Filed under: development, plugins, wordpress | Author: Sam Burdge

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:


40 Responses to “WordPress - Change Default Email Address Plugin”

  • Comment by Shane
    Date: February 29th, 2008 at 8:33 am

    Works great - thank you!

  • Comment by Change Default Email Address » Wordpress Plugins
    Date: July 9th, 2008 at 6:56 am

    [...] Version: 0.1 - License: n/a - Author: Sam Burge - Plugin Page - » Download [...]

  • Comment by Shane
    Date: August 25th, 2008 at 10:27 pm

    Is there a newer version than 0.1? I upgraded to WordPress 2.6.1 and the Plugins area is telling me that there is a new version, but the new version links to:
    GALLERY LIGHTBOX PLUGIN
    http://wordpress.org/extend/plugins/wp-25-gallery-lightbox-plugin/

    Also, just an FYI, version 0.1 now conflicts with the Subscribe2 plugin.

  • Comment by Greg
    Date: September 17th, 2008 at 8:06 pm

    Very handy little plug-in, works like a charm, thanks.

    I’m experiencing a little weirdness, though. I downloaded it from your site. When I install it, it works fine but I get an alert telling me there’s a new version 1.3 (the code says “Version 0.1″) and the link to get the new version points at http://wordpress.org/extend/plugins/wp-25-gallery-lightbox-plugin/ which is clearly wrong. The “Upgrade automatically” option doesn’t work either, with the error mentioning wp-25-gallery.

    Since it works, the only reason I care is I don’t want the little orange flag over “Plugins” in my dashboard, but seems like something you’d want to know.

  • Comment by huski
    Date: October 23rd, 2008 at 10:19 am

    wordpress 2.5
    not working for me, i made the two changes , name and email to the php file, verified the changes were saved in the php file, but it continues to send wordpress and wordpress@domain

    ????

  • Comment by m1k3y
    Date: February 9th, 2009 at 11:06 pm

    Great Job!
    THX !!!

  • Comment by marco
    Date: February 16th, 2009 at 5:08 am

    Hi

    IS the email address restricted to the domain of the website? OR can any email address be used, say gmail account?

  • Comment by marco
    Date: February 21st, 2009 at 11:50 pm

    Hi

    This appears like it will do the (magik) trick but I have an unusual situation in which the email address my client wants for default does not use the website’s domain name in it. So simply setting the name in front of the @ symbol is not sufficient - and neither is my confidence in editing your php to accomodate it. Could write up a note on how this customization in the php file would go?
    Sorry for the xtra trouble. Really appreciate your efforts with this as you can imagine.

    /marco

  • Comment by Açıklamalı 200 Wordpress Eklentisi (Part2) | Paylaşım Keyfi
    Date: March 1st, 2009 at 7:25 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Beth
    Date: August 3rd, 2009 at 6:40 pm

    I had no idea this was even an issue. I thought that it would just show up with my email by default. That’s dumb.

  • Comment by WordPress için tam 400 eklenti birsayfada! | PiyanoDegil.org :)
    Date: September 2nd, 2009 at 2:59 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Marcus Naylor
    Date: September 8th, 2009 at 3:35 pm

    Made the changes in the plugin editor, added my From Name and the from Address, updated changes and nothing happened. Still coming from Wordpress

  • Comment by Açıklamalı 200 Adet Wordpress Eklenti Arşivi 2 | Akgün-Web Ailesi
    Date: September 18th, 2009 at 5:29 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Como mudar endereço email do Wordpress
    Date: October 30th, 2009 at 10:49 am

    [...] ou pode fazer Download do plugin AQUI [...]

  • Comment by Adam
    Date: November 3rd, 2009 at 5:05 pm

    This is a life saver. We were testing the site and discovered the “wordpress@”. We knew we didn’t want that. Wordpress really need to allow users to change this from the dashboard. But anyway, this works perfectly and I wanted to thank you Sam for sharing your expertise with us.

  • Comment by Harry
    Date: November 6th, 2009 at 10:40 am

    Thanks for the plugin.

  • Comment by Tony
    Date: November 14th, 2009 at 1:27 pm

    Works Great!!!!! Thanks!!!

  • Comment by Rhino
    Date: November 21st, 2009 at 12:36 pm

    Hi,
    Thanks for your great plugin. I were used it in my blog, it run so cool. But i have a problem with my language. I use Vietnamese language and the email appear: [Tranh biếm hỠa Việt Nam] Có thà nh viên mới đăng kí

    Can you help me this problem?
    Thanks!

  • Comment by Wordpress 400 Eklenti « www.hakanersoy.org
    Date: January 2nd, 2010 at 12:53 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Norguad
    Date: January 8th, 2010 at 7:04 pm

    Hi, I used the plugin, but it turns all non-latin characters into weird characeters (for example: é is é or ř is Å™). Do you know how to solve it?

    Thank you very much.

  • Comment by Heath
    Date: March 9th, 2010 at 2:32 am

    Great
    thanhk you sam

  • Comment by Peter F.
    Date: March 9th, 2010 at 3:53 am

    Awesome plugin, guys! Works like a charm, and solves a problem I was afraid would be very tedious.

  • Comment by sol97one
    Date: March 10th, 2010 at 7:03 am

    Thanks a lot ! Very useful !

  • Comment by Terje
    Date: March 15th, 2010 at 12:51 pm

    Same as the others, i have problem with some signs. I try to write the norwegian letter “ø/ø” (ascii ø), but i cant get it to work

  • Comment by frank
    Date: March 21st, 2010 at 12:08 pm

    Hi there, this is a very helpful plugin, I have the same problems with the charset or something else. After installing the plugin I always receive weird characters in my mails. ÖÄÜ are not transformed properly. Thanks. F

  • Comment by Mike
    Date: April 5th, 2010 at 2:03 pm

    Great, thanks for the plugin…If you all are looking for a less extensible…but easier way to modify the email “From” line in wordpress, it’s on line 352 in pluggable.php. Just change [code]$from_name = ‘WordPress’;[/code] to something like…[code]$from_name = ‘Mike’;[/code]

  • Comment by Joshua
    Date: April 5th, 2010 at 11:52 pm

    I am using WP MU 2.9.2. I have installed the plugin and activated successfully. Yet I dont see any settings to configure !!!??

  • Comment by me2t
    Date: April 18th, 2010 at 2:02 pm

    i love this plug in very much.thanx

  • Comment by bosschan
    Date: May 9th, 2010 at 2:16 pm

    give thanx!

  • Comment by Jim
    Date: May 20th, 2010 at 2:10 pm

    Hey, thanks for this plugin, helped me sort out a really annoying reg problem I had :D

  • Comment by PLnet
    Date: May 25th, 2010 at 3:32 pm

    thank y

  • Comment by Greg
    Date: June 27th, 2010 at 2:25 am

    Great plug-in, just what I was looking for as I didn’t want any emails say “wordpress”

  • Comment by Thiago Dias
    Date: August 12th, 2010 at 3:45 am

    thanx!

  • Comment by TomCap
    Date: September 6th, 2010 at 1:56 am

    Worked like a charm. Another little irritating item solved. Thanks!

  • Comment by Maira
    Date: January 21st, 2011 at 12:59 am

    worked. highly recommend. thank you!

  • Comment by oriflon
    Date: February 17th, 2011 at 11:49 pm

    how to edit manual?

  • Comment by SteveB
    Date: February 18th, 2011 at 12:20 am

    I loaded the plug in but can’t find it in the plugin list.

  • Comment by haohuu_lam_em
    Date: March 3rd, 2011 at 10:04 am

    Can not convert to UTF-8!

  • Comment by Demo Games
    Date: April 8th, 2011 at 9:20 am

    Thanks for sharing works fine on wordpress 3.1.1

  • Comment by I Shoot Reno
    Date: April 19th, 2011 at 9:23 pm

    Fantastic solution! Minimal effort needed to make this work from a novice’s standpoint!

Leave a Comment

Al Fingers Website

Date: 20th November 2007 at 3:28 pm | Filed under: portfolio | Author: Sam Burdge

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

Leave a Comment

wordpress post navigation hacks

Date: 24th October 2007 at 2:34 pm | Filed under: development, scripts, wordpress | Author: Sam Burdge

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:
Read on…

5 Responses to “wordpress post navigation hacks”

  • Comment by gus
    Date: October 30th, 2008 at 1:18 pm

    Thanks for this interesting technique. I finally realized, though, that this works only for “paged” templates, like the index and categories and tag pages. Can you suggest a technique to use on single-post templates?

    I understand that some of this functionality is built in to the next_post_link and previous_post_link tags already, but it is still difficult to make one of them go away if you are using them with nested divs or other complicated bits of CSS — because you don’t have direct access to the anchor tag. Any suggestions for crafting an equivalent “if” statement that will make the previous or next link not show when not needed on a single-post page?

    Also, how much of a performance hit will the $wp_query call cause in the above code?

  • Comment by Sam Burdge
    Date: October 30th, 2008 at 11:34 pm

    Hi Gus

    With the next_post_link / previous_post_link functions you should use the ‘format’ parameter to keep your divs from displaying when the link is not needed. For example:


    <div id="post_nav">
    < ?php next_post_link('%link', 'Next Post'); ?>
    </div>

    will still display the div. While:


    < ?php
    $post_link_format='<div id="post_nav">%link</div>’;
    next_post_link($post_link_format, ‘Next Post’); ?>

    would only show the div when the link is needed.

  • Comment by Philip Arthur Moore
    Date: December 1st, 2008 at 12:17 pm

    Excellent writeup. Another suggestion: if you’re looking for a quick way to fade out elements that are disabled, consider adding a class to your disabled links (i.e. “.disabled”) and then controlling the functionality with jQuery (i.e. jQuery(”a.disabled”).fadeTo(”fast”, .2).removeAttr(”href”);). Thanks a ton.

  • Comment by Rumait
    Date: August 5th, 2009 at 1:51 pm

    where these changes will be made, i mean in which file, cofig.php or single post.php etc

  • Comment by sagive
    Date: August 28th, 2010 at 10:07 am

    do you have a solution that shows page number
    offering a direct navigation to the fifth, sixth page?

Leave a Comment