Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save cliffordp/90235d62ce8df52caa34 to your computer and use it in GitHub Desktop.

Select an option

Save cliffordp/90235d62ce8df52caa34 to your computer and use it in GitHub Desktop.
Add One-Click Empty buttons via Shortcake by adding 'add_button' => true when registering Shortcode UI arguments -- can also use 'add_button' => 'icon_only' -- requires https://wordpress.org/plugins/shortcode-ui/ be installed and activated
<?php
add_action( 'media_buttons', 'shortcode_ui_editor_one_click_insert_buttons', 200 ); // higher priority adds it to the right side of all the other buttons (Add Media, Gravity Forms, etc.)
// adapted from https://github.com/fusioneng/Shortcake/issues/94#issuecomment-68020127
function shortcode_ui_editor_one_click_insert_buttons( $editor_id = '' ) { // without $editor_id = '', 'content' gets printed before the button
// Check if Shortcake is installed and activated
if( ! method_exists( 'Shortcode_UI', 'get_instance' ) ) {
return false;
}
// Get all registered UI shortcodes
$ui_shortcodes = Shortcode_UI::get_instance()->get_shortcodes();
if( empty( $ui_shortcodes ) ) {
return false;
}
wp_enqueue_script( 'jquery' ); // if not already, we need it
// Initialize
$html = '';
$script = '';
// Add Button and jQuery for each UI shortcode with add_button attribute
// to display only an icon:
// 'add_button' => 'icon_only',
// to display icon + label:
// 'add_button' => 'yea',
// anything that passes the empty() check
foreach ( $ui_shortcodes as $shortcode => $atts ) {
// Skip if add_button attribute isn't set
if ( empty( $atts['add_button'] ) ) {
continue;
}
$label = ' Insert ' . $atts['label'];
if ( 'icon_only' == $atts['add_button'] ) {
$label = '';
}
// If shortcode has an inner_content attribute (e.g. single shortcode [button] vs [button][/button])
$enclosed_shortcode_tail = '';
if ( isset( $atts['inner_content'] ) ) {
$enclosed_shortcode_tail = sprintf( '[/%s]', $shortcode );
}
// Compile button HTML
$html .= sprintf( '<a href="#" id="insert-%1$s-button" class="button insert-%1$s add-%1$s" data-editor="content" title="Insert %2$s"><span class="dashicons wp-media-buttons-icon %3$s"></span>%4$s</a>',
$shortcode,
$atts['label'],
$atts['listItemImage'],
$label
);
// Build shortcode will all possible attributes set to ""
$att_markup = array();
$has_content_att = false;
foreach( $atts['attrs'] as $att_array ) {
$att_markup[] = $att_array['attr'] . '=""'; // notice double-quotes
} // end foreach
// notice escaped single-quotes in send_to_editor -- they are required, else JS console error and will not insert into Editor
$script .= sprintf( 'jQuery("#insert-%1$s-button").on("click", function() {
window.parent.send_to_editor(\'[%1$s %2$s]%3$s\');
});',
$shortcode,
implode( ' ', $att_markup ),
$enclosed_shortcode_tail
);
} // end foreach
$script = sprintf( '<script>%s</script>', $script );
$html .= $script;
echo $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment