$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>'Add Media' button doesn't work when editor is initialized from javascript|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

'Add Media' button doesn't work when editor is initialized from javascript

matteradmin9PV0评论

I am trying to instantiate the wp_editor with javascript, which works nicely except the 'Add Media' button does not work. There are no errors, but clicking the button does nothing.

The simplest test case I can think of it to add the editor to the comments form, which I did via this code in a twentynineteen child theme with completely stock content (using the Hello World! post to test).

add_action('wp_enqueue_scripts', function() {
  if ( is_single() && comments_open() ) {
    wp_enqueue_editor();
  }
});

add_action('wp_footer', function() {
?>

<script type="text/javascript">
  var editorSettings = {
    mediaButtons: true,
    tinymce: true,
    quicktags: true
  };

  jQuery(function($){
    $(document).ready(function() {
      wp.editor.initialize( 'comment', editorSettings );
    });
  });

</script>

<?php
}, 100);

The mediaButtons setting does act to show/hide the button itself, but is not functional.

I have the problem on a 4.9.8 site (custom theme), both on the comments field and on other textareas I have tried. The simplified test (above) was 5.0, and the same symptom is exhibited with and without the Classic Editor plugin enabled.

I have tried printing the editor via wp_editor() in php, and the Add Media button works fine then (eg. for the comments field), but that is not an option other than for testing, as the textareas I need to use are added dynamically via javascript.

Does anyone have any ideas to try, or would anyone have a configuration where instantiating the editor via wp.editor.initialize() results in a working 'Add Media' button?

I am trying to instantiate the wp_editor with javascript, which works nicely except the 'Add Media' button does not work. There are no errors, but clicking the button does nothing.

The simplest test case I can think of it to add the editor to the comments form, which I did via this code in a twentynineteen child theme with completely stock content (using the Hello World! post to test).

add_action('wp_enqueue_scripts', function() {
  if ( is_single() && comments_open() ) {
    wp_enqueue_editor();
  }
});

add_action('wp_footer', function() {
?>

<script type="text/javascript">
  var editorSettings = {
    mediaButtons: true,
    tinymce: true,
    quicktags: true
  };

  jQuery(function($){
    $(document).ready(function() {
      wp.editor.initialize( 'comment', editorSettings );
    });
  });

</script>

<?php
}, 100);

The mediaButtons setting does act to show/hide the button itself, but is not functional.

I have the problem on a 4.9.8 site (custom theme), both on the comments field and on other textareas I have tried. The simplified test (above) was 5.0, and the same symptom is exhibited with and without the Classic Editor plugin enabled.

I have tried printing the editor via wp_editor() in php, and the Add Media button works fine then (eg. for the comments field), but that is not an option other than for testing, as the textareas I need to use are added dynamically via javascript.

Does anyone have any ideas to try, or would anyone have a configuration where instantiating the editor via wp.editor.initialize() results in a working 'Add Media' button?

Share Improve this question edited Dec 8, 2018 at 0:26 Jesse Norell asked Dec 8, 2018 at 0:15 Jesse NorellJesse Norell 937 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

The solution was to call wp_enqueue_media() along with wp_enqueue_editor().

Post a comment

comment list (0)

  1. No comments so far