2

hey guys, is it possible to format text in a custom field input box automatically with paragraphs?

e.g. like the normal text-widget that has the option to say "auto-add paragraphs" when there is a linebreak.

I just want my blogauthors to spare typing
at the end of every line in a custom field!

is there a way to do so?

update:

<?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post();
                    $sidebar_title = get_post_meta($post->ID, 'sidebar-title', $single = true);
                    $sidebar_text = get_post_meta($post->ID, 'sidebar-text', $single = true); ?>

                    <?php if ( $sidebar_title !== '' && $sidebar_text !== '' ) { ?>
                        <li class="widget-container widget-light-blue custom">  
                            <h3 class="widget-title"><?php echo wpautop($sidebar_title, $br); ?></h3>
                            <?php echo wpautop($sidebar_text, $br); ?>
                        </li>
                    <?php } ?>

                <?php endwhile; ?>
            <?php endif; ?>
3

Why don't you use apply_filters( 'the_content', $var ); when outputting your custom field? You don't really want to save the extra paragraphs, otherwise you'll end up seeing them when editing the custom field. This is not what happens with WordPress.

If you're not happy with what the_content does (it does a lot of things including wpautop) then create a custom filter like this:

// Assuming $var is your custom field value
add_filter( 'my_custom_filter', 'wpautop' );
echo apply_filters( 'my_custom_filter', $var );

Cheers!

1

Can you pass the user-input data through wpautop() (Codex ref) when you save those data to the DB?

EDIT:

You should be doing something like the following, to validate/sanitize the user input:

/* Function to validate, sanitize, and save post metadata. */
function mytheme_save_custom_metadata(){

  global $post;

  $valid_custom_field_input = wp_kses( wpautop( $_POST['custom_meta_field'] ) );

  update_post_meta($post->ID, 'custom_meta_field', $valid_custom_field_input );
}
/* Add our function to the save_post hook. */
add_action('save_post', 'mytheme_save_custom_metadata');

Notice that I put wrapped the user input in wpautop() before passing through wp_kses().

10
  • really nice idea, however won't work: <?php echo wpautop($sidebar_text, $br); ?> Apr 28 '11 at 17:08
  • That's not what I suggested. You're trying to echo the output using wpautop(); I suggested passing the userdata through wpautop() upon save. Apr 28 '11 at 17:22
  • Question: what is the code for your function to save the userdata to the database? Apr 28 '11 at 17:22
  • 1
    @Chip- do you actually want to save data with the html though? I think you just want to pass it through when you output it to the front end template.
    – Milo
    Apr 28 '11 at 17:32
  • 1
    @Chip- WordPress only uses it internally when data is output to template, I figured that was the proper way. I try not to pollute my data with markup when I can avoid it. @mathiregister- in your wpautop function, $br should be a boolean or integer: wpautop($sidebar_title, 1);
    – Milo
    Apr 28 '11 at 19: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.