Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles

Reply
 
Thread Tools
[HOW-TO - vB4] Create a Custom Button for CK Editor (no file edits)
cellarius's Avatar
cellarius
Join Date: Aug 2005
Posts: 1,987

 

Show Printable Version Email this Page Subscription
cellarius cellarius is offline 02-18-2012, 11:00 PM

This article explains how to add or remove buttons from CKEditor by using Ckeditor and vBulletin plugins. No standard file edits needed.

To add a button to CKEditor, you need to create a CKE plugin. This is an example code for such a plugin. It will add "Hello World" to the editor and spawn a JS alert.
Important note: This article is on the technique of adding a button, not on how you will get your button to do whatever it is you want it to do. There are plenty sites out there that give support for CKEditor plugins. The CKE API documentation can be found here.



First step: Create the CKEditor plugin

The editor plugin files reside in the folder [forumroot]/clientscript/ckeplugins. Create a new folder in there. For the purpose of this article, I'll name it celButtonDemo. Inside that folder, create a file called plugin.js. So you end up with this:
[forumroot]/clientscript/ckeplugins/celButtonDemo/plugin.js
Important note: Please take notice that "celButtonDemo" will have to be replaced in the folder name and in the following code examples accordingly!.

Inside plugin.js goes the plugin code:
Code:
CKEDITOR.plugins.add( 'celButtonDemo',
{
	init : function( editor )
	{
		// This adds the button
		editor.ui.addButton('celButtonDemo',
		{
			// label: adds a hover text to the button. This uses the vBulletin
			// phrase "celButtonDemo" which you will have to add to your phrases
			// and to pass to CKE in a plugin
			label : editor.config.vbulletin.phrase.celButtonDemo,
			// command: the command that will be executed. It's defined later
			// in this file
			command : 'celButtonDemo',
			// icon: The path to the icon file you want to use for your button.
			// Here we use buttonimage.png in the buttons directory defined for
			// the current style (default: /images/editor)
			icon: BBURL + '/' + IMGDIR_BUTTON + '/celButtonDemo.png',
		});

		// This defines the command that's executed when the button is clicked
		editor.addCommand('celButtonDemo',
		{
			// modes: decide in what editor modes the button will be active;
			// set to 0 to grey it out
			modes : { enhancedsource:1, wysiwyg:1 },
			// Execute your JS Code - CKE offers a great API:
			exec: function(editor){
				alert('Hello world!');
				editor.insertHtml( 'Hello World! - Hello World!' );
			}
		});
	}
});


Second step: vB Operations - register your button, add it to the toolbar, and more

In a second step we will create two vB plugins and a vB phrase. You should bundle all that together to a product, to have it all nicely together.

Let's start with the phrase: Go to AdminCP, create a phrase in the CKEditor phrase group, and name it CelButtonDemo. Enter "Hello World, hello Button" - or whatever. Save. Done.

Then create a new plugin at hook "editor_construct". This will add our button to the CKE configuration.

PHP Code:
// Add our button to the extra plugins loaded
$this->config['_extraPlugins'] .= ',celButtonDemo';
// Register our phrase for the label of our button (see second step)
$this->config['vbulletin']['phrase']['celButtonDemo'] = $this->vbphrase['celButtonDemo']; 
OK, now let's create the second plugin at hook "editor_toolbar_filter":
In this example, our button will be inserted after the Quote-Button. Your options to place the button can be found in /vb/ckeditor.php. See line 7 in the following code:

PHP Code:
foreach ($toolbar AS &$row)
{
    if (
is_array($row))
    {
        foreach (
$row AS $id => $cmd)
        {
            if (
$cmd == 'Quote')
            {
                
$row array_merge(array_slice($row0$id+1),  array('celButtonDemo'), array_slice($row$id+1));
                break;
            }
        }
    }

Last, not least, we need a nice little icon for our button. Upload that to /images/buttons and name it celButtonsDemo.png. Dimensions: 21x21 px.



Result (Screencast):
http://screencast-o-matic.com/watch/clnqDcazQ



Removing buttons

The technique used above to add the button can also be used to remove a button. Example: Removing the image button would look like that (hook "editor_toolbar_filter")

PHP Code:
foreach ($toolbar AS &$row)
{
    if (
is_array($row))
    {
        foreach (
$row AS $id => $cmd)
        {
            if (
$cmd == 'Image')
            {
                
array_splice($row,$id,1);
                break;
            }    
        }
    }

Please note that removing the button will not render the attached bbcode inactive! However, you can use
PHP Code:
$this->config['_removePlugins'] .= ',celButtonDemo'
at hook editor_construct to remove plugins.



Restricting buttons by editor-mode or usergroup

You have everything available to you in the vB-universe to restrict access to your button, just extend the if-condition. You can restrict by usergroup, or you can show a button only in a certain editor

The editor mode can be queried via $this->editor_type in plugins at editor_toolbar_filter:
fe = full
qr = quick reply
qe = quick edit
cms_article

Example:

PHP Code:
foreach ($toolbar AS &$row)
{
    
// Condition extended: show new button only to Mods, Supermods, Admins and
    // only in Quick Reply Editor:
    
if (is_array($row) AND is_member_of($vbulletin->userinfo['usergroupid'], 567) AND $this->editor_type == 'qr' )
    {
        foreach (
$row AS $id => $cmd)
        {
            if (
$cmd == 'Quote')
            {
                
$row array_merge(array_slice($row0$id+1),  array('celButtonDemo'), array_slice($row$id+1));
                break;
            }
        }
    }


Have fun!
cellarius
.
.
Reply With Quote
  #22  
Old 02-03-2014, 09:32 PM
DaSpirit DaSpirit is offline
 
Join Date: Jan 2014
Location: Bulgaria
Posts: 3
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hello!

Great Plugin!

I want to just attach a link to my file that opens in new window. Would someone possiblly help me with that one?

Thanks a lot!
Reply With Quote
  #23  
Old 02-03-2014, 09:37 PM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No idea what you want to do, but this article is not about attaching anything to a file, but adding a button to the editor. Please open your own thread in the appropriate forums.
Reply With Quote
  #24  
Old 02-03-2014, 11:05 PM
DaSpirit DaSpirit is offline
 
Join Date: Jan 2014
Location: Bulgaria
Posts: 3
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by cellarius View Post
No idea what you want to do, but this article is not about attaching anything to a file, but adding a button to the editor. Please open your own thread in the appropriate forums.
Thanks for the answer!

I want to make the button link to a file in a new window. I have no idea about php and I am new to the vbulletin as well, thats why I am a bit disoriented, sorry about that. I think that what I want is simple, but I do not how to do it
Reply With Quote
  #25  
Old 02-04-2014, 06:21 AM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK. As I wrote in the article, this is not normally the place to ask for things that you want the button to do - this is for the addition of the button, not more.

Anyway: This has nothing to do with PHP - all of this is Javascript. And to open a page in a new window, you would add something like
Code:
window.open("http://www.your-page.com/goes/here.html");
to the exec part of my code. Like so:
Code:
exec: function(editor){
	window.open("http://www.your-page.com/goes/here.html");
}
For the additional syntax of window.open just google that command. There's plenty of sites explaining that.
Reply With Quote
  #26  
Old 02-05-2014, 01:56 AM
DaSpirit DaSpirit is offline
 
Join Date: Jan 2014
Location: Bulgaria
Posts: 3
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks a lot!
Reply With Quote
  #27  
Old 03-20-2014, 09:04 AM
Easy5s.net Easy5s.net is offline
 
Join Date: Jun 2011
Posts: 201
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How to move custom button to End Toolbar
Reply With Quote
  #28  
Old 03-20-2014, 09:37 AM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

By inserting it after the last button using the method described in the article.
Reply With Quote
2 благодарности(ей) от:
Easy5s.net, TheLastSuperman
  #29  
Old 08-18-2014, 10:06 AM
tini_lam tini_lam is offline
 
Join Date: Dec 2009
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

vbb 4.2.3 not work. member can't click button... but admin can
Reply With Quote
  #30  
Old 08-18-2014, 10:49 AM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

And? The article describes how you can restrict the button per usergroup. If you don't want that, don't implement it.
Reply With Quote
  #31  
Old 08-18-2014, 11:25 AM
tini_lam tini_lam is offline
 
Join Date: Dec 2009
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

i use code
Code:
foreach ($toolbar AS &$row) 
{ 
    if (is_array($row)) 
    { 
        foreach ($row AS $id => $cmd) 
        { 
            if ($cmd == 'Quote') 
            { 
                $row = array_merge(array_slice($row, 0, $id+1),  array('celButtonDemo'), array_slice($row, $id+1));
                break; 
            } 
        } 
    } 
}
course members see this button but can't click it, just administrator can click it to use
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:41 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04602 seconds
  • Memory Usage 2,340KB
  • Queries Executed 25 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (4)bbcode_code
  • (5)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (3)pagenav_pagelink
  • (11)post_thanks_box
  • (20)post_thanks_box_bit
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete