How to Create Your Own WordPress ShortCodes

Shortcodes were introduced in WordPress version 2.5. It also comes bundled with many plugins and themes. Shortcodes are something inside square brackets which is replaced with content in post and page. In WordPress based websites, you are not allowed to add PHP codes in posts and pages. But you can create custom functions and then shortcodes of those functions to run within the function. If you are new to WordPress and do not know how to create shortcodes for your function, you are at right place. In this post, we will see how to create shortcodes for a custom function.

WordPres Shortode

Before creating shortcode, you need to create a call back function. This function will be called each time the shortcode will encounter in post or page.. Open functions.php file of your theme and add the sample code. mycustomFunction is the call back function for shortcode Myfn.

Whenever you want to add the mycustomFunction within post or page, just add [Myfn] shortcode.

Sometimes, there is the need of passing values in the function. So, we have to create shortcodes that can pass value. See the example given below:

You can use this shortcode by passing values of height and width.

Try it by yourself and comment below if you are facing any problem.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest updates, web design resources and tools.

You have Successfully Subscribed!

About The Author

  • ADD YOUR COMMENT

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Receive fresh web design resources and SEO tips in your email by our weekly newsletter

You have Successfully Subscribed!

Pin It on Pinterest

Share This

Share This

Share this post with your friends!