Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The Customizer Library currently supports these options:
* Range
* Textarea
* Select (Typography)
* Richedit (TinyMCE)

### Sections

Expand Down Expand Up @@ -254,6 +255,17 @@ $options['example-content'] = array(
);
~~~

### Richedit

~~~php
$options['example-richedit'] = array(
'id' => 'example-richedit',
'label' => __( 'Example richedit', 'textdomain' ),
'section' => $section,
'type' => 'richedit'
);
~~~

### Pass $options to Customizer Library

After all the options and sections are defined, load them with the Customizer Library:
Expand Down
68 changes: 68 additions & 0 deletions custom-controls/richedit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* Customize for Richedit, extend the WP customizer
*
* @package Customizer_Library
* @author Devin Price, The Theme Foundry, Oleksandr Kravchenko
*/

if ( ! class_exists( 'WP_Customize_Control' ) ) {
return null;
}


class WP_Customize_Richedit_Control extends WP_Customize_Control {
function __construct( $manager, $id, $options ) {
parent::__construct( $manager, $id, $options );

global $num_customizer_teenies_initiated;
$num_customizer_teenies_initiated = empty( $num_customizer_teenies_initiated )
? 1
: $num_customizer_teenies_initiated + 1;
}

function render_content() {
global $num_customizer_teenies_initiated, $num_customizer_teenies_rendered;
$num_customizer_teenies_rendered = empty( $num_customizer_teenies_rendered )
? 1
: $num_customizer_teenies_rendered + 1;

$value = $this->value();
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<input id="<?php echo $this->id ?>-link" class="wp-editor-area" type="hidden" <?php $this->link(); ?>
value="<?php echo esc_textarea( $value ); ?>">
<?php
wp_editor( $value, $this->id, [
'textarea_name' => $this->id,
'media_buttons' => false,
'drag_drop_upload' => false,
'teeny' => true,
'quicktags' => false,
'textarea_rows' => 5,
// MAKE SURE TINYMCE CHANGES ARE LINKED TO CUSTOMIZER
'tinymce' => [
'setup' => "function (editor) {
var cb = function () {
var linkInput = document.getElementById('$this->id-link')
linkInput.value = editor.getContent()
linkInput.dispatchEvent(new Event('change'))
}
editor.on('Change', cb)
editor.on('Undo', cb)
editor.on('Redo', cb)
editor.on('KeyUp', cb) // Remove this if it seems like an overkill
}"
]
] );
?>
</label>
<?php
// PRINT THEM ADMIN SCRIPTS AFTER LAST EDITOR
if ( $num_customizer_teenies_rendered == $num_customizer_teenies_initiated ) {
do_action( 'admin_print_footer_scripts' );
}
}
}
3 changes: 3 additions & 0 deletions customizer-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
require plugin_dir_path( __FILE__ ) . 'custom-controls/textarea.php';
}

// Richedit control
require plugin_dir_path( __FILE__ ) . 'custom-controls/richedit.php';

// Arbitrary content controls
require plugin_dir_path( __FILE__ ) . 'custom-controls/content.php';

Expand Down
28 changes: 27 additions & 1 deletion extensions/interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ function customizer_library_register( $wp_customize ) {
$loop = 0;

// Loops through each of the options
foreach ( $options as $option ) {
foreach ( $options as $id => $option ) {

if (!isset($option['id'])) {
$option['id'] = $id;
}

if (!isset($option['type'])) {
$option['type'] = 'text';
}

// Set blank description if one isn't set
if ( ! isset( $option['description'] ) ) {
Expand Down Expand Up @@ -79,6 +87,7 @@ function customizer_library_register( $wp_customize ) {
case 'checkbox':
case 'range':
case 'dropdown-pages':
case 'image_gallery':

$wp_customize->add_control(
$option['id'], $option
Expand Down Expand Up @@ -167,6 +176,23 @@ function customizer_library_register( $wp_customize ) {
);

break;
case 'richedit':

$wp_customize->add_control(
new WP_Customize_Richedit_Control(
$wp_customize, $option['id'], $option
)
);

break;

}

if (!empty($option['selector'])) {
$wp_customize->selective_refresh->add_partial( $option['id'], array(
'selector' => $option['selector'],
'container_inclusive' => isset($option['container_inclusive'])?$option['container_inclusive']:true,
) );

}
}
Expand Down