Skip to content

Instantly share code, notes, and snippets.

@jaredrethman
Last active February 3, 2020 08:14
Show Gist options
  • Save jaredrethman/5cb3c9252834087b63ec to your computer and use it in GitHub Desktop.
Save jaredrethman/5cb3c9252834087b63ec to your computer and use it in GitHub Desktop.
Wordpress 3.9 TinyMCE 4 - Custom User state shortcode drop-down for loggedin, loggedout and loggedin and assigned to a buddypress group.
function my_user_has_group(){
global $bp;
if( !bp_has_groups( bp_ajax_querystring( 'groups' ).'&user_id='.bp_loggedin_user_id())) {
return false;
}else{
return true;
}
}
/* User state shortcode drop-down */
add_action('admin_head', 'twp_add_mce_button');
function twp_add_mce_button() {
// check user permissions
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
// check if WYSIWYG is enabled
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_external_plugins', 'my_add_tinymce_plugin' );
add_filter( 'mce_buttons_2', 'my_register_mce_button' );
}
}
// Declare script for new button
function my_add_tinymce_plugin( $plugin_array ) {
$plugin_array['myMCEbutton'] = plugins_url('/js/shortcodes.js',__FILE__);
return $plugin_array;
}
// Register new button in the editor
function my_register_mce_button( $buttons ) {
array_push( $buttons, 'my_button' );
return $buttons;
}
(function($) {
"use strict";
//Shortcodes
tinymce.PluginManager.add('myMCEbutton', function( editor, url ) {
var selected = tinyMCE.activeEditor.selection.getContent();
editor.addButton( 'my_button', {
type: 'splitbutton',
icon: false,
text: 'User State',
title: 'User State Shortcodes',
onclick : function(e) {},
menu: [
{text: 'Loggedin has group',onclick:function(){
var selected = tinyMCE.activeEditor.selection.getContent();
var content = '';
if (selected !== '') {
content = "[userstate loggedin_has_group='true']" + selected + "[/userstate]";
} else {
content = '';
}
tinymce.execCommand('mceInsertContent', false, content);
}},
{text: 'Loggedin no group',onclick:function(){
var selected = tinyMCE.activeEditor.selection.getContent();
var content = '';
if (selected !== '') {
content = "[userstate loggedin_no_group='true']" + selected + "[/userstate]";
} else {
content = '';
}
tinymce.execCommand('mceInsertContent', false, content);
}},
{text: 'LoggedOut',onclick:function(){
var selected = tinyMCE.activeEditor.selection.getContent();
var content = '';
if (selected !== '') {
content = "[userstate not_loggedin='true']" + selected + "[/userstate]";
} else {
content = '';
}
tinymce.execCommand('mceInsertContent', false, content);
}}
]
});
});
})(jQuery);
function my_userstate_sc( $atts, $content = null, $tag ) {
global $bp;
extract( shortcode_atts( array(
'loggedin_no_group' => '',
'loggedin_has_group' => '',
'not_loggedin' => ''
), $atts ) );
switch( $atts ) {
case $loggedin_no_group == 'true' :
if( is_user_logged_in() && !twp_user_has_group() ){
return $content;
}
break;
case $loggedin_has_group == 'true' :
if( is_user_logged_in() && twp_user_has_group() ){
return $content;
}
break;
case $not_loggedin == 'true' :
if( !is_user_logged_in() && $not_loggedin == 'true'){
return $content;
}
break;
default :
return;
break;
}
return;
}
add_shortcode( 'userstate', 'twp_userstatetwo_sc' );
@jaredrethman
Copy link
Author

Took me hours to find a way to create drop-downs in the new TinyMCE editor using the shortcodes I created. Thought i would share it with anyone else looking for a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment