1

I am using wp_editor in a custom meta box. It is saving the content to the post metadata. And displaying it in the meta box as it should.

The problem is when I am trying to display this content on the public-facing side of the website. It is displaying without the paragraphs. All other HTML that are part of the wp_editor content is displaying as they should.

My meta box code is as follows:

public static function as_bllp_create_call_to_action_meta_box( $post ){

        /* Retrieve call to action content from post meta */
        $as_bllp_call_to_action_para = get_post_meta( $post->ID, '_as_bllp_call_to_action_para', true );

        //The call to action section is going to be an HTML editor

        $settings = array(
            'textarea_name' => 'as_bllp_call_to_action_para',
            'textarea_rows' => 10,
            'media_buttons' => false,
            'wpautop' => false
        );

        echo '<div class="call-to-action-para">';
            wp_editor(htmlspecialchars_decode( $as_bllp_call_to_action_para ), 'as_bllp_call_to_action_editor1', $settings);
        echo '</div>';
    }

Saving the wp_editor data with the following code:

 /* Building args for allowed html for the content area filtering */
        $allowed_html_tags = array(
            'a' => array(
                'href' => array(),
                'title' => array(),
                'rel' => array(),
                'target' => array()
            ),
            'br' => array(),
            'p' => array(),
            'em' => array(),
            'strong' => array(),
            'h1' => array(),
            'h2' => array(),
            'h3' => array(),
            'h4' => array(),
            'h5' => array(),
            'ul' => array(),
            'ol' => array(),
            'li' => array()
        );

if ( !empty( $_POST[ 'as_bllp_call_to_action_para' ] ) ){

            /* Stripping received content of all HTML tags except for allowed tags */
            $html_content = wp_kses( $_POST[ 'as_bllp_call_to_action_para' ], $allowed_html_tags );

            /* Converting HTML to preserve their meaning */
            $as_bllp_call_to_action_para = htmlspecialchars( $html_content );

            /* Save the call to action content in post meta */
            update_post_meta( $post_id, '_as_bllp_call_to_action_para', $as_bllp_call_to_action_para );

        }

The above part works just fine. It saves and displays the content in the metabox as it should. With proper formatting.

But it doesn't add the paragraphs back when I display it on the custom post type single template on the front end of the site. Using this code:


/* Retrieve call to action content from post meta */
   $as_bllp_call_to_action_para = get_post_meta( $post_id, '_as_bllp_call_to_action_para', true );

/* echo the content for display */
   echo htmlspecialchars_decode( wp_kses( $as_bllp_call_to_action_para, $obj1->allowed_html_tags ) );

$obj1->allowed_html_tags is the same as above.

I thought 'wpautop' => false argument should tell WordPress that I want to preserve the paragraphs. As indicated here: https://codex.wordpress.org/Function_Reference/wp_editor

Any ideas what I need to do differently to get the paragraphs formatting the same as entered in the meta box?

Your Answer

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

Browse other questions tagged or ask your own question.