2

I wrote a shortcode that displayed author profiles based on id. For example [user-profile id="1"] would display the profile block defined in user-profile.php for author 1. It worked (even with multiple instances on the same page).

function user_profile( $atts, $content = null ) {
    extract(shortcode_atts(array('id' => ''), $atts));
    include 'user-profile.php';
}

...except the shortcode output was showing up before other entry content regardless of its place in the code. To solve this I added this fix:

function user_profile( $atts, $content = null ) {
    extract(shortcode_atts(array('id' => ''), $atts));
    function get_user_profile() { include 'user-profile.php'; }
    ob_start();
    get_user_profile();
    $output_string = ob_get_contents();
    ob_end_clean();
    return $output_string;
}

...which worked to solve the positioning problem but broke multiple instances of the shortcode. [user-profile id="1"] works but [user-profile id="1"] [user-profile id="2"] breaks it—the page just stops loading at that point.

How can I modify this to allow multiple instances?

ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 1
    what error you getting if any? i suspect its due to defining a function in function, have you tried putting `function get_user_profile(){}` out of the user_profile function – Lawrence Cherone Jun 06 '11 at 18:25
  • please move your comment to answer section, and mark it as the right answer. – ariefbayu Jun 07 '11 at 04:52
  • @silent Thanks and thanks, yea I tried doing that before but it said I couldn't do it for 8 hrs. – ryanve Jun 10 '11 at 22:02

2 Answers2

0

Try this way:

[user-profile id="1"][/user-profile] [user-profile id="2"][/user-profile]
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
0

Problem solved! I updated the code in user-profile.php code so that it was all PHP and did not use any echos. Then I changed the shortcode function to:

function user_profile( $atts, $content = null ) {
    global $post;
    extract(shortcode_atts(array('id' => ''), $atts));
    include 'user-profile.php';
    return $user_hcard;
}
ryanve
  • 50,076
  • 30
  • 102
  • 137