[Plugin: CubePoints] Cubepoints And Custom Post Types
-
I have a few custom post types and I’d like to give members the ability to earn points for their posts within custom post types. Is there a way to accomplish this?
-
You might already have found a solution for this but you can just add a custom module to CubePoints to accomplish what you are looking for.
Here is a module for you:
<?php /** Points for Publishing of Multiple Custom Post Type Module */ cp_module_register( __( 'Publish Custom Post Types', 'cp' ), 'mulcustomposttypepoints', '1.0', 'dbm', 'https://www.merovingi.com', 'https://www.merovingi.com', __('This module awards points for publishing custom post types.', 'cp' ), 1 ); if ( cp_module_activated( 'mulcustomposttypepoints' ) ) { /** Module Configuration */ add_action( 'cp_config_form', 'cp_multi_customtypes_admin_setting' ); function cp_multi_customtypes_admin_setting() { echo ' <br /> <h3>Custom Post Types</h3> <table class="form-table"> <tr valign="top"> <th scope="row"><label for="cp_customtype_used">All post types give same points?</label></th> <td valign="middle"> <select id="cp_multi_option" name="cp_multi_option">' . "\n"; $multiple_types = get_option( 'cp_multi_type' ); // defaults to yes if ( empty( $multiple_types ) ) $multiple_types = 'yes'; $our_option = array( 'yes', 'no' ); foreach ( $our_option as $option ) { echo '<option value="' . $option . '"'; if ( $option == $multiple_types ) echo ' selected="selected"'; echo '> ' . ucfirst( $option ) . '</option>' . "\n"; } echo ' </select> </td> </tr>'; // If all custom post types are given the same points we only want to know one thing, how much. if ( $multiple_types == 'yes' || empty( $multiple_types ) ) { echo ' <tr valign="top"> <th scope="row"><label for="cp_customtype_points">Points for Publishing a custom post type:</label></th> <td valign="middle"><input type="text" id="cp_multi_type_points" name="cp_multi_type_points" value="' . get_option( 'cp_multi_type_points' ) . '" size="30" /></td> </tr>'; } // Otherwise we list all custom post types with its own points else { $post_types = cp_module_get_custom_post_types( 'names' ); foreach ( $post_types as $post_type ) { $post_type_points = get_option( 'cp_mctype_' . $post_type ); $name = 'cp_mctype_' . $post_type; echo ' <tr valign="top"> <th scope="row"><label for="cp_customtype_points">Publishing <code>' . $post_type . '</code>:</label></th> <td valign="middle"><input type="text" id="' . $name . '" name="' . $name . '" value="' . $post_type_points . '" size="30" /></td> </tr>'; } } $remove_settings = get_option( 'cp_multi_remove_points' ); echo ' <tr valign="top"> <th scope="row"><label for="cp_customtype_points">Remove Points:</label></th> <td valign="middle"> <input type="checkbox" id="cp_multi_unpublish" name="cp_multi_remove_points" value="1" '; checked( $remove_settings, 1 ); echo ' /> <span class="description">Remove points if a </span><code>Published</code><span class="description"> item gets changed to </span><code>Draft</code><span class="description"> or </span><code>Pending</code> </td> </tr> </table>'; } /** Save Module Congif */ add_action( 'cp_config_process', 'cp_multi_customtypes_save' ); function cp_multi_customtypes_save() { // If all post types get the same points if ( $_POST['cp_multi_option'] == 'yes' ) { update_option( 'cp_multi_type', 'yes' ); update_option( 'cp_multi_type_points', (int)$_POST['cp_multi_type_points'] ); } else { update_option( 'cp_multi_type', 'no' ); delete_option( 'cp_multi_type_points' ); } update_option( 'cp_multi_remove_points', $_POST['cp_multi_remove_points'] ); // Delete any custom points previously used. $post_types = cp_module_get_custom_post_types( 'names' ); if ( $_POST['cp_multi_option'] == 'yes' ) { foreach ( $post_types as $post_type ) { delete_option( 'cp_mctype_' . $post_type ); } } else { foreach ( $post_types as $post_type ) { update_option( 'cp_mctype_' . $post_type, (int)$_POST['cp_mctype_' . $post_type] ); } } } // Get custom post types function cp_module_get_custom_post_types( $type = 'names' ) { $args = array( 'public' => true, '_builtin' => false ); $post_types = get_post_types( $args, $type, 'and' ); return $post_types; } // Check if we have already given a post points function cp_module_already_got_points( $post_id, $post_type ) { global $wpdb; $count = (int) $wpdb->get_var("select count(id) from <code>" . CP_DB . "</code> where <code>type</code>='$post_type' and <code>data</code>=" . $post_id ); if ( $count == 0 ) return false; else return true; } // Check if we want to remove points on unpublishing function cp_module_remove_points_unpublish() { if ( get_option( 'cp_multi_remove_points' ) ) return true; else return false; } // Hook for each registered custom post type // Add an action for each post type you want to give points for add_action( 'publish_book', 'cp_module_publish_post_type' ); //add_action( 'publish_posttype', 'cp_module_publish_post_type' ); // When a published item goes back to "draft" or "pending" we remove points until they get published again. if ( cp_module_remove_points_unpublish() ) { add_action( 'publish_to_draft', 'cp_module_multi_type_remove_points' ); add_action( 'publish_to_pending', 'cp_module_multi_type_remove_points' ); } // When we add points function cp_module_publish_post_type( $post_id ) { // If we dont want to remove points on unpublishing and points has already been given, bail. if ( !cp_module_remove_points_unpublish() && cp_module_already_got_points() ) return; $post_type = get_post_type( $post_id ); if ( $post_type != 'post' ) { $post = get_post( $post_id ); $user_id = $post->post_author; $post_type_rule = get_option( 'cp_multi_type' ); if ( $post_type_rule == 'yes' ) $points = get_option( 'cp_multi_type_points' ); else $points = get_option( 'cp_mctype_' . $post_type ); cp_points( $post_type, $user_id, (int) $points, $post_id ); } } // When we remove posts function cp_module_multi_type_remove_points( $post ) { // get the posts post type $post_id = $post->ID; $post_type = get_post_type( $post_id ); if ( $post_type != 'post' ) { // get our setting $post_type_rule = get_option( 'cp_multi_type' ); // grab how many points we want to remove if ( $post_type_rule == 'yes' ) $points_to_take = get_option( 'cp_multi_type_points' ); else $points_to_take = get_option( 'cp_mctype_' . $post_type ); cp_points( 'negative', $post->post_author, '-' . $points_to_take, $post_id ); } } /** Adjust the Log. Overwrite custom post type entries. */ add_action( 'cp_logs_description', 'cp_admin_logs_desc_multi_customtypes', 15, 4 ); function cp_admin_logs_desc_multi_customtypes( $type, $uid, $points, $data ) { // Grab details about the post type and the post itself. $post = get_post( $data ); if ( $post->post_type == $type && $type != 'post' ) { $obj = get_post_type_object( $type ); echo 'Points given for ' . $obj->labels->singular_name . ': <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a>'; } elseif ( $type == 'negative' ) { echo 'Removed points for: <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a> for getting drafted or pending.'; } } } ?>
You will need to add a an action for each custom post type you want to use. In the above example I am giving points for the post type
books
.How could i use Cubepoint with Achievement plugin?
My problem is if someone has increase his Cubepoints points upto 150 (it will be vary on the admin input) then that user will have some real award like a Dinner with someone special.
Thanks!Most plugins comes with hooks that you can take advantage off by hooking into them and manipulating CubePoints. Do a search for the plugin name and cubepoints. Could be that someone out there has already made a module for it.
Thank you designbymerovingi, but i only found something which will add an action i mean addaction() function but it is not working well.
Anyway, i have to know how the event is triggered in Achievement plugin so that it could be easier to make my own code as the need.Thanks
What is the name of the plugin your are using?
Hi, I use geoplaces4 at https://templatic.com/app-themes/geo-places-city-directory-wordpress-theme/.
How to when user submit new place, they will get new points by this module ?Thanks.
shiftdel15589 please open a new post with your question, as this post is regarding “Cubepoints And Custom Post Types”.
Hi @shiftdel15589
The below code will alter your points as much you want. If i want to reward the user bt 10 points, then i put this code exactly.<?php cp_alterPoints(cp_currentUser(), 10); ?>
You can add this code on click of that Submit New Place button.
Using this module, and the solution is super good.
but please i have a little problem:
My users can edit the Custom Post and when they do they always receive points. Any idea how to limit this.Currently I can not move on myCRED ??
myCRED it’s better and does not have this problem.The problem is in the function. The forum software messed up the code.
$count = (int) $wpdb->get_var("select count(id) from <code>" . CP_DB . "</code> where <code>type</code>='$post_type' and <code>data</code>=" . $post_id );
Should be this:
$count = (int) $wpdb->get_var("select count(id) from " . CP_DB . " where type='$post_type' and data=" . $post_id );
User still get points every time I update that post ??
I tried so:
// Check if we have already given a post points
function cp_module_already_got_points( $post_id, $post_type ) {global $wpdb;
$count = (int) $wpdb->get_var(“select count(id) from".CP_DB."
wheretype
=’post’ anddata
=”. $pid);
if ( $count == 0 )
return false;
else
return true;
}I tried with: Multiple Custom Post Type Points
I get the same problem with my Custom Post, do not understand that the posts have no problem.
You see where it says
where type = 'post'
. That needs to bewhere type = '$post_type'
. It is checking if a post of the type'post'
has been awarded points. You want to check for the post type passed to the function ($post_type
).my mistake, I did try with the correct form:
$count = (int) $wpdb->get_var(“SELECT COUNT(ID) FROM".CP_DB."
wheretype
=’$post_type’ anddata
=”. $post_id);
and it did not work.Hey.
I have made a new version of this module using the
transition_post_status
action instead ofpublish_$post_type
. Try it out and see what you think. It should only award point when the custom post type gets published.Using pastebin instead of pasting code here to avoid any errors.
https://pastebin.com/ZH4nYJba
- The topic ‘[Plugin: CubePoints] Cubepoints And Custom Post Types’ is closed to new replies.