Всем привет, смог запустить нормально wp_editor в виджете с помощью
данного способа Но в итоге после нажатия сохранить у меня редактор рушится, пропадают все кнопки управления TinyMCE, а при нажатии на Visual выдает такое Uncaught TypeError: Cannot read property 'canvas' of undefined. Кто-нибудь сталкивался? Заранее спасибо
Код виджета
<?php
class Glenbrook_Market_Entry_Strategy extends WP_Widget {
public function __construct() {
parent::__construct(
'glenbrook_market_entry_strategy',
'Glenbrook Market Entry Strategy',
array(
'description' => '',
'customize_selective_refresh' => true
)
);
add_action( 'widgets_init', function() {
register_widget( 'Glenbrook_Market_Entry_Strategy' );
} );
}
public function widget( $args, $instance ) {
echo $args['before_widget'];
$title_block = $instance['title_block'];
$link_widget = $instance['link_widget'];
$output = '';
$output .= '<div class="information-block information-block_pine-green">';
$output .= '<a href="' . $link_widget . '" class="information-block__inner">';
$output .= '<div class="information-block__img">';
$output .= '<img src="' . get_template_directory_uri() . '/images/widget-1.jpg" alt="Information Block">';
$output .= '</div>';
$output .= '<p class="information-block__text information-block__text_color_white">Market <strong>Entry</strong> Strategy</p>';
$output .= '</a>';
$output .= '</div>';
echo $output;
echo $args['after_widget'];
}
public function form( $instance ) {
?>
<script>
jQuery( document ).ready( function( $ ) {
$( document ).on( 'load', '#widget-<?php echo $this->id; ?>-link_widget', function( e ) {
//var curId = $( '#widget-<?php echo $this->id; ?>-link_widget' );
} );
//var curId = $( '#widget-<?php echo $this->id; ?>-link_widget' ).attr( 'id' );
//var curId = curId.replace( /-savewidget/, '' );
//console.log( curId );
$( document ).on( 'click', '.widget-control-save', function() {
// grab the ID of the save button
var saveID = $( this ).attr( 'id' );
// grab the 'global' ID
var ID = saveID.replace( /-savewidget/, '' );
// create the ID for the random-number-input with global ID and input-ID
var numberID = ID + '-the_random_number';
// grab the value from input field
var randNum = $( '#' + numberID ).val();
// create the ID for the text tab
var textTab = ID + '-wp_editor_' + randNum + '-html';
// trigger a click
$( '#' + textTab ).click();
} );
} );
</script>
<?php
echo '<pre>';
print_r( $instance );
echo '</pre>';
$title_block = ! empty( $instance['title_block'] ) ? $instance['title_block'] : __( 'Market Entry Strategy', 'glenbrook' );
$link_widget = ! empty( $instance['link_widget'] ) ? $instance['link_widget'] : __( '#', 'glenbrook' );
$rand = rand( 0, 999 );
$ed_id = $this->get_field_id( 'wp_editor_' . $rand );
$ed_name = $this->get_field_name( 'wp_editor_' . $rand );
$content = $title_block;
$editor_id = $ed_id;
$settings = array(
'media_buttons' => false,
'textarea_rows' => 3,
'textarea_name' => $ed_name,
'teeny' => true,
);
echo 'Editor ID: ' . $ed_id . '<br>';
echo 'Editor Name: ' . $ed_name;
wp_editor( $content, $editor_id, $settings );
?>
<!-- <p>
<label for="<?php echo $this->get_field_id( 'title_block' ); ?>"><?php _e( 'Title Block:', 'glenbrook' ); ?></label>
<textarea name="<?php echo $this->get_field_name( 'title_block' ); ?>" id="<?php echo $this->get_field_id( 'title_block' ); ?>"></textarea>
</p> -->
<p>
<label for="<?php echo $this->get_field_id( 'link_widget' ); ?>"><?php _e( 'Link Widget:', 'glenbrook' ); ?></label>
<input type="text" name="<?php echo $this->get_field_name( 'link_widget' ); ?>" id="<?php echo $this->get_field_id( 'link_widget' ); ?>" class="widefat" value="<?php echo esc_attr( $link_widget ); ?>">
</p>
<?php if( $this->number == '__i__' ): ?>
<p style="color: red;"><strong>Before use the widget please save it!</strong></p>
<?php endif; ?>
<?php
printf(
'<input type="hidden" id="%s" name="%s" value="%d" />',
$this->get_field_id( 'the_random_number' ),
$this->get_field_name( 'the_random_number' ),
$rand
);
}
function update( $new_instance, $old_instance ) {
$rand = (int) $new_instance['the_random_number'];
$editor_content = $new_instance[ 'wp_editor_' . $rand ];
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance['title_block'] = $editor_content;
$instance['link_widget'] = strip_tags( $new_instance['link_widget'] );
//exit( var_dump( $old_instance ) );
//die( var_dump( $editor_content) );
return $instance;
}
}