Skip to content

Instantly share code, notes, and snippets.

@mmikkel
Created April 20, 2015 14:06
Show Gist options
  • Save mmikkel/0937f634f788e78de5f5 to your computer and use it in GitHub Desktop.
Save mmikkel/0937f634f788e78de5f5 to your computer and use it in GitHub Desktop.
TinyMCE button for custom shortcode
<?php
// [myCustomShortcode type="tip"]Dette er et godt tips[/myCustomShortcode]
function clientName_myCustomShortcode_func( $atts, $content = null ) {
$args = shortcode_atts( array(
'type' => 'tip',
), $atts );
if ( ! $content ) {
return '';
}
if ( $args[ 'type' ] === 'foo' ) {
$headline = 'Foo';
} else {
$headline = 'Bar';
}
return '<span class="tip type-' . $args[ 'type' ] . '"><span class="tipHeadline">' . $headline . '</span><span class="tipText">' . strip_tags( trim( $content ), '<br/>' ) . '</span></span>';
}
add_shortcode( 'myCustomShortcode', 'clientName_myCustomShortcode_func' );
function clientName_add_myCustomShortcode_button() {
// Check user permissions
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
return;
}
// Verify the post type
global $typenow;
if( ! in_array( $typenow, array( 'post', 'page' ) ) )
return;
// Check if WYSIWYG is enabled
if ( get_user_option( 'rich_editing' ) == 'true') {
add_filter( 'mce_external_plugins', 'clientName_myCustomShortcode_btn_add_tinymce_plugin' );
add_filter( 'mce_buttons', 'clientName_myCustomShortcode_btn_register_tinymce_plugin' );
}
}
add_action( 'admin_head', 'clientName_add_myCustomShortcode_button' );
function clientName_myCustomShortcode_btn_add_tinymce_plugin($plugin_array) {
$plugin_array[ 'clientName_myCustomShortcode_button' ] = get_template_directory_uri() .'/assets/admin/scripts/clientName_myCustomShortcode_btn.js';
return $plugin_array;
}
function clientName_myCustomShortcode_btn_register_tinymce_plugin( $buttons ) {
array_push( $buttons, 'clientName_myCustomShortcode_button' );
return $buttons;
}
function clientName_shortcode_btns_css() {
wp_enqueue_style( 'clientNamemyCustomShortcode', get_template_directory_uri() .'/assets/admin/styles/clientName_shortcode_btns.css' );
}
add_action( 'admin_enqueue_scripts', 'clientName_shortcode_btns_css' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment