Skip to content

Instantly share code, notes, and snippets.

@sorich87
Created September 28, 2010 08:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorich87/600616 to your computer and use it in GitHub Desktop.
Save sorich87/600616 to your computer and use it in GitHub Desktop.
Widget sample with tinymce wysiwyg editor
<?php
class Custom_Widget extends WP_Widget {
function Custom_Widget() {
...
}
function widget($args, $instance) {
...
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if ( isset($new_instance['description']) ) {
if ( current_user_can('unfiltered_html') ) {
$instance['description'] = $new_instance['description'];
} else {
$instance['description'] = wp_filter_post_kses($new_instance['description']);
}
}
return $instance;
}
function form($instance) {
$defaults = array(
'title' => 'RECOMMENDED READING',
'description' => '<p>Available for <u>Immediate</u> Download</p>'
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = esc_attr($instance['title']);
$description = $instance['description'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">
<i><strong><?php _e( 'Title' ); ?></strong></i><br>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('description'); ?>">
<i><strong><?php _e( 'Description' ); ?></strong></i>
</label><br/>
<textarea cols="53" rows="4" class="mceEditor" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>"><?php echo $description; ?></textarea>
<a href="#" onclick="showMCE('<?php echo $this->get_field_id('description'); ?>',this);return false;">Show WYSIWYG Editor</a>
</p>
<?php
}
}
// add javascript to widgets page header
add_action('admin_head-widgets.php', 'custom_tinymce');
function custom_tinymce() {
echo '<script type="text/javascript">
<!--
function showMCE(id,linkObj) {
if (tinyMCE.getInstanceById(id) == null) {
linkObj.innerHTML = "Hide WYSIWYG Editor";
tinyMCE.mode = "specific_textareas";
tinyMCE.execCommand("mceAddControl", false, id);
} else {
linkObj.innerHTML = "Show WYSIWYG Editor";
tinyMCE.execCommand("mceRemoveControl", false, id);
}
}
tinyMCE.init({
theme : "advanced",
mode : "none",
plugins : "style",
skin: "default",
dialog_type:"modal",
theme_advanced_buttons1: "fontselect,fontsizeselect,formatselect,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,justifyfull",
theme_advanced_buttons2: "bold,italic,underline,strikethrough,|,forecolor,styleprops,|,link,unlink,|,removeformat,charmap,blockquote,|,outdent,indent,|,undo,redo",
theme_advanced_buttons3: "",
theme_advanced_buttons4: "",
spellchecker_languages: "+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv",
theme_advanced_toolbar_location:"bottom",
theme_advanced_toolbar_align:"center",
theme_advanced_resizing:"1",
theme_advanced_resize_horizontal:"",
editor_selector: "mceEditor",
width : "200",
height : "200",
setup : function(ed){ed.onChange.add(function(ed){tinyMCE.triggerSave();});}
});
-->
</script>
';
}
?>
@bbubser
Copy link

bbubser commented Dec 24, 2010

What exactly do I need to do in order to install this code? I really need to get the tinyMCE functionality for my widgets. Please help!

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