Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BAProductions/8cd18939a8e29f7437025177e1f4c9a4 to your computer and use it in GitHub Desktop.
Save BAProductions/8cd18939a8e29f7437025177e1f4c9a4 to your computer and use it in GitHub Desktop.
// Add a custom function to create the meta box
add_action('add_meta_boxes', 'add_shortcodes_metabox');
// Function to add the meta box to the post edit screen
function add_shortcodes_metabox() {
add_meta_box(
'shortcodes-metabox', // Unique ID for the meta box
'Available Shortcodes', // Title of the meta box
'display_shortcodes', // Callback to display the meta box content
'post', // Screen (post type) where the meta box will be displayed
'side', // Context (side or normal)
'low' // Priority (low for a less prominent position)
);
}
// Function to display content of the meta box
function display_shortcodes() {
global $shortcode_tags; // Correctly access the global shortcode_tags variable
// Sort the shortcodes alphabetically
$available_shortcodes = $shortcode_tags;
ksort($available_shortcodes);
// Output the list of all available shortcodes
echo '<div class="categorydiv">';
echo '<div class="tabs-panel">';
echo '<ul id="categorychecklist" class="categorychecklist form-no-clear" style="">';
foreach ($available_shortcodes as $shortcode => $callback) {
echo '<li><pre><code>[' . esc_html($shortcode) . ']</code></pre></li>';
}
echo '</ul>';
echo '</div>'; // End of "All" tab content panel
echo '</div>'; // End of shortcodes container
}
function enqueue_custom_admin_styles() {
echo '<style>
#shortcodes-metabox .categorychecklist { margin: 0 }
#shortcodes-metabox .tabs-panel { max-height: 212px }
#shortcodes-metabox li pre code { margin: 0; display: flex }
</style>';
}
add_action('admin_head', 'enqueue_custom_admin_styles');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment