Jump to content

Script for updating all CKEditor wysiwyg fields simultaneously


thetuningspoon
 Share

Recommended Posts

Here's a script that I've been using on our sites and recently enhanced to also handle the conversion from TinyMCE to CKEditor in ProcessWire 2.5. It's a sort of "global config" for CKEditor, which allows you to change the settings for all CKEditor fields at once.

Note:

  • This one will automatically update all CKEditor fields unless they are explicitly excluded in the config ($excludedFields).
  • If you want it to change TinyMCE fields into CKEditor fields, make sure $replaceTinyMCE is set to true in the config section of the code.
<?php
include('./index.php');

// Config
$excludedFields = ""; // List of fields to exclude, separated by pipe (i.e. "body|sidebar")
$replaceTinyMCE = false; // true|false If true, converts any fields using TinyMCE to use CKEditor instead

// Change any fields using TinyMCE to use CKEditor instead
if($replaceTinyMCE) {
	foreach(wire("fields") as $field) {
		if($field->type == 'FieldtypeTextarea' && $field->inputfieldClass == 'InputfieldTinyMCE') {
			$field->inputfieldClass = 'InputfieldCKEditor';
			$field->save();
		}
	}
}

// Get all fields using CKEditor
$fields = new WireArray();
foreach(wire("fields") as $field) {
	if($field->type == 'FieldtypeTextarea' && $field->inputfieldClass == 'InputfieldCKEditor') {
		$fields->append($field);
	}
}

// Exclude any fields specified above
$fields = $fields->find("name!=$excludedFields");

// Apply settings
foreach($fields as $field) {

    $field->contentType = "1"; // 0="Unknown", 1="Markup/HTML"
    $field->rows = "15"; // The number of rows initially shown for this field.
    $field->toolbar = "
    	Styles
    	Undo, Redo
    	Bold, Italic, -, RemoveFormat
    	NumberedList, BulletedList, -, Blockquote
    	PWLink, Unlink, Anchor
    	PWImage, Table, HorizontalRule, SpecialChar
    	PasteText, PasteFromWord
    	Scayt, -, Sourcedialog
    	";
    $field->inlineMode = "0"; // 0="Regular Editor", 1="Inline Editor"
    $field->textformatters = array("TextformatterHannaCode","TextformatterVideoEmbed","TextformatterSmartypants");  // Accepts ordered array of module names
    $field->useACF = "1"; // 0="No", 1="Yes"
    $field->usePurifier = "1"; // 0="No", 1="Yes"
    $field->extraAllowedContent = "";
    $field->formatTags = "p;h2;h3;h4;h5;h6";
    $field->contentsCss = ""; // Path to your css file, i.e. "/site/templates/styles/contents.css"
    $field->stylesSet = ""; // Path to js file containing stylesSet, i.e. "mystyles:/site/modules/InputfieldCKEditor/mystyles.js"
    $field->customOptions = ""; // Custom Config Options
    $field->extraPlugins = "pwimage,pwlink,sourcedialog";
    $field->plugin_sourcedialog = ""; // Sourcedialog settings
    $field->removePlugins = "image";
    $field->save();

}
?>

CKEditor is now updated. Updated fields: <? foreach($fields as $field) echo $field.', ' ?>

To use the script, simply copy the above code into a file in the root directory of your ProcessWire installation, and name it something like "ckupdater.php". Edit the configuration settings under the "Apply settings" section how you'd like, (I've done my best to briefly document how these work--at least the more confusing ones).

To run the updater, just go to www.yoursitegoeshere.com/ckupdater.php. It will confirm the update and tell you which fields were effected.

Hope this helps someone out!

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...