{"id":81,"date":"2024-09-26T20:02:07","date_gmt":"2024-09-26T16:02:07","guid":{"rendered":"http:\/\/keksus.com\/?p=81"},"modified":"2025-01-05T21:52:27","modified_gmt":"2025-01-05T17:52:27","slug":"creating-a-simple-wordpress-shortcode-button","status":"publish","type":"post","link":"https:\/\/hostatto.com\/blog\/creating-a-simple-wordpress-shortcode-button.html","title":{"rendered":"Creating a simple shortcode button"},"content":{"rendered":"<p>In this tutorial I create simple button shortcode with\u00a0three attributes:<\/p>\n<ul>\n<li>url<\/li>\n<li>target<\/li>\n<li>background<\/li>\n<\/ul>\n<p>Put this code in your functions.php file<br \/>\n<div class=\"codenote php\" style=\"background:#F39D77\">\n\t\t\t\t<strong>Wordpress<\/strong>\n\t\t\t<\/div><\/p>\n<pre class=\"lang:default decode:true \">function btn_big_shortcode( $atts, $content = null ){\r\n\t$args = shortcode_atts( array(\r\n\t\t'url'           =&gt; '',\r\n\t\t'target'        =&gt; 'self',\r\n\t\t'background'    =&gt; '#F39D77',\r\n\t), $atts );\r\n\t$out = '&lt;div class=\"btn-big\"&gt;\r\n\t\t\t\t&lt;a style=\"background:'. esc_attr( $args['background'] ) .'\" target='. esc_attr( $args['target'] ) .' href='. esc_attr( $args['url'] ) .'&gt;\r\n\t\t\t\t'. do_shortcode( wp_kses_post( $content ) ) .'\r\n\t\t\t\t&lt;\/a&gt;\r\n\t\t\t&lt;\/div&gt;';\r\n\treturn $out;\r\n}\r\nadd_shortcode('btn_big', 'btn_big_shortcode');<\/pre>\n<p>By the same principle, you can add attributes to an array and display them in a shortcode.<\/p>\n<p>Now set the CSS styles for the button<br \/>\n<div class=\"codenote php\" style=\"background:#F39D77\">\n\t\t\t\t<strong>CSS<\/strong>\n\t\t\t<\/div><\/p>\n<pre class=\"lang:default decode:true\">.btn-big a {\r\n    display: inline-block;\r\n    font: bold 20px sans-serif;\r\n    color: #fff;\r\n    border-radius: 3px;\r\n    padding: 10px 40px;\r\n    margin-bottom: 15px;\r\n    text-align: center;\r\n    text-decoration: none;\r\n}\r\n.btn-big a:hover{\r\n    text-decoration: none;\r\n}<\/pre>\n<p>The button is ready, to output it add this shortcode in text editor:<\/p>\n<blockquote><p>[btn_big url=\"http:\/\/keksus.com\/\"]Sample button[\/btn_big]<\/p><\/blockquote>\n<p>Only change the link to the one you need.<br \/>\nIf you want open link in a new window, add the attribute target=\"_blank\" to shortcode:<\/p>\n<blockquote><p>[btn_big url=\"http:\/\/keksus.com\/\" target=\"_blank\"]Sample button[\/btn_big]<\/p><\/blockquote>\n<p>If you want change button background color, add the attribute background=\"#000\" to shortcode:<\/p>\n<blockquote><p>[btn_big url=\"http:\/\/keksus.com\/\" target=\"_blank\" background=\"#000\"]Sample button[\/btn_big]<\/p><\/blockquote>\n<h2>Demo<\/h2>\n<div class=\"btn-big\">\n\t\t\t\t<a style=\"background:#F39D77\" target=self href=http:\/\/keksus.com\/>\n\t\t\t\tSample button<\/a>\n\t\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>...<\/p>\n","protected":false},"author":1,"featured_media":134,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,54],"tags":[17,13],"class_list":["post-81","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tuts","category-wordpress","tag-shortcode","tag-tutorials"],"_links":{"self":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":0,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/media\/134"}],"wp:attachment":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}