Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Make cubepoints register points for custom post types! Help! WordPress

  • SOLVED

I completely miscalculated how many things would hate custom post types, and have just spent a whole day moving my community added posts to a Custom-post type (around 12,000 posts), but now people aren't getting their cubepoints for creating them (grrrr).

In my crazed googling I found this, although no idea what it does.

http://code.google.com/p/cubepoints/source/browse/trunk/modules/custom_points.php?spec=svn32&r=32


and this:
http://cubepoints.com/forums/topic/extend-cubepoints-module


And this is in their documentation

Modules System
Using the modules system to integrate with other plugins is easy. Simply add this code into your intergration codes, and your module information will be shown in CubePoints > Modules under WP Admin Panel


<?php
$cp_modules[] = array (
name => 'Module Name',
version => '1.0.0',
url => 'Module URL',
description => 'Module Description',
api_version => '1.0 (Do not change)',
author => 'Module Author',
author_url => 'Author URL'
);
?>


Found here - http://plugins.svn.wordpress.org/cubepoints/tags/2.1.1/docs/docs.html


Anyone have any experience with Cubepoints?The developers seem to believe it's pretty easy to create custom functions, but haven't gotten around to ever properly explaining how :(

Fingers and toes crossed.

Answers (1)

2011-07-31

Gabriel Merovingi answers:

Hey!

What version of Cube do you use?


kateM82 comments:

The most recent one, just updated today :)


Gabriel Merovingi comments:

Can you paste your code for when you create this custom post type you wish to give points for?


Gabriel Merovingi comments:

Here is a module that gives 100 points (set on line 92) when someone publishes a post with custom post type "book" (also set on line 92). You can change this to suit your needs.

I have tried it on my server and it works. Let me know what you think.

<?php

/** Custom Points Module */

cp_module_register(__('Book Points', 'cp') , 'customtype' , '1.0', 'DBM', 'http://www.merovingi.com', 'http://www.merovingi.com' , __('This module gives you the ability to set custom points for custom post type book.', 'cp'), 1);

if(cp_module_activated('customtype')){

/* Define the custom box */
add_action('admin_init', 'cp_module_customtype_add_custom_box', 1);

/* Do something with the data entered */
add_action('save_post', 'cp_module_customtype_save_postdata');
add_action('publish_post', 'cp_module_customtype_publish_postdata');

/* Adds a box to the main column on the Post and Page edit screens */
function cp_module_customtype_add_custom_box() {
add_meta_box( 'cp_module_customtype_set', 'CubePoints - Custom Points', 'cp_module_customtype_box', 'book', 'normal', 'high' );
}

/* Prints the box content */
function cp_module_customtype_box() {

global $post;

// Use nonce for verification
wp_nonce_field( plugin_basename(__FILE__), 'cp_module_customp_nonce' );

// The actual fields for data entry
echo '<br /><input type="checkbox" id="cp_module_customp_enable" name="cp_module_customp_enable" value="1" size="25" '.((bool)(get_post_meta($post->ID , 'cp_points_enable', 1))?'checked="yes"':'').' /> ';
echo '<label for="cp_module_customp_enable">' . __("Set custom points for comments on this page" , 'cp') . '</label> ';
echo '<br /><br />';
echo '<label for="cp_module_customp_points">' . __("Number of points per comment" , 'cp') . ':</label> ';
echo '<input type="text" id= "cp_module_customp_points" name="cp_module_customp_points" value="'.(int)get_post_meta($post->ID , 'cp_points', 1).'" size="25" /><br /><br />';
}

/* When the post is saved, saves our custom data */
function cp_module_customtype_save_postdata( $post_id ) {

// get post id from the revision id
if($parent_id = wp_is_post_revision($post_id)){
$post_id = $parent_id;
}

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times

if ( !wp_verify_nonce( $_POST['cp_module_customp_nonce'], plugin_basename(__FILE__) )) {
return $post_id;
}

// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;



// OK, we're authenticated: we need to find and save the data
update_post_meta($post_id, 'cp_points_enable', (int)$_POST['cp_module_customp_enable']);
update_post_meta($post_id, 'cp_points', (int)$_POST['cp_module_customp_points']);

}
/* When the post is saved, saves our custom data */
function cp_module_customtype_publish_postdata( $post_id ) {

// get post id from the revision id
if($parent_id = wp_is_post_revision($post_id)){
$post_id = $parent_id;
}

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times

if ( !wp_verify_nonce( $_POST['cp_module_customp_nonce'], plugin_basename(__FILE__) )) {
return $post_id;
}

// verify if this is an auto save routine. If it is our form has not been submitted, so we don't want to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;



// OK, we're authenticated: we need to find and save the data

$posttype = get_post($post_id);
$uid = $posttype->post_author;
global $wpdb;
$count = (int) $wpdb->get_var("select count(id) from `".CP_DB."` where `type`='post' and `data`=". $pid);
if($count==0){
cp_points('book', $uid, '100', $post_id);
}
update_post_meta($post_id, 'cp_points_enable', (int)$_POST['cp_module_customp_enable']);
update_post_meta($post_id, 'cp_points', (int)$_POST['cp_module_customp_points']);

}

/* Add CubePoints comment action hook */
add_action('cp_newComment', 'cp_module_customtype_newComment');
function cp_module_customtype_newComment($cid) {
if (is_user_logged_in()) {
$cdata = get_comment($cid);
$pid = $cdata->comment_post_ID;
$customp_enabled = (bool) get_post_meta($pid,'cp_points_enable', 1);
$customp_points = (int) get_post_meta($pid,'cp_points', 1);
if( $customp_enabled ){
add_filter('cp_comment_points',create_function('$points', 'return '.$customp_points.';'),1);
}
}
}

}

?>


Gabriel Merovingi comments:

Save this as a php file and put it into your module folder of the plugin. Adjust it to suit your needs and don't forget to activate


kateM82 comments:

Hi Gabriel,

It's still not adding any points, do I need to change permission of the file/folder or anything like that?

It appears in the modules admin and I have activated it, but nothing is showing in the logs when I create the custom post?

Here is what I added (custom post is called 'dshop' , points value is 15)


<?php



/** Custom Points Module */



cp_module_register(__('dSHOP Points', 'cp') , 'customtype' , '1.0', 'DBM', 'http://www.merovingi.com', 'http://www.merovingi.com' , __('This module gives you the ability to set custom points for custom post type dSHOP.', 'cp'), 1);



if(cp_module_activated('customtype')){



/* Define the custom box */

add_action('admin_init', 'cp_module_customtype_add_custom_box', 1);



/* Do something with the data entered */

add_action('save_post', 'cp_module_customtype_save_postdata');

add_action('publish_post', 'cp_module_customtype_publish_postdata');



/* Adds a box to the main column on the Post and Page edit screens */

function cp_module_customtype_add_custom_box() {

add_meta_box( 'cp_module_customtype_set', 'CubePoints - Custom Points', 'cp_module_customtype_box', 'dshop', 'normal', 'high' );

}



/* Prints the box content */

function cp_module_customtype_box() {



global $post;



// Use nonce for verification

wp_nonce_field( plugin_basename(__FILE__), 'cp_module_customp_nonce' );



// The actual fields for data entry

echo '<br /><input type="checkbox" id="cp_module_customp_enable" name="cp_module_customp_enable" value="1" size="25" '.((bool)(get_post_meta($post->ID , 'cp_points_enable', 1))?'checked="yes"':'').' /> ';

echo '<label for="cp_module_customp_enable">' . __("Set custom points for comments on this page" , 'cp') . '</label> ';

echo '<br /><br />';

echo '<label for="cp_module_customp_points">' . __("Number of points per comment" , 'cp') . ':</label> ';

echo '<input type="text" id= "cp_module_customp_points" name="cp_module_customp_points" value="'.(int)get_post_meta($post->ID , 'cp_points', 1).'" size="25" /><br /><br />';

}



/* When the post is saved, saves our custom data */

function cp_module_customtype_save_postdata( $post_id ) {



// get post id from the revision id

if($parent_id = wp_is_post_revision($post_id)){

$post_id = $parent_id;

}



// verify this came from the our screen and with proper authorization,

// because save_post can be triggered at other times



if ( !wp_verify_nonce( $_POST['cp_module_customp_nonce'], plugin_basename(__FILE__) )) {

return $post_id;

}



// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )

return $post_id;







// OK, we're authenticated: we need to find and save the data

update_post_meta($post_id, 'cp_points_enable', (int)$_POST['cp_module_customp_enable']);

update_post_meta($post_id, 'cp_points', (int)$_POST['cp_module_customp_points']);



}

/* When the post is saved, saves our custom data */

function cp_module_customtype_publish_postdata( $post_id ) {



// get post id from the revision id

if($parent_id = wp_is_post_revision($post_id)){

$post_id = $parent_id;

}



// verify this came from the our screen and with proper authorization,

// because save_post can be triggered at other times



if ( !wp_verify_nonce( $_POST['cp_module_customp_nonce'], plugin_basename(__FILE__) )) {

return $post_id;

}



// verify if this is an auto save routine. If it is our form has not been submitted, so we don't want to do anything

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )

return $post_id;







// OK, we're authenticated: we need to find and save the data



$posttype = get_post($post_id);

$uid = $posttype->post_author;

global $wpdb;

$count = (int) $wpdb->get_var("select count(id) from `".CP_DB."` where `type`='post' and `data`=". $pid);

if($count==0){

cp_points('dshop', $uid, '15', $post_id);

}

update_post_meta($post_id, 'cp_points_enable', (int)$_POST['cp_module_customp_enable']);

update_post_meta($post_id, 'cp_points', (int)$_POST['cp_module_customp_points']);



}



/* Add CubePoints comment action hook */

add_action('cp_newComment', 'cp_module_customtype_newComment');

function cp_module_customtype_newComment($cid) {

if (is_user_logged_in()) {

$cdata = get_comment($cid);

$pid = $cdata->comment_post_ID;

$customp_enabled = (bool) get_post_meta($pid,'cp_points_enable', 1);

$customp_points = (int) get_post_meta($pid,'cp_points', 1);

if( $customp_enabled ){

add_filter('cp_comment_points',create_function('$points', 'return '.$customp_points.';'),1);

}

}

}



}



?>


kateM82 comments:

Sorry I didn't get onto this earlier, it was 3am my time when I posted it.


Gabriel Merovingi comments:

Hey!

Ok here is a new module. Just replace the previous code with this one. It gives you 15 points for publishing "dshop" items. It also adjusts your log (by default it shows blank for custom post types).
Also i removed the custom points for comments bit. Let me know if you want that feature back.


<?php

/** Custom Points Module */

cp_module_register( __('dSHOP Points', 'cp') , 'customposttypepoints' , '1.0', 'DBM', 'http://www.merovingi.com', 'http://www.merovingi.com' , __('This module awards points when publishing dSHOP items.', 'cp' ), 1 );

if ( cp_module_activated( 'customposttypepoints' ) ) {

/* When a item is published we want to add points */
add_action( 'publish_dshop', 'cp_module_customtype_publish_points' );

function cp_module_customtype_publish_points( $post_id ) {

// get post id from the revision id
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}

// verify if this is an auto save routine. If it is our form has not been submitted, so we don't want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;

// Add points and log it.
$post = get_post( $post_id );
cp_points( 'dshop', $post->post_author, '15', $post_id );
}

/** Adjust log */
add_action('cp_logs_description','cp_admin_logs_desc_dshop', 5, 4);
function cp_admin_logs_desc_dshop($type,$uid,$points,$data){
if($type!='dshop') { return; }
$post = get_post($data);
echo __('Points given for dSHOP item:', 'cp') . ' "<a href="'.get_permalink( $post ).'">' . $post->post_title . '</a>"';
}
}

?>


Remember to deactivate the module before changing the file! Then re-activate it. Tested it in my sandbox and it works.


kateM82 comments:

Crashed site. Waiting for host to reboot server and will try again... eek!


kateM82 comments:

It seems to assign the points twice? Any ideas what would cause this?


kateM82 comments:

It also prevents my users from being able to login and would cause white screen of death if I tried to delete a post.

Any other suggestions?


Gabriel Merovingi comments:

What version of WordPress do you use?


kateM82 comments:

3.1.2


Gabriel Merovingi comments:

Can you paste the code you use to create this custom post type "dshop"?


kateM82 comments:

Hi Gabriel,

I used the plugin Custom Post Type UI (http://webdevstudios.com/support/wordpress-plugins/) to create them.

Attached is a screenshot of the settings.