How To Filter caption-Shortcode in WordPress


Sponsored Links






WordPress 2.5 and up added new featured that called wp-caption, this is nice custom, you can add extra text on your image post, beside that tricks you can explore more with this featured. My bigg problem is the default wp-caption is added extra inline style. This inline style added extra padding on the left and right on your wp-caption. If you wp-caption style is using standard method of wordpress, you didn’t need this tricks.

But if you theme design need another style, like disable padding on your wp-caption, you’ll need it. Let’s explore more details.

On this wp tricks, we will modified default function on wp-caption and hook it with this modified caption. All need to do, is open functions.php on your theme and write this code

add_shortcode('wp_caption', 'fixed_img_caption_shortcode');
add_shortcode('caption', 'fixed_img_caption_shortcode');
function fixed_img_caption_shortcode($attr, $content = null) {
	// Allow plugins/themes to override the default caption template.
	$output = apply_filters('img_caption_shortcode', '', $attr, $content);
	if ( $output != '' ) return $output;
	extract(shortcode_atts(array(
		'id'=> '',
		'align'	=> 'alignnone',
		'width'	=> '',
		'caption' => ''), $attr));
	if ( 1 > (int) $width || empty($caption) )
	return $content;
	if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
	return '<div ' . $id . 'class="wp-caption ' . esc_attr($align)
	. '" style="width: ' . ((int) $width) . 'px">'
	. do_shortcode( $content ) . '<p class="wp-caption-text">'
	. $caption . '</p></div>';
}

And done, you can check it on your theme and the inline style is gone ;) This tricks I got it from WordPress.org forum, WP Engineer show different method. Do you have another solutions to do this? Share to me

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

How To Create a PDF viewer shortcode in WordPress

Did you know, we can create PDF viewer shorcode in WordPress with easy steps? All need to do only using Google Docs viewer tools and integrated it into our WordPress site. The first thing we should do, is written shortcode … Read More

Detect Category ID using the Category Slug in WordPress

Sometime WordPress Developer need to know Category ID, I will show you how to find Category ID using the Category Slug in WordPress, this tricks is original by Bavotasan and I rewrite it. WordPress built in with nicely functions get_term_by(), … Read More

Added Permalinks Shortcode on WordPress

WordPress is: Themes, Plugins, Widget, Shortcode, PHP and GPL Licensed. Did you know that, beside Widgets, Another useful WordPress API is shortcode, and now I will show you how to added permalinks shortcode into our WordPress site. Why this shortcod

How To Integrate Images Shortcode with TimTumb

Every WordPress Developer has seen that TimTumb is wonderful script, This script is better handle to resize image on the fly and display it what ever size we want and we need. Let's me show you How To Integrate Images … Read More

Enable Shortcode in Widget

There are simple ways to Enable Shortcode in Widget Area. Using WordPress, there are almost unlimited possibilities, one nice featured that you should learn and uses is Shortcode. With WordPress shortcode, WordPress content writer will be more easier

The Right Way to Change The Default Gravatar in WordPress

I will show you How to Change the Default Gravatar in WordPress, why? Because one of my favourite WordPress Comment enhancement is added Gravatar, after Gravatar become Automattic Family, Gravatar become more and more better. Especially integrated wi

Filter caption-Shortcode in WordPress - WP Engineer

Filter caption-Shortcode in WordPress. March 20th, 2009 by Frank • WordPress Hacks • 11 Comments. The title may not speak for itself and therefore I will explain read more

Source: http://wpengineer.com/917/filter-caption-shortcode-in-wordpress/

How To Filter caption-Shortcode in WordPress / WordPress Tricks & Tips

WordPress 2.5 and up added new featured that called wp-caption, this is nice custom, you can add extra text on your image post, beside that tricks you can explore read more

Source: http://wptricks.net/how-to-filter-caption-shortcode-in-wordpress/

Function Reference/add filter « WordPress Codex

Hooks a function to a specific filter action. Filters are the hooks that WordPress launches to The filter img_caption_shortcode is applied in media.php using the read more

Source: http://codex.wordpress.org/Function_Reference/add_filter

img_caption_shortcode Wordpress hook details -- Adam Brown, BYU

Detailed information about every action hook and filter used in WordPress. Makes Plugin API easier to use. Lists appearance, file location, and deprecation data for read more

Source: http://adambrown.info/p/wp_hooks/hook/img_caption_shortcode

WordPress › Support » Add new caption shortcode attribute

The * filter is 'img_caption_shortcode' and passes an empty string, the attr * parameter and Latest reply from jimisdon; This topic is resolved; WordPress version: 2.8 read more

Source: http://wordpress.org/support/topic/add-new-caption-shortcode-attribute


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>