Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Shortcode to show number of members

  • Avatar of angslycke
    angslycke
    Participant

    @angslycke

    Hi!

    I’m learning how to create shortcodes and am trying to create a shortcode to show the current number of members in my network. This is the code I’m using:

    `function show_number_of_members() {

    $count=bp_total_member_count();
    return $count;
    }

    function register_shortcodes() {
    add_shortcode(‘member_count’, ‘show_number_of_members’);
    }

    add_action( ‘init’, ‘register_shortcodes’);`

    Now, the code works when using the shortcode [member_count] but the number is displayed at the top of the page. This happens when you use “echo” in PHP because that gets parsed before the page content, but nothing changed after I switched to the “return” command. The code is placed in a “functions-custom.php” file where I keep my custom functions. I’m sure this is a simple mistake, can anyone here help me correct it? Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Avatar of meg@info
    meg@info
    Participant

    @megainfo

    bp_total_site_member_count() print the number of members

    bp_get_total_site_member_count() return the number of members

    :)

    Avatar of angslycke
    angslycke
    Participant

    @angslycke

    megainfo: Thanks for the tip, but the problem isn’t that I’m not getting the number of members to display but rather that the number is displayed at the top of the page and not where I put the shortcode in the page contents.

    Avatar of meg@info
    meg@info
    Participant

    @megainfo

    Change your function show_number_of_members()

    use bp_get_total_site_member_count() instead bp_total_member_count()

    bp_total_member_count() use echo to print the number , you need to return the number

    function show_number_of_members() {
    $count = bp_get_total_site_member_count();
    return $count;
    }

    PS : You can echo the number of members without creation an shortcode just use bp_total_site_member_count() [Without _get in the name of function].

    Avatar of angslycke
    angslycke
    Participant

    @angslycke

    megainfo: Thank you so much! I didn’t realise that bp_total_member_count() use echo to print the number, I thought that I could change that by using return in my code. The shortcode works now, excellent!

    Avatar of angslycke
    angslycke
    Participant

    @angslycke

    For anyone looking for the working code to create the shortcode [member_count] which can be inserted anywhere in pages or posts, here it is:

    `function show_number_of_members() {

    $count = bp_get_total_site_member_count();
    return $count;
    }

    function register_shortcodes() {
    add_shortcode(‘member_count’, ‘show_number_of_members’);
    }

    add_action( ‘init’, ‘register_shortcodes’);`

    Insert the above code in your functions.php or custom functions.php-file in your WordPress theme.

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.