Search code examples
phpwordpressredux-framework

wordpress how to add wp_editor in an array


I had a small problem in my wordpress code I need to show a wordpress wp_editor in my page where it has array of values.the values are defined like the following

    $fields[] = array(
        'name' => __('Class', 'my-theme'),
        'desc' => __('', 'my-theme'),
        'id' => 'class'.$n,
        'std' => ( ( isset($class_text[$n]['class']) ) ? $class_text[$n]['class'] : '' ),
        'type' => 'text');

When I define my wp_editor like the above array it doesn't display where I want. Instead all the editors displayed at the top before any content in all pages.

I tried like the following set of array for the editor:

    $fields[] = array(
        'name' => __('My Content', 'my-theme'),
        'id' => 'sectioncontent'.$n,
        'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
        'type' => wp_editor( '', 'sectioncontent'.$n ));

Attached the image of my problem:

enter image description here


Solution

  • Cause

    By default wp_editor prints the textarea that's why you cannot assign it to any variable or array.

    Solution

    you can use output buffering of php to get printed data in variable like this:

    ob_start(); // Start output buffer
    
    // Print the editor
    wp_editor( '', 'sectioncontent'.$n );
    
    // Store the printed data in $editor variable
    $editor = ob_get_clean();
    
    // And then you can assign that wp_editor to your array.
    
    $fields[] = array(
            'name' => __('My Content', 'my-theme'),
            'id' => 'sectioncontent'.$n,
            'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
            'type' => $editor); // <-- HERE