0

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;
            
        }
4
  • I'm not sure how your widget is meant to know what the content is if you're giving it a unique ID on the javascript side using 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?
    – Tom J Nowell
    Commented Jul 4, 2023 at 15:31
  • Tanks for your answer. The content_id is from the widget side 'var content_id = "<?php echo $this->get_field_id( 'panels' ) . '-content-'; ?>" ;' I add a uniqid() on javascript side to allow multiple panels. But, you're right, there's a way to get the editor ID in the widget? Commented Jul 4, 2023 at 15:53
  • it was a comment/clarifying question, can you edit your question to include the save function for your widget? Your question is missing important information
    – Tom J Nowell
    Commented Jul 4, 2023 at 15:57
  • 1
    just edit my question with the save/update function Commented Jul 4, 2023 at 16:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.