WordPress.org

Forums

How to echo the results of the wp_editor (15 posts)

  1. dshalgishira
    Member
    Posted 2 years ago #

    Hi,
    I need a second textarea to the posts/pages. So I used the wp_editor but I don't find how I echo the content of it to the site in the loop. what am i missing here?
    This is the code to ass the wp_editor:

    /* Second Editor tinyMCE */
    add_action( 'edit_form_advanced', 'my_second_editor' );
    add_action( 'edit_post_form', 'my_second_editor' );
    function my_second_editor() {
    	$args = array(
    		'textarea_rows' => 5,
    		'media_buttons' => true
    	);
    	// get and set $content somehow...
    	wp_editor( '', 'my_second_editor', $args );
    }

    Any help will be much appreciated :)

  2. Steven Jones
    Member
    Posted 2 years ago #

    You'd be better of using a plugin like Advanced Custom Fields which lets you add custom meta fields (by reusing WP_Editor).

    It also handles the saving of the data too and you can use get_post_meta in your theme.

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  3. dshalgishira
    Member
    Posted 2 years ago #

    Thank you, I will try it now.

  4. gicolek
    Member
    Posted 2 years ago #

    Hey dshalgishira,

    First thing is that the most reasonable way of adding extra input fields to a post or page is via Custom Meta Boxes:

    see here

    This topic is quite enormous so if you're confident with a bit more advanced programming and quite a lot of reading then go ahead - this would only improve your knowledge.

    If your not confident with that then use ACF - I've been using this plugin for quite some time already and it's one of the best plugins out there (yet still it involves some programming on the Front End).

    Anyway please remember about the data validation http://codex.wordpress.org/Data_Validation whenever adding custom user input (even if it's for your own sake - that's a good practice as well)

    Likewise to output a properly formatted content filtered via wp editor please do the following:

    <?php
    echo apply_filters( 'the_content', $variable_name );
    ?>

    Where '$variable_name' would be the name of a variable which had the contents stored.

    This solution would run a lot of different filters, outputting the properly formatted text.

    Hopefully I haven't discouraged you. It is a good practice to avoid filtering the data as you did above, whenever there's a semi-automatic solution (like the MetaBoxes API).

  5. dshalgishira
    Member
    Posted 2 years ago #

    I did use the ACF plugin and it is a good solution.
    BUT, if you know how to make a custom field to be a WYSIWYG editor I will be happy to know :)

  6. gicolek
    Member
    Posted 2 years ago #

    I'll post you with a full code snippet on how to do it tomorrow :) For now please get familiar with the MetaBoxes above.

  7. gicolek
    Member
    Posted 2 years ago #

    Hey, I'm extremely sorry for the delay, I've had a crazy X-Mas parties ... I can still feel the Polish Vodka within my veins.

    Anyway here's a sample code on how to add a Custom WP Editor Meta Box. It's a github's gist: https://gist.github.com/4392890

    There are two sections

    1. Backend (put the code into functions.php file)
    2. Front End (put the code into the loop in single.php file)

    I have created a code that allows adding a WP editor meta box, to the post edit screen (so it's connected with a post in that case).

    Each section of the code is commented and I have provided sample to-be-followed links for you.

    If you'd like to have a more in-depth understanding of Custom Meta Boxes creation you have to carefully study these. There's a lot of reading provided on the codex already so there's no sense to duplicate the effort.

    In case you had any comments to any line feel free to ask.

  8. lilike
    Member
    Posted 2 years ago #

    @gicolek: Thank you very much for the sample code!

  9. gicolek
    Member
    Posted 2 years ago #

    @lilike enjoy :)

  10. lilike
    Member
    Posted 2 years ago #

    Perhaps have you got any suggestions on adding tinyMCE dynamically - via AJAX - into that metabox?

  11. gicolek
    Member
    Posted 2 years ago #

    Hey @lilike, sorry for a late reply.

    Why would you want to add TinyMCE to that editor? It's already bounded with it (and you can easily switch to the HTML view).

    Perhaps you were thinking on extending the editor with extra options? If yes, then check the following plugin: http://wordpress.org/extend/plugins/tinymce-advanced/

    It's also possible to do couple of things like that manually without the need of installing a plugin, although it requires some coding.

    Anyway if you were thinking about something totally different, then you'd have to refer to tinyMCE documentation found @ http://www.tinymce.com/wiki.php

    Should you have any questions, go ahead :)

  12. lilike
    Member
    Posted 2 years ago #

    Thanks for the links, finally found the clues to the solution: after the AJAX call I need to add controls to the tinyMCE editor:
    tinymce.execCommand( 'mceAddControl', false, 'editor_id' );

  13. gicolek
    Member
    Posted 2 years ago #

    Hey,

    That's the right path :-)

    I guess you're wanting to load tinyMCE dynamically based on a checkbox or a dialog perhaps, right? Unfortunately I haven't been doing that yet, so I'm not going to be much of a help.

    Anyway you'll probably be able to use parts of the code I prepared, especially getting the editors value and using it via $_POST variable.
    Make sure to check if the data was set and validate user privileges afterwards.

    Good luck,
    Rafal

  14. lilike
    Member
    Posted 2 years ago #

    Yes, I would like to load it by a checkbox control. Thank you again, setting the $_POST variable was a little bit tricky.

  15. typhon
    Member
    Posted 2 years ago #

    @gicolek: Thanks a lot! Your code example helped me enormously. I wanted an input box for sidebar content and I had been researching if I could do it with metaboxes when I found your great example and explanation.

    Thanks to Steven Jones for pointing out the ACF plugin. I'll probably try it if I ever need something more complex.

Topic Closed

This topic has been closed to new replies.

About this Topic