{"id":4482637,"date":"2014-01-08T16:07:28","date_gmt":"2014-01-08T16:07:28","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/add-insert-button\/"},"modified":"2016-08-21T14:32:21","modified_gmt":"2016-08-21T14:32:21","slug":"add-insert-button","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/add-insert-button\/","title":{"rendered":"Add Insert Button"},"content":{"rendered":"<p>It would make this plugin more client friendly if there were a way to auto insert the shortcode by clicking a button and selecting which snippet to use from a drop down.  If you are interested, here is the code I used:<\/p>\n<pre><code>\/\/ Function to add the snippet button next to the media button\n\tfunction add_snippet_button( $context ){\n\t\t\/\/ If we are on a post\/page or new post\/page\n\t\tif( is_admin() &amp;&amp; in_array( basename( $_SERVER[&#039;PHP_SELF&#039;] ), array( &#039;post.php&#039;, &#039;page.php&#039;, &#039;page-new.php&#039;, &#039;post-new.php&#039; ) ) ) {\n\t\t\t$context .= &#039;&lt;a class=&quot;button thickbox&quot; title=&quot;Add Snippet&quot; href=&quot;#TB_inline?width=400&amp;inlineId=add_snippet_popup_container&quot;&gt;&lt;span class=&quot;snippet_button_icon&quot;&gt;&lt;\/span&gt;Add Snippet&lt;\/a&gt;&#039;;\n\t\t}\n\t\treturn $context;\n\t}\n\tadd_action( &#039;media_buttons_context&#039;, &#039;add_snippet_button&#039; );\n\t\/\/ Function to add drop down to a pop up window\n\tfunction add_snippet_button_popup_content() {\n\t\t?&gt;\n\t\t&lt;script&gt;\n\t\t\tfunction InsertSnippet(){\n\t\t\t\tvar snippet_slug = jQuery(&quot;#add_snippet_slug&quot;).val();\n\t\t\t\tif(snippet_slug == &quot;&quot;){\n\t\t\t\t\talert(&quot;Please select a snippet&quot;);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\twindow.send_to_editor(&quot;[snippet slug=\\&quot;&quot; + snippet_slug + &quot;\\&quot; \/]&quot;);\n\t\t\t}\n\t\t&lt;\/script&gt;\n\t\t&lt;div id=&quot;add_snippet_popup_container&quot; style=&quot;display:none;&quot;&gt;\n\t\t\t&lt;div class=&quot;wrap&quot;&gt;\n\t\t\t\t&lt;div&gt;\n\t\t\t\t\t&lt;div style=&quot;padding:15px 15px 0 15px;&quot;&gt;\n\t\t\t\t\t\t&lt;h3 style=&quot;color:#5A5A5A!important; font-family:Georgia,Times New Roman,Times,serif!important; font-size:1.8em!important; font-weight:normal!important;&quot;&gt;Insert A Snippet&lt;\/h3&gt;\n\t\t\t\t\t\t&lt;span&gt;Select a snippet below to add it to your post or page.&lt;\/span&gt;\n\t\t\t\t\t&lt;\/div&gt;\n\t\t\t\t\t&lt;div style=&quot;padding:15px 15px 0 15px;&quot;&gt;\n\t\t\t\t\t\t&lt;select id=&quot;add_snippet_slug&quot;&gt;\n\t\t\t\t\t\t\t&lt;option value=&quot;&quot;&gt;Select a Snippet&lt;\/option&gt;\n\t\t\t\t\t\t\t&lt;?php\n\t\t\t\t\t\t\t\t$args = array(\n\t\t\t\t\t\t\t\t\t&#039;post_type&#039; =&gt; &#039;snippet&#039;,\n\t\t\t\t\t\t\t\t\t&#039;posts_per_page&#039; =&gt; -1,\n\t\t\t\t\t\t\t\t\t&#039;post_status&#039; =&gt; &#039;publish&#039;\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t$snippets = get_posts( $args );\n\t\t\t\t\t\t\t\tforeach( $snippets as $snippet ){\n\t\t\t\t\t\t\t\t\t?&gt;\n\t\t\t\t\t\t\t\t\t&lt;option value=&quot;&lt;?php echo $snippet-&gt;post_name; ?&gt;&quot;&gt;&lt;?php echo $snippet-&gt;post_title; ?&gt;&lt;\/option&gt;\n\t\t\t\t\t\t\t\t\t&lt;?php\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t?&gt;\n\t\t\t\t\t\t&lt;\/select&gt;\n\t\t\t\t\t&lt;\/div&gt;\n\t\t\t\t\t&lt;div style=&quot;padding:15px;&quot;&gt;\n\t\t\t\t\t\t&lt;input type=&quot;button&quot; class=&quot;button-primary&quot; value=&quot;Insert Snippet&quot; onclick=&quot;InsertSnippet();&quot;\/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;\n\t\t\t\t\t\t&lt;a class=&quot;button&quot; style=&quot;color:#bbb;&quot; href=&quot;#&quot; onclick=&quot;tb_remove(); return false;&quot;&gt;Cancel&lt;\/a&gt;\n\t\t\t\t\t&lt;\/div&gt;\n\t\t\t\t&lt;\/div&gt;\n\t\t\t&lt;\/div&gt;\n\t\t&lt;\/div&gt;\n\t\t&lt;?php\n\t}\n\tadd_action( &#039;admin_footer&#039;,  &#039;add_snippet_button_popup_content&#039; );<\/code><\/pre>\n<p>I haven&#8217;t tested to see if it&#8217;s fool proof, but it works well enough for me.  I&#8217;m hopeful that you&#8217;ll go through it and make it better and add it in to the next update?  Thanks for a great plugin.<\/p>\n<p>https:\/\/wordpress.org\/plugins\/wp-snippets\/<\/p>\n","protected":false},"template":"","class_list":["post-4482637","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/4482637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/4482637\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=4482637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}