ilDavid.com

Shortcode and custom quicktag

Postato il 09 settembre 2010

I was developing a WordPress custom theme for a client and I came across a small problem: the articles should have an introductory text other than the abstract (basic functionality of WP). In practice it was to include some text in a <div> with a specific class, and to avoid the customer having to remember every time the correct syntax, I created a shortcode and a quicktag button on the bar of the text editor to allow him to do everything with a click.
I recalled a post on the use of shortcode in WPrecipes, and so I went looking for that article, but then I stumbled on the problem of adding the quicktag button to the toolbar: all the results that I found was to edit a file of the basic installation of WordPress, and this would mean that an update of the platform would lose my changes. Until I found this post which explains how to add a button without changing the original file, with the creation of a plugin. But I prefer to have a theme related function, so that, once the theme is activated, everything would be available.
So here is the code to get all this:

function intro_shortcode( $atts, $content = null ) {
   return '<div class="introtext">' . $content . '</div>';
}
add_shortcode('intro', 'intro_shortcode');

add_action('admin_print_scripts', 'my_custom_quicktags');
function my_custom_quicktags() {
	wp_enqueue_script(
		'my_custom_quicktags',
		get_bloginfo( 'template_url' ).'/js/custom-quicktags.js',
		array('quicktags')
	);
}

in the functions.php and the JavaScript is:

edButtons[edButtons.length] =
new edButton('ed_block'
,'intro'
,'[intro]'
,'[/intro]'
,''
);

to put in template_directory/js/custom-quicktags.js

Tags:

Lascia un Commento