{"id":1680,"date":"2015-12-01T07:13:53","date_gmt":"2015-12-01T05:13:53","guid":{"rendered":"https:\/\/www.robinglauser.ch\/blog\/?p=1680"},"modified":"2015-11-30T22:29:12","modified_gmt":"2015-11-30T20:29:12","slug":"create-runnable-snippets-on-your-wordpress-blog","status":"publish","type":"post","link":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/","title":{"rendered":"Create runnable snippets on your WordPress blog"},"content":{"rendered":"<p>Here&#8217;s how to create a runnable snippet on your WordPress blog.<\/p>\n<pre>$(function() {\r\n    $('.js-runnable').each(function(){\r\n        var that = this;\r\n        var button = $('&lt;p&gt;&lt;button class=\"btn btn-default\"&gt;Run Snippet&lt;\/button&gt;&lt;\/p&gt;');\r\n        $(button).find('button').click(function(){\r\n            eval($(that).text());\r\n        });\r\n        $(this).after(button);\r\n    });\r\n});<\/pre>\n<p>You need to create a pre element with the class .js-runnable.\u00a0The upper script will the automatically add a button after the code to run the code inside the snippet.<\/p>\n<p><strong>Demonstration:<\/strong><\/p>\n<pre class=\"js-runnable\">!(function(){\r\n    var a = 'World';\r\n    alert('Hello '+a);\r\n})();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Here&#8217;s how to create a runnable snippet on your WordPress blog. $(function() { $(&#8216;.js-runnable&#8217;).each(function(){ var that = this; var button = $(&#8216;&lt;p&gt;&lt;button class=&#8221;btn btn-default&#8221;&gt;Run Snippet&lt;\/button&gt;&lt;\/p&gt;&#8217;); $(button).find(&#8216;button&#8217;).click(function(){ eval($(that).text()); }); $(this).after(button); }); }); You need to create a pre element with the class .js-runnable.\u00a0The upper script will the automatically add a button after the code to run the code inside the snippet. &#8230; <a class=\"read-more\" href=\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":1681,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[45],"tags":[],"class_list":["post-1680","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create runnable snippets on your Wordpress blog - Robin Glauser<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create runnable snippets on your Wordpress blog - Robin Glauser\" \/>\n<meta property=\"og:description\" content=\"Here&#8217;s how to create a runnable snippet on your WordPress blog. $(function() { $(&#039;.js-runnable&#039;).each(function(){ var that = this; var button = $(&#039;&lt;p&gt;&lt;button class=&quot;btn btn-default&quot;&gt;Run Snippet&lt;\/button&gt;&lt;\/p&gt;&#039;); $(button).find(&#039;button&#039;).click(function(){ eval($(that).text()); }); $(this).after(button); }); }); You need to create a pre element with the class .js-runnable.\u00a0The upper script will the automatically add a button after the code to run the code inside the snippet. ... Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\" \/>\n<meta property=\"og:site_name\" content=\"Robin Glauser\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-01T05:13:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"467\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Robin Glauser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@robinglauser\" \/>\n<meta name=\"twitter:site\" content=\"@robinglauser\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Robin Glauser\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\"},\"author\":{\"name\":\"Robin Glauser\",\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19\"},\"headline\":\"Create runnable snippets on your WordPress blog\",\"datePublished\":\"2015-12-01T05:13:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\"},\"wordCount\":50,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19\"},\"image\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg\",\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\",\"url\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\",\"name\":\"Create runnable snippets on your Wordpress blog - Robin Glauser\",\"isPartOf\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg\",\"datePublished\":\"2015-12-01T05:13:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage\",\"url\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg\",\"contentUrl\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg\",\"width\":1280,\"height\":467},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.robinglauser.ch\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create runnable snippets on your WordPress blog\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/#website\",\"url\":\"https:\/\/www.robinglauser.ch\/blog\/\",\"name\":\"Robin Glauser\",\"description\":\"My Blog about Development, Design and my random thoughts.\",\"publisher\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.robinglauser.ch\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19\",\"name\":\"Robin Glauser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg\",\"url\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg\",\"contentUrl\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg\",\"width\":800,\"height\":530,\"caption\":\"Robin Glauser\"},\"logo\":{\"@id\":\"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg\"},\"description\":\"I'm a web developer.\",\"sameAs\":[\"https:\/\/www.robinglauser.ch\",\"https:\/\/www.instagram.com\/robinglauser\/\",\"https:\/\/x.com\/robinglauser\"],\"url\":\"https:\/\/www.robinglauser.ch\/blog\/author\/robin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create runnable snippets on your Wordpress blog - Robin Glauser","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:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/","og_locale":"en_US","og_type":"article","og_title":"Create runnable snippets on your Wordpress blog - Robin Glauser","og_description":"Here&#8217;s how to create a runnable snippet on your WordPress blog. $(function() { $('.js-runnable').each(function(){ var that = this; var button = $('&lt;p&gt;&lt;button class=\"btn btn-default\"&gt;Run Snippet&lt;\/button&gt;&lt;\/p&gt;'); $(button).find('button').click(function(){ eval($(that).text()); }); $(this).after(button); }); }); You need to create a pre element with the class .js-runnable.\u00a0The upper script will the automatically add a button after the code to run the code inside the snippet. ... Read More","og_url":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/","og_site_name":"Robin Glauser","article_published_time":"2015-12-01T05:13:53+00:00","og_image":[{"width":1280,"height":467,"url":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg","type":"image\/jpeg"}],"author":"Robin Glauser","twitter_card":"summary_large_image","twitter_creator":"@robinglauser","twitter_site":"@robinglauser","twitter_misc":{"Written by":"Robin Glauser"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#article","isPartOf":{"@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/"},"author":{"name":"Robin Glauser","@id":"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19"},"headline":"Create runnable snippets on your WordPress blog","datePublished":"2015-12-01T05:13:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/"},"wordCount":50,"commentCount":0,"publisher":{"@id":"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19"},"image":{"@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg","articleSection":["Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/","url":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/","name":"Create runnable snippets on your Wordpress blog - Robin Glauser","isPartOf":{"@id":"https:\/\/www.robinglauser.ch\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage"},"image":{"@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg","datePublished":"2015-12-01T05:13:53+00:00","breadcrumb":{"@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#primaryimage","url":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg","contentUrl":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/11\/photo-1440470177828-6381dc5074ba-e1448914861257.jpg","width":1280,"height":467},{"@type":"BreadcrumbList","@id":"https:\/\/www.robinglauser.ch\/blog\/2015\/12\/01\/create-runnable-snippets-on-your-wordpress-blog\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robinglauser.ch\/blog\/"},{"@type":"ListItem","position":2,"name":"Create runnable snippets on your WordPress blog"}]},{"@type":"WebSite","@id":"https:\/\/www.robinglauser.ch\/blog\/#website","url":"https:\/\/www.robinglauser.ch\/blog\/","name":"Robin Glauser","description":"My Blog about Development, Design and my random thoughts.","publisher":{"@id":"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robinglauser.ch\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.robinglauser.ch\/blog\/#\/schema\/person\/e1a94504a6ff5171fa13670932514b19","name":"Robin Glauser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg","url":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg","contentUrl":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg","width":800,"height":530,"caption":"Robin Glauser"},"logo":{"@id":"https:\/\/www.robinglauser.ch\/blog\/wp-content\/uploads\/2015\/10\/DSC_1244_small.jpg"},"description":"I'm a web developer.","sameAs":["https:\/\/www.robinglauser.ch","https:\/\/www.instagram.com\/robinglauser\/","https:\/\/x.com\/robinglauser"],"url":"https:\/\/www.robinglauser.ch\/blog\/author\/robin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/posts\/1680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/comments?post=1680"}],"version-history":[{"count":7,"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/posts\/1680\/revisions"}],"predecessor-version":[{"id":1688,"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/posts\/1680\/revisions\/1688"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/media\/1681"}],"wp:attachment":[{"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/media?parent=1680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/categories?post=1680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robinglauser.ch\/blog\/wp-json\/wp\/v2\/tags?post=1680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}