0

I have a plugin setting that uses wp_editor() to handle the input and will get echoed as part of a shortcode. Everything is working great on the admin side but when I echo the output on the client side, in the shortcode, the content does not echo with line breaks and paragraph breaks, like the_content().

Is there an existing method (or function) for echoing wp_editor() content from other sources, e.g. a plugin setting? Or do I need to parse and build paragraphs and line breaks manually (I feel like I'm reinventing the wheel here!)?

Example code for reference:

add_action('admin_init', 'my_plugin_settings');

function my_plugin_settings() {

    add_settings_section(
        'my_messages_section',
        'Messages',
        'Messages',
        'my_settings'
    );

    register_setting('my_settings', 'my_failure_message');

    add_settings_field(
        'my_failure_message',
        'Failure Message',
        'my_failure_message_input',
        'my_settings',
        'my_messages_section'
    );

}

function my_failure_message_input() {
    wp_editor(get_option('my_failure_message'), 'my_failure_message');
}

function my_plugin_shortcodes() {
    add_shortcode('my_shortcode', 'my_messages_shortcode');
}

add_action('init', 'my_plugin_shortcodes');

function my_messages_shortcode($atts = [], $content = NULL, $tag = '') {
    echo get_option('my_failure_message');
}

Desired output:

<p><strong>Failed.</strong></p>
<p>There was an error. Please try again later.</p>

Actual output (that folds into one line):

<strong>Failed.</strong>

There was an error. Please try again later.
0

the_content filter can be used to filter the content after it is retrieved from the database and before it is printed to the screen.
The Core filters on the_content are:

add_filter( 'the_content', 'wptexturize'        );  
add_filter( 'the_content', 'convert_smilies'    );
add_filter( 'the_content', 'convert_chars'      );
add_filter( 'the_content', 'wpautop'            );
add_filter( 'the_content', 'shortcode_unautop'  );
add_filter( 'the_content', 'prepend_attachment' );

Reference to some of above filters:
http://codex.wordpress.org/Function_Reference/wptexturize
http://codex.wordpress.org/Function_Reference/convert_smilies
http://codex.wordpress.org/Function_Reference/wpautop

In general; this should get your job done.

function my_messages_shortcode($atts = [], $content = NULL, $tag = '') {
    return apply_filters('the_content', get_option('my_failure_message') );
}
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Apr 23 at 9:46
  • The function name is apply_filters and not apply_filter.. – Sally CJ Apr 24 at 1:34
  • 1
    @SallyCJ Thanks for correction. – Qaisar Feroz Apr 24 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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