Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
When getting points for liking a content, change the reference to identify the post type so we can create badges for each post type that gets liked instead of just one for all likes.
/**
* Adjust WP ULike Reference
* When getting points for liking a content, change the reference
* to identify the post type so we can create badges for each post type
* that gets liked instead of just one for all likes.
* @version 1.0
*/
add_filter( 'mycred_run_this', 'mycred_wpulike_per_cpt', 10, 3 );
function mycred_wpulike_per_cpt( $run_this, $mycred ) {
// By default WPUlike uses wp_add_like
if ( $run_this['ref'] == 'wp_add_like' ) {
// The post we are liking is available under $ref_id
$post = get_post( $run_this['ref_id'] );
// If the post exists append the post type
if ( isset( $post->ID ) )
$run_this['ref'] = 'wp_add_like_' . $post->post_type;
}
return $run_this;
}
/**
* Add Custom References
* We need to add these new references to the list so we can select them when
* we for example create a badge for these instances.
* @version 1.0
*/
add_filter( 'mycred_all_references', 'mycred_pro_add_wpulike_references' );
function mycred_pro_add_wpulike_references( $list ) {
$post_type_args = array(
'public' => true,
'_builtin' => false
);
$post_types = get_post_types( $post_type_args, 'objects', 'and' );
foreach ( $post_types as $post_type ) {
$reference_id = 'wp_add_like_' . $post_type->name;
$list[ $reference_id ] = sprintf( 'Liking %s', $post_type->labels->name );
}
return $list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment