{"id":2627,"date":"2020-11-28T02:27:10","date_gmt":"2020-11-28T02:27:10","guid":{"rendered":"https:\/\/tschaki.com\/?p=2627"},"modified":"2021-11-20T14:42:32","modified_gmt":"2021-11-20T14:42:32","slug":"new-shortcode-for-wordpress-theme-and-plugin","status":"publish","type":"post","link":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/","title":{"rendered":"Create shortcode in PHP for WordPress"},"content":{"rendered":"<p>To extend your WordPress theme or plugin with a brand new shortcode, simple steps are necessary.<\/p>\n\n\n\n<h2 id=\"h-create-simple-shortcode\">Create simple shortcode<\/h2>\n\n\n\n<p>The following code is placed in the <code>functions.php<\/code> file in the theme or in the plugin file<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">function dynamic_content_shortcode( ) {\n    $content = &quot;Inhalt&quot;;\n    return $content;\n}\nadd_shortcode( 'dynamic_content', 'dynamic_content_shortcode' );<\/pre><\/div>\n\n\n\n<p>With these five lines of code a shortcode is already registered in WordPress and can be used. <code>[dynamic_content]<\/code> as shortcode would output the word &#8220;content&#8221; in this example. Here it would also be possible to add a complex function and thus generate dynamic content.<\/p>\n\n\n\n<h2 id=\"h-create-shortcode-with-attributes\">Create shortcode with attributes<\/h2>\n\n\n\n<p>To create a new shortcode with a little more dynamic, you can also add various attributes to the shrotcode, which were previously defined in <code>functions.php<\/code> or the plugin file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">function dynamic_content_shortcode( $atts ) {\n    $atts = shortcode_atts(\n        array(\n            'attribute_1' =&gt; 'predefined_1',\n          \t'attribute_2' =&gt; 'predefined_2',\n         \t'attribute_3' =&gt; 'predefined_3'\n        ), $atts, 'product-overview' );\n  \t\n  \t$content = $atts['attribute_1'];\n  \t$content .= $atts['attribute_2'];\n  \t$content .= $atts['attribute_3'];\n  \treturn $content;\n}\nadd_shortcode( 'dynamic_content', 'dynamic_content_shortcode' );<\/pre><\/div>\n\n\n\n<p><code>$atts<\/code> is predefined with an array of three elements: <code>attribute_1<\/code>, <code>attribute_2<\/code> and <code>attribute_3<\/code>. The element names as well as the values can be freely chosen for the new shortcode.<\/p>\n\n\n\n<h3 id=\"h-example-output-with-attribute\">Example output with attribute<\/h3>\n\n\n\n<p>If the shortcode would be inserted as follows, the output would be in each case:<\/p>\n\n\n\n<p><code>[dynamic_content]<\/code> &#8211; <code>predefined_1predefined_2predefined_3<\/code><\/p>\n\n\n\n<p><code>[dynamic_content attribute_1=\"manipuliert\"]<\/code> &#8211; <code>manipuliertpredefined_2predefined_3<\/code><\/p>\n\n\n\n<p><code>[dynamic_content attribute_1=\"manipuliert\" attribute_2=\"nochmals\" attribute_3=\"und wieder\"]<\/code> &#8211; <code>manipuliertnochmalsund wieder<\/code><\/p>\n\n\n\n<p> Source: <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_shortcode\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress add_shortcode()<\/a> <\/p>\n\n\n\n<p> Downloads: <a href=\"https:\/\/tschaki.com\/downloads\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/tschaki.com\/downloads<\/a> <\/p>","protected":false},"excerpt":{"rendered":"<p>Admittedly, a WordPress website without shortcuts would be almost comparable to a static HTML template. We show how you can add your own dynamic content and features to a theme or plugin.<\/p>","protected":false},"author":1,"featured_media":2398,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60,99],"tags":[144,104,102,142,143,100],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v16.6 (Yoast SEO v17.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>New shortcode for wordpress theme and plugin &bull; tschaki<\/title>\n<meta name=\"description\" content=\"Create a new custom shortcode in PHP for your WordPress plugin or theme with attributes for dynamic output.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create shortcode in PHP for Wordpress\" \/>\n<meta property=\"og:description\" content=\"Create a new custom shortcode in PHP for your WordPress plugin or theme with attributes for dynamic output.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/\" \/>\n<meta property=\"og:site_name\" content=\"tschaki\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-28T02:27:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-20T14:42:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1580\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Trent Bojett\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/tschaki.com\/#organization\",\"name\":\"Tschaki.com\",\"url\":\"https:\/\/tschaki.com\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png\",\"contentUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png\",\"width\":1022,\"height\":273,\"caption\":\"Tschaki.com\"},\"image\":{\"@id\":\"https:\/\/tschaki.com\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tschaki.com\/#website\",\"url\":\"https:\/\/tschaki.com\/\",\"name\":\"tschaki\",\"description\":\"Codesupport and alternatives\",\"publisher\":{\"@id\":\"https:\/\/tschaki.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tschaki.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"contentUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"width\":1580,\"height\":500,\"caption\":\"create wordpress plugin\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#webpage\",\"url\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/\",\"name\":\"New shortcode for wordpress theme and plugin &bull; tschaki\",\"isPartOf\":{\"@id\":\"https:\/\/tschaki.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#primaryimage\"},\"datePublished\":\"2020-11-28T02:27:10+00:00\",\"dateModified\":\"2021-11-20T14:42:32+00:00\",\"description\":\"Create a new custom shortcode in PHP for your WordPress plugin or theme with attributes for dynamic output.\",\"breadcrumb\":{\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tschaki.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create shortcode in PHP for WordPress\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#webpage\"},\"author\":{\"@id\":\"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219\"},\"headline\":\"Create shortcode in PHP for WordPress\",\"datePublished\":\"2020-11-28T02:27:10+00:00\",\"dateModified\":\"2021-11-20T14:42:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#webpage\"},\"wordCount\":172,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/tschaki.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"keywords\":[\"[]\",\"php\",\"plugin\",\"shortcode\",\"theme\",\"wordpress\"],\"articleSection\":[\"Technology\",\"Wordpress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219\",\"name\":\"Trent Bojett\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g\",\"caption\":\"Trent Bojett\"},\"description\":\"My name is Trent Bojett, I am 28 years old and I live in Switzerland. Because of my enthusiasm for writing and the logical world (= software development) I started this blog in early 2020.\",\"sameAs\":[\"https:\/\/tschaki.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"New shortcode for wordpress theme and plugin &bull; tschaki","description":"Create a new custom shortcode in PHP for your WordPress plugin or theme with attributes for dynamic output.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/","og_locale":"en_US","og_type":"article","og_title":"Create shortcode in PHP for Wordpress","og_description":"Create a new custom shortcode in PHP for your WordPress plugin or theme with attributes for dynamic output.","og_url":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/","og_site_name":"tschaki","article_published_time":"2020-11-28T02:27:10+00:00","article_modified_time":"2021-11-20T14:42:32+00:00","og_image":[{"width":1580,"height":500,"url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Trent Bojett","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/tschaki.com\/#organization","name":"Tschaki.com","url":"https:\/\/tschaki.com\/","sameAs":[],"logo":{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/#logo","inLanguage":"en-US","url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png","contentUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png","width":1022,"height":273,"caption":"Tschaki.com"},"image":{"@id":"https:\/\/tschaki.com\/#logo"}},{"@type":"WebSite","@id":"https:\/\/tschaki.com\/#website","url":"https:\/\/tschaki.com\/","name":"tschaki","description":"Codesupport and alternatives","publisher":{"@id":"https:\/\/tschaki.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tschaki.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#primaryimage","inLanguage":"en-US","url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","contentUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","width":1580,"height":500,"caption":"create wordpress plugin"},{"@type":"WebPage","@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#webpage","url":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/","name":"New shortcode for wordpress theme and plugin &bull; tschaki","isPartOf":{"@id":"https:\/\/tschaki.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#primaryimage"},"datePublished":"2020-11-28T02:27:10+00:00","dateModified":"2021-11-20T14:42:32+00:00","description":"Create a new custom shortcode in PHP for your WordPress plugin or theme with attributes for dynamic output.","breadcrumb":{"@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tschaki.com\/en\/"},{"@type":"ListItem","position":2,"name":"Create shortcode in PHP for WordPress"}]},{"@type":"Article","@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#article","isPartOf":{"@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#webpage"},"author":{"@id":"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219"},"headline":"Create shortcode in PHP for WordPress","datePublished":"2020-11-28T02:27:10+00:00","dateModified":"2021-11-20T14:42:32+00:00","mainEntityOfPage":{"@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#webpage"},"wordCount":172,"commentCount":0,"publisher":{"@id":"https:\/\/tschaki.com\/#organization"},"image":{"@id":"https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#primaryimage"},"thumbnailUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","keywords":["[]","php","plugin","shortcode","theme","wordpress"],"articleSection":["Technology","Wordpress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/tschaki.com\/en\/new-shortcode-for-wordpress-theme-and-plugin\/#respond"]}]},{"@type":"Person","@id":"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219","name":"Trent Bojett","image":{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g","caption":"Trent Bojett"},"description":"My name is Trent Bojett, I am 28 years old and I live in Switzerland. Because of my enthusiasm for writing and the logical world (= software development) I started this blog in early 2020.","sameAs":["https:\/\/tschaki.com"]}]}},"_links":{"self":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/2627"}],"collection":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/comments?post=2627"}],"version-history":[{"count":8,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/2627\/revisions"}],"predecessor-version":[{"id":3044,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/2627\/revisions\/3044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/media\/2398"}],"wp:attachment":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/media?parent=2627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/categories?post=2627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/tags?post=2627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}