X

How to Add Shortcodes in WordPress

Snippets by IsItWP

Are you looking for a way to implement php-functions in your content? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add shortcodes in WordPress.

There are three different kinds of shortcodes:

  • [myshortcode] – Simple shortcode.
  • [myshortcode id="5"] – Shortcode with parameter.
  • [myshortcode]Hello World![/myshortcode] – Shortcode goes BB Code.

Shortcodes are a great way to use php-functions in your posts and pages. You can also use it for styling your content, for example Bold text.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

// Simple Shortcode
# Adds a shortcode called 'hello'.
function helloworld() {
  return 'Hello World!';
}
add_shortcode('hello', 'helloworld');  

// Shortcode with parameter
# Adds a shortcode that allows parameters. #
function myname($name) {
 extract(shortcode_atts(array(
  	'name' => 'name'
  	), $name));
  return 'My name is' . $name;
}
add_shortcode('user', 'myname');
# Example: [user name="Filip"] # 

// BB Code style
function font_bold( $attr, $content = null ) {
  return '<span style="font-weight: bold">' . $content . '</span>';
}
add_shortcode('bold', 'font_bold'); 

// BB Code with parameters
function colorpick( $color, $content = null ) {
  extract(shortcode_atts(array(
  	'color' => 'color'
  	), $color));
  return '<span style="color: ' . $color . '">' . $content . '</span>';
}
add_shortcode('font', 'colorpick'); 

For a very simple link shortcode, use this snippet. Placing [mysite] within your post will be replaced by the hyperlink within the mysite function on line 2. Place this code within the functions.php of your WordPress theme or in a site-specific plugin:

function mysite(){
	return '<a href="http://mysite.com">visit my website</a>';
}
add_shortcode('mysite', 'mysite');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 best WordPress accordion plugins and how to set up download tracking in WordPress with Google Analytics.

Comments   Leave a Reply

  1. Marcel Rodriguez October 11, 2021 at 8:02 pm

    iss possible two parameteers ?

    1. Yes, you can do this by passing more array items to the shortcode_atts function.

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!