WordPress: Load wp_editor() through AJAX/jQuery

Simply when we use wp_editor() it loads WordPress default visual editor. But when we are generating form or fields through AJAX call it doesn’t work. I was stuck in same problem in one of my projects. After some research I found easy fix to this problem. This might be helpful to you also. Below is my jQuery code which I have used to call form which has wp_editor()



It’s clearly seen in code that I have first removed instance of existing editor which is applied in ‘post_content’ class ID. You do not need to add this mceRemoveEditor line if you are calling it in textarea first time.

And in next line I’m initializing new instance on textarea with ID ‘post_content’. That’s It !

Method 2 for initializing wp_editor() (TinyMCE)

Use above code in your jQuery in AJAX success.

Shyam Makwana has written 21 articles

I am PHP developer with passion of exploring IT world for new technologies. I do projects in WordPress, Joomla, Magento, Drupal, Open Cart, Code Igniter, MVC and CorePHP. Checkout my about.me profile here http://about.me/shyammakwana to know more about me.

  • nelson

    fascinating !
    you saved me dude.
    nice article. 🙂

  • After two days of brain fry finally found this post. Thank you very much!!

    BTW, was you able to make work with media uploader button? It doesn’t work and shows js error.

    • Thank you @Sisir,

      Actually, I haven’t tried media uploader, I will check it in my time. And post an article after success.
      Stay tuned with my blog to get more articles, tweaks.

  • Konstantin

    Good day Shyam.
    Could you help me and explain in detail how to implement the case which is described above?
    Namely, what does mean string: data : { product_cat: category , action : ‘newproduct_form’},
    As I understand, I need to create some function newproduct_form?
    Sorry for stupid question, I am begginer and only start to study wordpress.
    Thank in advance!

    • Good day,

      yes you need to create function and use ‘add_action’ to register ‘newproduct_form’ action, then you can name function of anything.
      You are welcome.

      • Konstantin

        Hello Shyam!
        Thank you for feedback.
        Could you provide an example of php code?
        Thank in advance!

  • Nice one very good..!!!!