0

I need to add TinyMCE to descriptions of images. For it, I'm using this code in my functions.php:

function add_tiny_mce_hook() {      
if (function_exists('wp_tiny_mce')){ 
    wp_tiny_mce(false, 
        array(
            'remove_linebreaks' => false, 
            'mode'              => 'textareas', 
            'editor_selector'   => 'my-tinymce-class')
        );
}}
function alternate_tiny_mce_method($fields, $post) {
if (isset($fields['post_content'])) {
    $value = $fields['post_content']['value'];
    $label = $fields['post_content']['label'];
    $name = 'post_content';
    $fields[$name] = array(
        'label' => $label,
        'input' => 'html',
        'html' => '<textarea name="attachments['.$post->ID.']['.$name.']" id="'.$name.'-'.$post->ID.'" class="my-tinymce-class">'.nl2br($value).'</textarea>'
    );
}
return $fields;}
add_action('admin_head', 'add_tiny_mce_hook');
add_filter('attachment_fields_to_edit', 'alternate_tiny_mce_method', 10, 2);

On the Edit Media page works fine (wp-admin/media.php?attachment_id=XXX&action=edit). But, I have a problem after opening "Add an Image" modal window, and switching to the "Gallery" tab, from "Edit Post". I can't add a link via TinyMCE editor. Every time I click Add Link from the pop-up modal nothing happens.

I see the problem in gallery.js (gallery.dev.js; wp-admin/js/gallery.js; WordPress loads this JS). There are 3 lines that do me the trouble: .... tinymce = w.tinymce; tinyMCE = w.tinyMCE; t.editor = tinymce.EditorManager.activeEditor; ....

When I drop "tinymce = w.tinymce;" and "tinyMCE = w.tinyMCE;" and replace "tinymce.EditorManager.activeEditor;" with "w.tinymce.EditorManager.activeEditor" - works fine. But, I don't want to do a core mod to WordPress

My WordPress version is 3.2.1

Any help, please.

2 Answers 2

1

i had the same "problem" just two days ago. Finally it worked somehow but wasn't reliable at all. Switching editors just didn't work occasionally and as said, wasn't a reliable solution.

The only reason to use tinyMCE here was the ability to add links to attachments. I took an other route and came to this solution:

http://www.screenr.com/xYas

I've done this for somebody else, so this is just a early demo, quick & dirty. The final script is a bit prettier. If you're interested, please send a short mail to kai@[->myusername<-].de or find me at twitter @ungestaltbar

0

Try increasing priority for the attachment_fields_to_edit filter:

add_filter('attachment_fields_to_edit', 'alternate_tiny_mce_method', 15, 2);
1
  • An explanation would be useful.
    – fuxia
    Commented Nov 5, 2012 at 3:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.