最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I render content from a wp_editor in a plugin setting field?

programmeradmin2浏览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.

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.
Share Improve this question asked Apr 23, 2019 at 5:56 WarwickWarwick 2732 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/Function_Reference/wptexturize
http://codex.wordpress/Function_Reference/convert_smilies
http://codex.wordpress/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') );
}
发布评论

评论列表(0)

  1. 暂无评论