• Resolved appleaday

    (@appleaday)


    I created a custom post type and successfully added to it custom fields with the metabox mechanism. I was happy I succeded in getting the custom fields with just code, no extra plugin added for this. But a nightmare started when I tried to connect the tinyMCE editor to something that at the beginning was a textarea. The display of the editor looks messed up and mixed together with the controls to move the box. I read somewhere tinyMCE instances don’t get along with metaboxes and so far I could find no fix for my issue.
    I know metaboxes are just an option to add custom fields to custom post types, but I really don’t feel like resorting to an extra plugin, I’d like to keep on coding as I did so far. I momentarily got lost in searching for a guide to add non-metabox fields with just coding. I hope I won’t have to give up, but I mean to complete my task within next Friday, so I’ve gotta find a right balance between the pleasure of DIY and the need to get rid of these WordPress problems in a reasonable time. I usually deal with Drupal but this time I had to show I could however work it out with WordPress, so, dealing with this supposed advanced topic was kind of a challenge to me.
    Would you please lend me a helping hand?

    Thanks in advance!

    Andrea

    • This topic was modified 3 years, 6 months ago by appleaday.
Viewing 5 replies - 1 through 5 (of 5 total)
  • I read somewhere tinyMCE instances don’t get along with metaboxes and so far I could find no fix for my issue

    Yes I have had that happen to me. Although I do have an instance where is seems to work OK, although I’m not exactly sure why. I’ll look at the code later and try and see what makes it play nicely.

    OK The solution is not to use a meta box. It is quite well documented in wp_editor() https://developer.www.ads-software.com/reference/functions/wp_editor/

    Unless you just use quicktags ( text ) the tinyMCE does not like being moved. It normally works OK until it is moved, then it goes haywire.

    So the solution is to fix the output in the DOM.

    For a CPT to fix the output position, don’t use metaboxes (moveable) but the action 'edit_form_advanced'

    You can then position initially the other metaboxes above or below with context below set to advanced above set to normal

    So example

    add_action('edit_form_advanced', array( $this, 'inner_custom_editor' ) );
    public function inner_custom_editor( $post ) {
                   if ( 'myCPT' !== $post->post_type) {
    		    return;
                    }
    		echo '<p>some markup</p>;
    		wp_editor( $mymeta, 'my_id', $settings );
                    echo '<p>some markup</p>;
    	}
    • This reply was modified 3 years, 6 months ago by Alan Fuller. Reason: Added gateway check so only applies to specific pots type
    Thread Starter appleaday

    (@appleaday)

    I did appreciate your hint, thanks!

    Though I didn’t properly get the way you added the edit_form_advanced action.

    I eventually used something like what follows.

    add_action('edit_form_after_title', 'custom_tinymce_editor');
    function custom_tinymce_editor () {
    
       global $post;
       if ( 'my_custom_post_type' !== $post->post_type) {
        return;
       }
       $custom = get_post_custom( $post->id );
       $desc = $custom[ "_desc" ][ 0 ];
    
       $textarea_id = 'desc';
       echo '<p>something to display</p>';
       $wp_editor_settings = array(
                                    'tinymce' => true,
                                    'textarea_name' => '_desc',
                                    'quicktags' => false,
                                    'media_buttons' => true
       );
       wp_editor($desc, $textarea_id, $wp_editor_settings);
       echo '<p>still something to display</p>';
    }
    

    This way I avoided having the TinyMCE code mixed with the code of a meta box (it could still work, but it gave me an unpleasant idea of disorder).

    I’m still puzzled, though, because I’m still using a code that mentions meta

    update_post_meta( $post->ID, "_desc", $_POST[ "_desc" ]);

    But desc is no more handled by a meta box… should I mind that?

    Thanks!

    Andrea

    Your code is fine, my code was just part of a class thats why the array($this wa s there.

    I would have done it slightly differntly. You don’t need the global $post as the hook passes post as a parameter
    i.e. function custom_tinymce_editor ($post) {

    And I would have used get_post_meta rather than get_post_custom
    i.e. $desc = get_post_meta($post->id,'_desc',true);

    Also you need to sanitize $_POST[ “_desc” ]

    e.g. update_post_meta( $post->ID, '_desc', wp_kses_post($_POST[ '_desc' ]));

    There is no relationship between meta-boxes ( a display element ) and post-meta ( a custom field) as such, so nothing to be concerned about

    • This reply was modified 3 years, 6 months ago by Alan Fuller.
    Thread Starter appleaday

    (@appleaday)

    Many thanks, Alan!

    Andrea

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Failed to use tinyMCE within a metabox’ is closed to new replies.