Hi I'm trying to build a custom accordion widget with the possibility to add multiples panels with WYSIWYG editor. My issue is that the editor content from the javascript is not saved in my widget $instance. Here's my code :
$(document).on("click", ".add-panel", function (e) {
var editor_id = content_id + uniqid() ;
var panel = $('<div class="accordion-panel">' +
'<input class="widefat panel-title" type="text" ' +
'name="' + title_name +'" />' +
'<textarea class="widefat panel-content" id="'+ editor_id +'" ' +
'name="' + content_name +'" ></textarea>' +
'</div>');
panel.appendTo('.accordion-panels');
wp.editor.initialize(editor_id, {
tinymce: {
wpautop: true,
toolbar1: 'bold italic underline strikethrough | bullist numlist | blockquote hr wp_more | alignleft aligncenter alignright | link unlink | fullscreen | wp_adv',
toolbar2: 'formatselect alignjustify forecolor | pastetext removeformat charmap | outdent indent | undo redo | wp_help'
},
quicktags: true,
mediaButtons: true
});
});
And the widget form function :
<script>
var content_name = "<?php echo $this->get_field_name( 'panels' ); ?>[content][]" ;
var title_name = "<?php echo $this->get_field_name( 'panels' ); ?>[title][]" ;
var content_id = "<?php echo $this->get_field_id( 'panels' ) . '-content-'; ?>" ;
</script>
<div class="accordion-panels">
<?php
if ( ! empty( $panels ) ) {
foreach ( $panels as $panel ) {
?>
<div class="accordion-panel">
<input class="widefat panel-title" type="text"
name="<?php echo $this->get_field_name( 'panels' ); ?>[title][]"
value="<?php echo esc_attr( $panel['title'] ); ?>" />
<?php
$content_id = $this->get_field_id( 'panels' ) . '-content-' . uniqid();
wp_editor( $panel['content'], $content_id );
?>
</div>
<?php
}
}
?>
</div>
<p>
<button class="add-panel button"><?php _e('Add Panel','a2c_widget') ; ?></button>
</p>
<?php
}
and my debug infos
[04-Jul-2023 14:39:59 UTC] Array
(
[title] => titre du widget
[panels] => Array
(
[title] => Array
(
[0] => titre
)
[content] => Array
(
[0] =>
)
)
Any ideas of what I'm doing wrong?
Here's my 'update' function in the widget
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance = $old_instance;
// Sanitize and save the widget title
if ( isset( $new_instance['title'] ) ) {
$instance['title'] = sanitize_text_field( $new_instance['title'] );
}
// Save the accordion panels
if ( isset( $new_instance['panels'] ) && is_array( $new_instance['panels'] ) ) {
$panels = array();
// Sanitize and save each panel title and content
foreach ( $new_instance['panels']['title'] as $index => $title ) {
$panel = array(
'title' => sanitize_text_field( $title ),
'content' => wp_kses_post( $new_instance['panels']['content'][ $index ] ),
);
// Only add the panel if both title and content are provided
if ( ! empty( $panel['title'] ) && ! empty( $panel['content'] ) ) {
$panels[] = $panel;
}
}
$instance['panels'] = $panels;
}
return $instance;
}
editor_id = content_id + uniqid()
, how would it know that ID on the PHP side? I also see that your save function is missing from your question, so if the problem is there then there's no way to know, can you add the full code?