WP Editor - wp_editor() is not showing properly on ajax call

Omer

I'm creating a WordPress plugin. It has a functionality to show the editor when adding a product through AJAX but the the editor is not showing properly.

User can add as many products as he want so keep in mind that there will be more than one wp_editor()

Please refer to the attached screenshot:

enter image description here

I have used the following code:

PHP

public function add_product() {

        // Get product id
        $prod_id = filter_input(INPUT_POST, 'pid');

        // WordPress WYSIWYG Editor
        wp_editor("Test Content", "textarea" . $prod_id, array('textarea_name' => 'text'));
        wp_die();
}

JQUERY

$('.add-prod').live('click', function () {

        var prod_id = $('.prod-id').val();
        var data = {
            action: 'add_prod',
            pid: prod_id
        };
        $('#update-msg').show();

        $.post(ajaxurl, data, function (result) {
            $('#the-list').append(result);
            $('#update-msg').hide();
        });

        return false;
});

I have used a solution from the internet but its partially working not fully. Used the following code:

PHP

wp_editor($product->prod_desc, $textarea_id, array('textarea_name' => 'text'));
\_WP_Editors::enqueue_scripts();
print_footer_scripts();
\_WP_Editors::editor_js();

JQUERY

var eid = '#item-list';
switchEditors.go(eid, 'tmce')
quicktags({id: eid});
//init tinymce
tinyMCEPreInit.mceInit[eid]['elements'] = eid;
tinyMCEPreInit.mceInit[eid]['body_class'] = eid;
tinyMCEPreInit.mceInit[eid]['succesful'] = false;
tinymce.init(tinyMCEPreInit.mceInit[eid]);

And the code above does this:

enter image description here

Alpesh Panchal

Obviously wp_editor won't show up as you're making ajax call which only returns ajax response but not wp editor which is built by javascript on that page. In short, ajax call can get server side text response but not javascript editor which is built on client side and needs javascript processor to process.

Following can be suedo example of what can be done to make editor working.

  1. just below "add product" button, from where ajax call is being made, print a editor using php code and set its display to none so editor doesn't appear on page.

e.g.

<div class="wp-editor-wrapper" style="display: none;">
     <?php wp_editor("Test Content", "textarea" . $prod_id, array('textarea_name' => 'text')); ?>
</div>
  1. php function for ajax response should only return text content only. Not editor itself and it should look like this.

    public function add_product() {

        // Get product id
        $prod_id = filter_input(INPUT_POST, 'pid');
    
        // if $prod_id is used here, use it to get content
        echo "Test Content";
        wp_die();
    

    }

  2. when response is received of text content, using jQuery, create a clone of "wp-editor-wrapper" div and add it in place of textarea and set its content from ajax response.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

load wordpress wp_editor dynamically (ajax)

From Dev

How to properly use wp_editor() from frontend

From Dev

Google maps <iframe> code not showing properly from wp text editor

From Dev

Replacing a textarea with WordPress TinyMCE wp_editor()

From Dev

wordpress how to add wp_editor in an array

From Dev

Include TinyMCE plugins with Wordpress wp_editor?

From Dev

Prevent wp_editor to alter initial content

From Dev

Using wp_editor output correctly

From Dev

Replacing a textarea with WordPress TinyMCE wp_editor()

From Dev

Error in buddypress when using wp_editor

From Dev

How to detect changes in wp_editor?

From Dev

wp_editor: How to load wp_editor on demand using jQuery?

From Dev

WordPress wp_editor removes html tags - how to stop it?

From Dev

<p></p> missing with wp_editor( get_option() );

From Dev

using wp_editor to edit post title on the front end

From Dev

WP link not showing properly on linkedin

From Dev

Call async method properly on WP8.1

From Dev

AJAX Call - Does not recognize WP_Query

From Dev

Tiles not showing up properly in WP8.1 Silverlight app

From Dev

implement wp-editor in a wordpress page

From Dev

How to make an AJAX call in WP plugin on the front end

From Dev

How to make an AJAX call in WP plugin on the front end

From Dev

ajax in wp front end

From Dev

showing no posts on wp_query

From Dev

Css not working properly in codemirror editor

From Dev

Distance lines are not showing in interface editor

From Dev

tinyMCE the textbox is showing beneath the editor

From Dev

tinymce editor content value not showing

From Dev

froala unordered list not showing in editor

Related Related

HotTag

Archive