I have seen many professional wordpress themes for sale that have shortcodes built in with which you could easily make posts like this:

[alert_box]This is an alert box which based on this short-code automatically
switches the CSS so it is easier than messing with classes every time[/alert_box]

How can I do this?

I know that it requires you to add something to the functions.php, though I haven't been able to figure out how.

Thank you, any help is appreciated.

  • In general, ask the plugin developer codex. Yes, this is nice for RTFM ;) Also, you can look into an arbitrary plugin that adds a shortcode, but then you risk copying bad style. – Raphael Nov 9 '10 at 20:24

See the Shortcode API article on the Codex for a tutorial and my Shortcode Plugin for some examples.

A very basic example for the functions.php:

 * Shortcode for bloginfo()
 * Usage: [bloginfo key="template_url"]
 * @see http://codex.wordpress.org/Template_Tags/get_bloginfo
 * @source http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress
 * @param array $atts
 * @return string
function bloginfo_shortcode($atts)
    extract( shortcode_atts( array( 'key' => 'name' ), $atts ) );
    return get_bloginfo( $key );
add_shortcode( 'bloginfo', 'bloginfo_shortcode' );
|improve this answer|||||
  • Great example, shows how to pass attributes too. +1 – MikeSchinkel Nov 9 '10 at 6:55
  • Thank you very much, this is exactly what I was looking for. – Logan Nov 10 '10 at 20:43

For some stimulus to your example above...have the shortcode wrap the contents in a div or paragraph or blockquote or whatever wrapper you want, with some classes defined with it. That way they inherit the styling rules for those classes from the stylesheet. You could have .alert or .safe etc all defined already and then which type of notification you need determined the one used ;)

EDIT: Here's what I was thinking with my answer above, and perhaps someone else could use it themselves. For the example I'll use Alert, notice, and announcement

function announce_shortcode($atts, $content = null) { extract( shortcode_atts( array( 'type' => 'alert' ), $atts ) ); return ''.$content.'

'; } add_shortcode( 'announce', 'announce_shortcode' );

Then used like so [announce type="alert"]This is an alert![/announce] shown in red or however you wish to style. [announce type="notice"]This is just a notice[/announce] styled with milder colors than the alert, etc. Have the class names for alert and notice and whatnot defined in the css.

This was the need that I got out of reading the OP.

|improve this answer|||||
  • Much more useful to actually provide an example than to reference another example and speak about what you'd do differently in vague terms. Please provide an actual code snippet demonstrating how to "wrap the contents in a div or paragraph or blockquote or whatever..." – EAMann Nov 9 '10 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.