• Hi,

    I’m just creating a new theme and I have some different shortcodes. I now want a button into the editor which just inserts something like: [table name=”” age=””]

    Is it possible to do this without a plugin? I need it to be in my theme.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Sub_Zero

    (@sub_zero)

    Ok thanks, I was reading many tutorial but still I don’t get it.

    Here is my current code:

    // functions.php
    add_action('init', 'mylink_button');
    
    function mylink_button() {
    
    	if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
    		return;
    	}
    
    	if ( get_user_option('rich_editing') == 'true' ) {
    		add_filter( 'mce_external_plugins', 'add_plugin' );
    		add_filter( 'mce_buttons', 'register_button' );
    	}
    
    }
    
    function register_button( $buttons ) {
    	array_push( $buttons, "|", "mylink" );
    	return $buttons;
    }
    
    function add_plugin( $plugin_array ) {
    	// main.js is saved into: wp_content/themes/myTheme/assets/js/main.js
    	$plugin_array['mylink'] = get_bloginfo( 'template_url' ) . '/assets/js/main.js';
    	return $plugin_array;
    }
    // main.js
    (function() {
    	alert('test'); // Even no alert!!
        tinymce.create('tinymce.plugins.mylink', {
            init : function(ed, url) {
                ed.addButton('mylink', {
                    title : 'My Link',
                    image : url+'/mylink.png',
                    onclick : function() {
                         ed.selection.setContent('[mylink]' + ed.selection.getContent() + '[/mylink]');
    
                    }
                });
            },
            createControl : function(n, cm) {
                return null;
            },
        });
        tinymce.PluginManager.add('mylink', tinymce.plugins.mylink);
    })();

    This code does pretty much nothing! I added that alert(‘test’) to see if there is something wrong with my main.js. It seems like it can’t get loaded.

    Any ideas what I’m doing wrong?

    Is the button being created? Can you see it in tinyMCE?

    What happens when you click it??

    Let me know, I have a plugin I recently compiled with a custom tinyMCE button that adds a shortcode to the page. I can reference that code if needed.

    I would first take it piece by piece. Get the tinyMCE button in there first. Then work on getting it to print something when clicked. Then adjust it to use your shortcode.

    Thread Starter Sub_Zero

    (@sub_zero)

    Thanks, I found a solution now. This works for me:

    add_action( 'init', 'code_button' );
    function code_button() {
    	add_filter( "mce_external_plugins", "code_add_button" );
    	add_filter( 'mce_buttons', 'code_register_button' );
    }
    function code_add_button( $plugin_array ) {
    	$plugin_array['mycodebutton'] = $dir = get_template_directory_uri() . '/assets/js/shortcode.js';
    	return $plugin_array;
    }
    function code_register_button( $buttons ) {
    	array_push( $buttons, 'codebutton' );
    	return $buttons;
    }
    (function() {
        tinymce.create('tinymce.plugins.code', {
            init : function(ed, url) {
    
                ed.addButton('codebutton', {
                    title : 'Tabelle',
                    cmd : 'codebutton',
                    image :  url + '/img/table.png'
                });
    
                ed.addCommand('codebutton', function() {
                    var selected_text = ed.selection.getContent();
                    var return_text = '';
                    return_text = '[myShortcode]';
                    ed.execCommand('mceInsertContent', 0, return_text);
                });
            },
            // ... Hidden code
        });
        // Register plugin
        tinymce.PluginManager.add( 'mycodebutton', tinymce.plugins.code );
    })();
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to create a button for my shortcode?’ is closed to new replies.