Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

With the below two functions I can add a textarea custom user meta field named 'publications' to the user profile and save/update it:

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
  <textarea rows="10" cols="450" name="publications" id="publications"  class="regular-text" />
  <?php echo esc_attr( get_the_author_meta( 'publications', $user->ID ) ); ?></textarea>
<?php }


add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
  update_user_meta( $user_id, 'publications', $_POST['publications'] );
}

With the next function I can display the meta data from the field created above on an user page, with a shortcode [USER_META user_id=2 meta="publications"]:

add_shortcode('USER_META', 'user_meta_shortcode_handler');

function user_meta_shortcode_handler($atts,$content=null){
    return esc_html(get_user_meta($atts['user_id'], $atts['meta'], true));
}

The problem is that in the textarea I can format the text with simple paragraphs, but when it is displayed on front-end the paragraphs disappear and I see only a simple text flow. How to solve this?

I have Wordpress 3.5, Twenty Twelve Theme.

share|improve this question
    
as an aside, the WordPress Codex Shortcode API page states that shortcodes should be all lowercase, so you should use user_meta instead of USER_META. –  Simon Blackbourn Jan 21 '13 at 1:03

3 Answers 3

Have a look at both functions esc_attr() and esc_html()

Replace this one :

add_shortcode('USER_META', 'user_meta_shortcode_handler');

function user_meta_shortcode_handler($atts,$content=null){
    return esc_html(get_user_meta($atts['user_id'], $atts['meta'], true));
}

with this :

add_shortcode('USER_META', 'user_meta_shortcode_handler');

function user_meta_shortcode_handler($atts,$content=null){
    return esc_attr(get_user_meta($atts['user_id'], $atts['meta'], true));
}
share|improve this answer

You can use PHP's nl2br function which will convert newlines to <br> tags, or you could try something like this Stack Overflow answer.

share|improve this answer
up vote 0 down vote accepted

Finally I adopted this the second solution:

/* Display the selected user meta data with a shortcode */
add_shortcode('user_meta', 'user_meta_shortcode_handler');
/* usage: [user_meta user_id=1] */
function user_meta_shortcode_handler($atts,$content=null){ ?>
  <?php
    echo '<h3>Publications</h3>';
    echo wpautop(get_user_meta($atts['user_id'], 'publications', true));
  ?>
  <?php
}

For a good result paragraphs should be separated by a blank line.

EDIT: I updated my code to this (second solution):

add_shortcode('user_meta', 'user_meta_shortcode_handler');
/* usage: [user_meta user_id=1] */
function user_meta_shortcode_handler($atts,$content=null){ ?>
  <?php

    $text = "";

    //Explode the textareas rows to paragraphs
    function explode_paragraphs($text) {
      $text = explode("\n", $text); 
      foreach($text as $str) { echo '<p>'.$str.'</p>'; }
    }

    $text = esc_html(get_user_meta($atts['user_id'], 'publications', true));
    if(!empty($text)) { echo '<h3>Publications</h3>'; explode_paragraphs($text); }

  ?>
  <?php
}

Now, for a good result paragraphs should NOT be separated by a blank line.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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