1

I've been working with a plugin making various tweaks and adding additional functionality.

It uses custom post type to create a page with an associated download. In the admin area which allows you to create these new downloads, there is a box which allows you to add/edit a description for the download. This serves as the body content for the rendered page.

However, the admin page only had a standard for this which was created by the following code;

    public function display_sdm_description_meta_box($post) {  // Description metabox
_e('Add a description for this download item.', 'sdm_lang');
echo '<br /><br />';

$old_description = get_post_meta($post->ID, 'sdm_description', true);
?>
<textarea id="sdm_description" name="sdm_description" style="width:60%;height:100px;"><?php echo $old_description; ?></textarea>
<?php
wp_nonce_field('sdm_description_box_nonce', 'sdm_description_box_nonce_check');

}

I needed this to give the typical WP editor functionality so that the end user can create the page / body content much the same as on a normal page or post. As a result I was given the following code via another question I posted here on SO.

$old_description = get_post_meta($post->ID, 'sdm_description', true); 
$editor_id = 'sdm_description';
$settings = array( 'media_buttons' => false );

wp_editor( $old_description , $editor_id, $settings );

So I now have;

public function display_sdm_description_meta_box($post) {  // Description metabox
    _e('Add a description for this download item.', 'sdm_lang');
    echo '<br /><br />';

    $old_description = get_post_meta($post->ID, 'sdm_description', true);

    //adds TinyMCE capabilities
    $editor_id = 'sdm_description';
    $settings = array( 'media_buttons' => false );
    wp_editor( $old_description , $editor_id, $settings );

    ?>
    <!--<textarea id="sdm_description" name="sdm_description" style="width:60%;height:500px;"><?php echo $old_description; ?></textarea>-->
    <?php
    wp_nonce_field('sdm_description_box_nonce', 'sdm_description_box_nonce_check');
}

This render the tinyMCE editor in the admin section. However, if I hit enter to add a new line to the content this is only rendered as &nbsp; on the front end. Multiple 'enters' just result in more spaces, but never a new line or the equivalent of <br/>

Why isnt the content output as expected? How can it be fixed?

Thanks in advance!

--- EXTRA INFO

If i click the 'Tools' > 'Source' button on the TinyMCE editor I get a pop up windows showing the content I've created as being: <p>This is a test</p><p>Another line of testing!!!</p>

However on the front-end [eg the website] the source code shows as: `This is a test

Another line of testing!!!`

If I go into the DB, I can see that the data is stored literally like this:

This is a test

&nbsp;

&nbsp;

Another line of testing!!!
  • possible duplicate of Extra TinyMCE editor strips <p> and <br> tags? – Mark Kaplun Aug 14 '14 at 13:06
  • The answer given in the linked question does not seem to offer any solution to my needs. Ive tried it but it doesnt make any difference. – Phill Healey Aug 14 '14 at 13:38
  • 1
    yes, some time have passed and maybe the code there is stale, but the point is that what you describe in the "extra info" sounds like a lack of wpautop() formatting. If you look at the DB you will see that there are no "<p>" elements as part of the content and they are added before generating the HTML itself. – Mark Kaplun Aug 14 '14 at 13:48
  • @MarkKaplun Ive just checked the DB and updated my OP. There are no <p> tags in the db. Im not sure how Im supposed to add this wpautop() functionality. But it does sound like the probable issue. Any further help you could offer would be greatly appreciated. – Phill Healey Aug 14 '14 at 13:51
3

As mentioned in the comments, this is a lack of wp_autop() and other formatting functions that get applied to the the_content filter. The editor does not save <p> tags at all, but later on line-breaks are converted into paragraphs via wp_autop().

For meta content, I like to recreate the default filters. I do this because some plugins add things via the the_content filter, Once I had apply_filters('the_content', $your_meta_content); for all my meta fields and ended up with 20 sets of social sharing buttons.

/* 
 * Recreate the default filters on the_content
 * this will make it much easier to output the meta content with proper/expected formatting
*/
add_filter( 'meta_content', 'wptexturize'        );
add_filter( 'meta_content', 'convert_smilies'    );
add_filter( 'meta_content', 'convert_chars'      );
add_filter( 'meta_content', 'wpautop'            );
add_filter( 'meta_content', 'shortcode_unautop'  );
add_filter( 'meta_content', 'prepend_attachment' );
add_filter( 'meta_content', 'do_shortcode');

Then where you actually wish to display your new data, you should write the following:

$description = get_post_meta($post->ID, 'sdm_description', true);
echo apply_filters( 'meta_content', $description );
  • The last bit is definitely what I needed. Thanks again! – Phill Healey Aug 14 '14 at 14:31

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.