Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active November 29, 2020 09:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/07469917245d2c0dc98d80d3c15c8e6f to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/07469917245d2c0dc98d80d3c15c8e6f to your computer and use it in GitHub Desktop.
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