Last active
July 6, 2017 01:22
-
-
Save gatespace/07e5411c3d148a1b5893973368b37f8e to your computer and use it in GitHub Desktop.
WordPress ショートコードの作成とエディタへのボタン追加 ref: http://qiita.com/gatespace/items/bf46f5da04bd0493a5d3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add Shortcode [myshortcode][/myshortcode] | |
function my_shortcode_func( $atts , $content = null ) { | |
return '<div>' . $content . '</div>'; | |
} | |
add_shortcode( 'myshortcode', 'my_shortcode_func' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add new buttons | |
add_filter( 'mce_buttons', 'myplugin_register_buttons' ); | |
function myplugin_register_buttons( $buttons ) { | |
array_push( $buttons, 'separator', 'my_shortcode' ); // separator は区切り線(オプション) | |
return $buttons; | |
} | |
// Load the TinyMCE plugin : editor_plugin.js (wp2.5) | |
add_filter( 'mce_external_plugins', 'myplugin_register_tinymce_javascript' ); | |
function myplugin_register_tinymce_javascript( $plugin_array ) { | |
$plugin_array['myplugin_script'] = plugins_url( '/tinymce-plugin.js',__FILE__ ); // TinyMCE用のJS | |
return $plugin_array; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add Quicktags | |
function myshortcode_quicktags() { | |
if ( wp_script_is( 'quicktags' ) ) { | |
?> | |
<script type="text/javascript"> | |
QTags.addButton( 'my_shortcode', '[myshortcode]', '[myshortcode]', '[/myshortcode]', '', 'My shortcode', ); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'admin_print_footer_scripts', 'myshortcode_quicktags' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
/* Register the buttons */ | |
tinymce.create('tinymce.plugins.MyButtons', { | |
init : function(ed, url) { | |
/** | |
* Inserts shortcode content | |
*/ | |
ed.addButton( 'my_shortcode', { | |
title : 'Insert my shortcode', | |
image : url + '/images/btn.png', // アイコン urlはこのjsのあるディレクトリまで | |
cmd: 'my_shortcode_cmd' | |
}); | |
ed.addCommand( 'my_shortcode_cmd', function() { | |
var selected_text = ed.selection.getContent(); | |
var return_text = ''; | |
return_text = '[myshortcode]' + selected_text + '[/myshortcode]'; // 挿入されるコード | |
ed.execCommand('mceInsertContent', 0, return_text); | |
}); | |
}, | |
createControl : function(n, cm) { | |
return null; | |
}, | |
}); | |
/* Start the buttons */ | |
tinymce.PluginManager.add( 'myplugin_script', tinymce.plugins.MyButtons ); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment