• Resolved monitor

    (@oomskaap)


    When we used Cubepoints, I echoe’d

    “Welcome back USERNAME. You are 1 of 2 members in our community.
    You have 0 Store Credits. ” to new members, and guest would have another message.

    I would greatly appreciate it if you could help us to reproduce this cubepoints code over to myCRED’s code.

    I see myCRED has a %user_name% hook? that great so I can get rid of the native wordpress code in the below function and use yours all together.

    You could use this as an example in your documentation, its’ quite a nice feature that beginners would like to paste in their theme.

    The template code to output this in my theme is:

    <?php global $user_ID; if( $user_ID ) : ?>
    <?php if( current_user_can('level_0') ) : ?>
    Welcome back <strong><?php global $current_user; get_currentuserinfo(); echo ($current_user->user_login); ?>
    .<?php else : ?>
    <?php endif; ?>
    <?php endif; ?>
    </strong>
    You are 1 of <strong><?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
    echo $users."</strong> members in our community."; ?>
    You have <?php if(function_exists('cp_displayPoints') && is_user_logged_in() ) { cp_displayPoints($user->ID); echo ' Store Credit points!';
    } else {
        echo '0 Store Credits. Register and get 100 Store Credits for free!';
    }?>
    <strong>

    Thanks again.

    https://www.ads-software.com/extend/plugins/mycred/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author myCred

    (@designbymerovingi)

    Hey oonskap.

    My first thought was “Cant the myCRED Balance widget do this already?” and looking at the code, the answer is almost.

    You see the myCRED Balance widget shows a users balance and optionally a message of your choosing for guests. However it does not accept any user related template tags in it’s current version. I feel however that allowing user related template tags in the “Layout:” filed for this widget is a good idea so I will include this in the next update.

    But to get it to work before that, you could adjust the mycred-widgets.php file (located in mycred/includes/ folder) on line 56 from this:

    $layout = $mycred->template_tags_amount( $instance['cred_format'], $balance );

    to this:

    $layout = $mycred->template_tags_amount( $instance['cred_format'], $balance );
    global $wpdb;
    $num_members = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
    $layout = str_replace( '%num_members%', $num_members, $layout );
    $layout = $mycred->template_tags_user( $layout, $user_id );

    Dont worry about making changes in the plugin files! I will implement this in the next version of myCRED.

    This way, you could change the widgets “Layout:” filed to:

    Welcome back %display_name%. You are 1 of %num_members% members in our community. You have %cred_f% Store Credit Points!

    Notice that I created the %num_members% tempalate tag for you to use here. This template tag would only work in this particular widget filed.

    Then you tick “Show message when not logged in”
    and enter the message:

    0 Store Credits. Register and get 100 Store Credits for free!

    In case you do not want to use the widget I have included a function below that goes into your functions.php file and then called anywhere in your theme:

    function my_custom_mycred_greet() {
    	if ( is_user_logged_in() && class_exists( 'myCRED_Core' ) ) {
    		global $current_user, $wpdb;
    		get_currentuserinfo();
    		// Load myCRED
    		$mycred = mycred_get_settings();
    		// Count members
    		$num_members = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
    		// Welcome text
    		echo 'Welcome back ' . $current_user->user_login . '. You are 1 of <strong>' . $num_members . '</strong> members in our community. ';
    		echo 'You have ' . $mycred->get_users_cred( get_current_user_id() ) . ' Store Credits.';
    	}
    	else {
    		echo '0 Store Credits. Register and get 100 Store Credits for free!';
    	}
    }

    Alternatively, if you are using a prefix / suffix with your points:

    function my_custom_mycred_greet() {
    	if ( is_user_logged_in() && class_exists( 'myCRED_Core' ) ) {
    		global $current_user, $wpdb;
    		get_currentuserinfo();
    		// Load myCRED
    		$mycred = mycred_get_settings();
    		// Balance
    		$balance = $mycred->get_users_cred( get_current_user_id() );
    		// Count members
    		$num_members = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
    		// Welcome text
    		echo 'Welcome back ' . $current_user->user_login . '. You are 1 of <strong>' . $num_members . '</strong> members in our community. ';
    		// Balance formated with prefix/suffix
    		echo 'You have ' . $mycred->format_creds( $balance ) . ' Store Credits.';
    	}
    	else {
    		echo '0 Store Credits. Register and get 100 Store Credits for free!';
    	}
    }

    Then you just call this function anywhere in your theme:

    <?php my_custom_mycred_greet(); ?>

    There are 4 types of myCRED Template tags:

    – Amount related
    – User related
    – Post related and
    – Comment related

    Template tags are used mainly for the log but also by myCRED widgets to help you customize layouts. It’s pretty easy to implement support for template tags in your code. The functions that handles template tags are located in the myCRED_Settings class and using the mycred_get_settings function you could access these methods directly.

    Example:

    $my_string = 'Hello %display_name%!';
    $mycred = mycred_get_settings();
    echo $mycred->template_tags_user( $my_string, get_current_user_id() );

    There is one method for each template tag type and all of them require minimum two arguments. First the string that contains the template tags and second a reference id. For user related template tags the reference id is the users id, for posts it’s the post id, for comments it’s the comment id and for amounts it is the actual amount (unformatted).

    Let me know if you need anything else.

    Thread Starter monitor

    (@oomskaap)

    That code worked like a charm. I want the greeting to appear in a very top bar of my page, which is not a sidebar, so a widget won’t do.

    <?php my_custom_mycred_greet(); ?> works perfectly.

    Thanks again.

    Plugin Author myCred

    (@designbymerovingi)

    No worries, I figured as much but I thought I’ll include both solutions just in case you or anyone else might need it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to echo "Welcome %user_name%, you have XXXX points"’ is closed to new replies.