• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » Snippets » WordPress Snippets » Automatically Add Attributes to All Images in WordPress Posts

Automatically Add Attributes to All Images in WordPress Posts

Posted on December 8, 2011 by Renji | Short URL: http://sumtips.com/?p=6022

The below snippet will allow you to add an attribute to all images embedded in a WordPress post.

function sumtips_image_attribute($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 attribute="attribute_value"';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}
add_filter('the_content', 'sumtips_image_attribute');

Replace attribute="attribute_value" to whatever attribute you need.

Example:

function sumtips_image_attribute($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 rel="lightbox"';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}
add_filter('the_content', 'sumtips_image_attribute');

If you want to use the post title as the title attribute of the images anchor tag, replace line 4 in the above code with the following:

$replacement = '<a$1href=$2$3.$4$5 attribute="attribute_value" title="'.$post->post_title.'"$6>';

 


Tweet

Related posts:

  • Add Signature to WordPress Posts without Installing a Plugin
  • Customize and Add Google +1 Button to WordPress, Blogger and other Websites
  • Extract Images from PDF Files or Convert PDF Pages to Images
  • Exclude Specific Post, Category and Author from WordPress Feed
  • Open, Edit and Create WebP Images in Windows
Categories: WordPress Snippets
Search and Delete Unused Post Tags in WordPress
Add Pages, Custom Post Types in WordPress Site Feed

  • Get Updates via Email

  • Recent Posts

    • Gmail for Android: Delete Message Instead of Archive from Notification
    • AlarMe: A Weather-based Alarm Clock for Android Devices
    • Hide any Term, Phrase, Person, or Hashtag from Twitter & Facebook Feed
    • How to Enable or Disable Tabs in Gmail Inbox
    • Install Google Chrome Extensions in Opera directly from Web Store
    • List of Web Services Supporting Two-Factor Authentication
  • Random Posts

    • Windows Phone 7 Mango and iOS Skin Packs for Windows 7
    • Second Generation of Credit Cards Are Here
    • Friendeye : Portable App to Manually Adjust Screen Brightness
    • Android 2.3 Gingerbread Features [Video Included]
    • Add Option to Hide/Show Comments on Google+ Posts with +Comment Toggle
    • Automatically Switch Windows Theme When on Laptop Battery
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy