• wpismypuppet

    (@wordpressismypuppet)


    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:

    // Function to add the snippet button next to the media button
    	function add_snippet_button( $context ){
    		// If we are on a post/page or new post/page
    		if( is_admin() && in_array( basename( $_SERVER['PHP_SELF'] ), array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) {
    			$context .= '<a class="button thickbox" title="Add Snippet" href="#TB_inline?width=400&inlineId=add_snippet_popup_container"><span class="snippet_button_icon"></span>Add Snippet</a>';
    		}
    		return $context;
    	}
    	add_action( 'media_buttons_context', 'add_snippet_button' );
    	// Function to add drop down to a pop up window
    	function add_snippet_button_popup_content() {
    		?>
    		<script>
    			function InsertSnippet(){
    				var snippet_slug = jQuery("#add_snippet_slug").val();
    				if(snippet_slug == ""){
    					alert("Please select a snippet");
    					return;
    				}
    				window.send_to_editor("[snippet slug=\"" + snippet_slug + "\" /]");
    			}
    		</script>
    		<div id="add_snippet_popup_container" style="display:none;">
    			<div class="wrap">
    				<div>
    					<div style="padding:15px 15px 0 15px;">
    						<h3 style="color:#5A5A5A!important; font-family:Georgia,Times New Roman,Times,serif!important; font-size:1.8em!important; font-weight:normal!important;">Insert A Snippet</h3>
    						<span>Select a snippet below to add it to your post or page.</span>
    					</div>
    					<div style="padding:15px 15px 0 15px;">
    						<select id="add_snippet_slug">
    							<option value="">Select a Snippet</option>
    							<?php
    								$args = array(
    									'post_type' => 'snippet',
    									'posts_per_page' => -1,
    									'post_status' => 'publish'
    								);
    								$snippets = get_posts( $args );
    								foreach( $snippets as $snippet ){
    									?>
    									<option value="<?php echo $snippet->post_name; ?>"><?php echo $snippet->post_title; ?></option>
    									<?php
    								}
    							?>
    						</select>
    					</div>
    					<div style="padding:15px;">
    						<input type="button" class="button-primary" value="Insert Snippet" onclick="InsertSnippet();"/>&nbsp;&nbsp;&nbsp;
    						<a class="button" style="color:#bbb;" href="#" onclick="tb_remove(); return false;">Cancel</a>
    					</div>
    				</div>
    			</div>
    		</div>
    		<?php
    	}
    	add_action( 'admin_footer',  'add_snippet_button_popup_content' );

    I haven’t tested to see if it’s fool proof, but it works well enough for me. I’m hopeful that you’ll go through it and make it better and add it in to the next update? Thanks for a great plugin.

    http://wordpress.org/plugins/wp-snippets/

  • The topic ‘Add Insert Button’ is closed to new replies.