Skip to content

Instantly share code, notes, and snippets.

@petersplugins
Created November 14, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petersplugins/e75840cf2a325cf93e192640cc2b2665 to your computer and use it in GitHub Desktop.
Save petersplugins/e75840cf2a325cf93e192640cc2b2665 to your computer and use it in GitHub Desktop.
<?php
// This code snippet adds a meta box in the right sidebar of the post screen that lists all available shortcodes
// As we use a default WordPress admin meta box the box can be closed to save space and opened when needed
// Add a custom function to create the meta box
add_action( 'add_meta_boxes', 'add_shortcodes_metabox' );
// This function adds the meta box
function add_shortcodes_metabox() {
add_meta_box( 'shortcodes-metabox', 'Available Shortcodes', 'display_shortcodes', 'post', 'side', 'high' );
// The parameter 'post' makes sure the meta box shows only on the post screen
// If you want it also to appear on the pages screen change 'post' to array( 'post', 'page' )
}
// This function creates the content of the meta box
// We don't have to care about all the other stuff because this is done by add_meta_box
function display_shortcodes() {
// The available shortcodes are stored in the global variable $shortcode_tags, so we need access to it
global $shortcode_tags;
// let's sort the list alphabetically
$available_shortcodes = $shortcode_tags;
ksort( $available_shortcodes );
// show an unordered list of available shortcodes
echo '<ul>';
foreach ( $available_shortcodes as $key => $value ) {
echo '<li>' . $key . '</li>';
}
echo '</ul>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment