• Resolved monitor

    (@oomskaap)


    I’m on the edge of migrating all our sites away from Cubepoints (slow development) over to yours. However, one of my concerns is that we have a piece of code on a page, and whenever that page is visited, it gives out 300 points. Can you give me a substitute code for your plugin?

    if( function_exists('cp_alterPoints') && is_user_logged_in() ){
    cp_points('type_of_activity', cp_currentUser(), 300, 'optional_data');
    add_action('cp_logs_description','cp_type_of_activity', 10, 4);
    function cp_type_of_activity($type,$uid,$points,$data){
    if($type != 'type_of_activity') { return; }
    echo 'You got points for this';
    }

    Thanks!

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

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

    (@designbymerovingi)

    Hey oomskaap

    Thank you for your question. The following code does exactly the same thing as your code assuming it is placed in your page template file:

    if ( class_exists( 'myCRED_Core' ) && is_user_logged_in() && !mycred_exclude_user( get_current_user_id() ) ) {
    	global $post;
    	mycred_add( 'points_for_page_view', get_current_user_id(), 300, 'Points for viewing page', $post->ID );
    }

    The above code translates into: If myCRED is activated, the current user is logged in and the user is not excluded, add 300 points to the current user with the reference “points_for_page_view” and the page ID as reference id. The reference is used by the log to allow users (and you) to filter results according to reference. myCRED will replace underscores with whitespaces and return each word in upper case so make sure the reference is explanatory.

    You can find more information about the mycred_add function in our Codex. In this above example, the log will be populated with “Points for viewing page.”. If you want to take advantage of the template tags to for example include the page title you would do it as follows:

    if ( class_exists( 'myCRED_Core' ) && is_user_logged_in() && !mycred_exclude_user( get_current_user_id() ) ) {
    	global $post;
    	mycred_add( 'points_for_page_view', get_current_user_id(), 300, 'Points for viewing the page %post_title%', $post->ID, array( 'ref_type' => 'post' ) );
    }

    As you can see the differernce here beside us using the %post_title% template tag is to pass on a sixth variable to mycred_add which is an array defining that the template tag is for posts (no matter post type). This way when myCRED presents your log, the template tag is replaced with the post title.

    I am using class_exists in the above codes but of course a function_exists check for mycred_add would also work.

    Let me know if you have any issues or further queries.

    Update
    I forgot to include the mycred_exclude_user check to make sure the current user is not excluded (since you can exclude users if you want).

    Plugin Author myCred

    (@designbymerovingi)

    P.S. If this code is placed inside the loop in your page template you could replace $post->ID with the get_the_ID() function. I.e.:

    if ( class_exists( 'myCRED_Core' ) && is_user_logged_in() && !mycred_exclude_user( get_current_user_id() ) ) {
    	mycred_add( 'points_for_page_view', get_current_user_id(), 300, 'Points for viewing page', get_the_ID() );
    }
    Thread Starter monitor

    (@oomskaap)

    Wow thanks so much for your in depth reply. This is really useful.

    Thread Starter monitor

    (@oomskaap)

    Hi Gabriel,

    Unfortunately after a few purchases the points are not being awarded.
    I think it is because the after purchase page ( I use cart66 ), shows a custom receipt file, receipt.php where all the codes are in.

    It is there that i need to fire the award points function.
    Is there a way your code can work to fire the php that is not on a post, or a page or a loop as the above cubepoints code?

    Plugin Author myCred

    (@designbymerovingi)

    Hey

    You could just use:

    if ( class_exists( 'myCRED_Core' ) && is_user_logged_in() && !mycred_exclude_user( get_current_user_id() ) ) {
    	mycred_add( 'points_for_page_view', get_current_user_id(), 300, 'Points for viewing page' );
    }

    This code will just make a simple log entry without any reference to a post. This code will simply just award 300 points to the current user when ever the code is executed (page load).

    Plugin Author myCred

    (@designbymerovingi)

    If the code is not executing it could also be that this custom php file is loaded before myCRED is loaded causing class_exist to return false. If you have further problems activate WP_DEBUG in your wp-config.php file to see what error messages you are getting.

    Thread Starter monitor

    (@oomskaap)

    For testing, i create a custom page template, with this code:

    <?php if ( class_exists( 'myCRED_Core' ) && is_user_logged_in() && !mycred_exclude_user( get_current_user_id() ) ) {
    	mycred_add( 'points_for_page_view', get_current_user_id(), 300, 'Points for viewing page' ); } ?>

    Technically it should add 300 points upon each refresh, right?
    I’ve tried several methods but can’t get the points to add.

    Sorry for annoying you with this, but its quite a big feature of our ecommerce sites and i would be grateful if it can work.

    Thread Starter monitor

    (@oomskaap)

    I see this :

    Notice: Undefined variable: pref in /home/xxxx/public_html/xxxx/wp-content/plugins/mycred/includes/mycred-functions.php on line 968

    Could it be related?

    When i view those lines it shows:

    // Add creds
    		return $mycred->add_creds( $pref, $user_id, $amount, $entry, $ref_id, $data, $type );
    	}
    }
    Plugin Author myCred

    (@designbymerovingi)

    Nice catch!

    It seems I miss spelled $ref for $pref. If you open the mycred-functions.php file and change that on line 968 to:

    return $mycred->add_creds( $ref, $user_id, $amount, $entry, $ref_id, $data, $type );

    Alternatively if you do not want to make changes in the plugin file your code could be changed to:

    if ( class_exists( 'myCRED_Core' ) && is_user_logged_in() && !mycred_exclude_user( get_current_user_id() ) ) {
    	$mycred = mycred_get_settings();
    	$mycred->add_creds( 'points_for_page_view', get_current_user_id(), 300, 'Points for viewing page' );
    }

    Also, if you want to display some sort of message when points are awarded you could change the code to this:

    if ( class_exists( 'myCRED_Core' ) && is_user_logged_in() && !mycred_exclude_user( get_current_user_id() ) ) {
    	$mycred = mycred_get_settings();
    	if ( $mycred->add_creds( 'points_for_page_view', get_current_user_id(), 300, 'Points for viewing page' ) === true );
    		echo 'You have received 300 points!';
    }
    Thread Starter monitor

    (@oomskaap)

    It works now! Thanks for your time.

    Thanks again for your help Gabriel. In regards to above, what would the code be to do the following:
    (1) Limit the points for page given to a user only once and/or to once per day. Right now people can keep hitting refresh and get as many points as they want.
    (2) If the points we’re given, then show an image and text message of “sorry already received, etc”
    (3) The notification pop up balloon shows ‘Points for viewing the page %post_title%’ even though the log file shows the actual post title. Any idea on fixing this.

    Plugin Author myCred

    (@designbymerovingi)

    Hey mryan2013.

    1) Have a look at the mycred_give shortcode which does just this and has a built in limit.

    2) The “easiest” way to accomplish this would be to create a new custom shortcode which either executes the mycred_give shortcode (if user has not passed limit) or if the user has, shows your image and text.

    Example:

    add_shortcode( 'mycred_give_custom', 'my_custom_shortcoce' );
    function my_custom_shortcoce( $atts, $content = NULL )
    {
    	extract( shortcode_atts( array(
    		'amount' => NULL,
    		'log'    => '',
    		'ref'    => 'gift',
    		'limit'  => 0,
    		'type'   => 'mycred_default'
    	), $atts ) );
    
    	$limit = abs( $limit );
    	// if over limit
    	if ( $limit != 0 && mycred_count_ref_instances( $ref, $user_id ) >= $limit ) {
    		//show your text and image (remember to return result not echo)
    	}
    	// not over limit load mycred_give
    	return mycred_render_shortcode_give( $atts, $content );
    }

    3) This is a known bug. It will be fixed in the next update, till then you can find a solution in our forum.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can your plugin reproduce this Cubepoints code? – award points via function –’ is closed to new replies.