{"id":10610,"date":"2015-07-05T11:00:31","date_gmt":"2015-07-05T16:00:31","guid":{"rendered":"https:\/\/getflywheel.com\/?p=10610"},"modified":"2024-10-02T14:17:30","modified_gmt":"2024-10-02T19:17:30","slug":"create-custom-shortcode-in-wordpress","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/","title":{"rendered":"How to Create your own Custom Shortcode in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes, no matter how cool your theme or how incredible the visual editing plugin you are using is, there is a feature you want that they just don\u2019t have. And the easiest way to add some really cool functionality into your site is with a shortcode.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today we&#8217;re going to walk you through an easy way to add shortcodes to your site. It doesn\u2019t involve editing your theme or functions.php file. Why? Because that just isn\u2019t portable enough. &nbsp;What if you write your shortcode and it\u2019s amazing, and you want to use it on other sites? &nbsp;Or sell it to make millions of dollars?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The easy way to create portable code for use in WordPress is by creating&nbsp;your very own plugin. So today, you are going to learn how to write your own plugin that lets you create your own custom shortcode in WordPress.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the code you&#8217;ll need:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&amp;lt;?php\n\n\/*\n\n* Plugin Name: Your Plugin Name Here\n\n* Description: Tell Us What Your Shortcode Does\n\n* Version: 1.0\n\n* Author: Your Name\n\n* Author URI: Your URL<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That was easy, right? Let\u2019s break it down.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That first block that is all commented out is what WordPress uses to display shortcode information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Obviously, you are going to want to change all of those variables, except maybe starting at version 1.0. Then, there is the shortcode addition itself. This happens in two steps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Create your function<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function any_name_here(){\n\n\/\/ All your cool code here\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You\u2019ll want to pick a name for it. If this is going to be a \u2018single shortcode\u2019 plugin, maybe you want to name it the same as the plugin file. But, you could add hundreds of shortcodes to one plugin if you wanted, in which case you\u2019d just want to name it intuitively. What you name it really doesn\u2019t matter as it will only be referenced internally by the next bit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Allow the function to be executed<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_shortcode(\u2018the_shortcode_text\u2019, \u2018any_name_here\u2019);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That line says to execute the function we just wrote <code>any_name_here<\/code> when we use the shortcode that looks like this <code>[the_shortcode_text]<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Or, if we go back to the original full plugin code above, if you put <code>[my_shortcode]<\/code> into a page like this:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2015\/06\/shortcode2.jpg\" alt=\"Screenshot of Add a New Page in WordPress. The page is titled My Shortcode and in the visual editor, the author has written [my_shortcode]\" class=\"wp-image-10613\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">and you then view the page, you\u2019ll see the plugin in action:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2015\/06\/shortcode3.jpg\" alt=\"screenshot that reads My Shortcode Yippee! I did it! I am a shortcode creating genius!\" class=\"wp-image-10614\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And this plugin <em>actually<\/em> works. To install it, it should be in a folder. Create a folder called <code>my_shortcode<\/code> and then save the following&nbsp;code into a file called <code>my_shortcode.php<\/code>. Put the folder into a zip archive and upload it to a WordPress site using the default \u2018Add a Plugin\u2019 upload feature. Here&#8217;s what you&#8217;ll see:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2015\/06\/shortcode1.jpg\" alt=\"Screenshot reads Your Plugin Name Here next to a clickable box. Beneath the text are three clickable buttons titled Activate, Edit, and Delete. On the righthand side, more text reads Tell Us What Your Shortcode Does, Version 1.0 By Your Name\" class=\"wp-image-10612\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Beyond that, your imagination is the limit!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For some inspiration, check out <a href=\"http:\/\/wpsnipp.com\/index.php\/category\/shortcode\/\" target=\"_blank\" rel=\"noreferrer noopener\">this list of code snippets<\/a> you can instantly use to get started. And to ensure all your code as performant as possible, make sure you&#8217;re using WP Engine <a href=\"https:\/\/wpengine.com\/wordpress-hosting\/\" target=\"_blank\" rel=\"noreferrer noopener\">host your WordPress sites<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, no matter how cool your theme or how incredible the visual editing plugin you are using is, there is a feature you want that they just don\u2019t have. And the easiest way to add some really cool functionality into your site is with a shortcode. Today we&#8217;re going to walk you through an easy<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":140979,"template":"","resource-topic":[901],"resource-role":[896],"resource-type":[916],"class_list":["post-10610","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to create your own custom shortcode in WordPress<\/title>\n<meta name=\"description\" content=\"The easiest way to add functionality to your site is with a shortcode. Use this guide to create your own custom shortcode in WordPress.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create your own custom shortcode in WordPress\" \/>\n<meta property=\"og:description\" content=\"The easiest way to add functionality to your site is with a shortcode. Use this guide to create your own custom shortcode in WordPress.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-02T19:17:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/short-code-header.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/\",\"name\":\"How to create your own custom shortcode in WordPress\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2015-07-05T16:00:31+00:00\",\"dateModified\":\"2024-10-02T19:17:30+00:00\",\"description\":\"The easiest way to add functionality to your site is with a shortcode. Use this guide to create your own custom shortcode in WordPress.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Create your own Custom Shortcode in WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create your own custom shortcode in WordPress","description":"The easiest way to add functionality to your site is with a shortcode. Use this guide to create your own custom shortcode in WordPress.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"How to create your own custom shortcode in WordPress","og_description":"The easiest way to add functionality to your site is with a shortcode. Use this guide to create your own custom shortcode in WordPress.","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2024-10-02T19:17:30+00:00","og_image":[{"width":1100,"height":500,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/short-code-header.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/","name":"How to create your own custom shortcode in WordPress","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2015-07-05T16:00:31+00:00","dateModified":"2024-10-02T19:17:30+00:00","description":"The easiest way to add functionality to your site is with a shortcode. Use this guide to create your own custom shortcode in WordPress.","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/create-custom-shortcode-in-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"How to Create your own Custom Shortcode in WordPress"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/short-code-grid.png","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Developer","topic":"<strong>Topics:<\/strong> WordPress","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/10610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/140979"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=10610"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=10610"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=10610"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=10610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}