0

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.

1

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));
}
| improve this answer | |
0

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

| improve this answer | |
0

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.

| improve this answer | |

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.