BFCM Sale Is Live —SAVE 24% on All Plans. 🙌 Use code:BFCM24

Add Custom Template Tags

mycred tutorials custom template

Navigation:

Description

myCred offers a growing number of template tags for you to use in your installation, but for some, this is simply not enough. In this tutorial I will show how you can add custom template tags for general, amount related, post related, comment related and user related.

Requirements

  • myCred version 1.1 or higher.
  • Basic understanding of how WordPress filter hooks work.

General Template Tags

General template tags are one of the most used template tags in myCred and is run either individually or when any other type of template tags are parsed.

To add our own template tags we will need to use the  mycred_parse_tags_amountmyCred_parse_tags_general fiter. In the code snippet below, we will add a new template tag %register_url% which will return a link to your websites default registration page.

add_filter( 'mycred_parse_tags_general', 'my_custom_general_template_tags' );
function my_custom_general_template_tags( $content )
{
	$content = str_replace( '%register_url%', get_bloginfo( 'url' ) . '/wp-login.php?action=register', $content );
	return $content; 
}

This code goes into your themes functions.php file.

Amount Related Template Tags

There are only two amount related template tags, %cred% and %cred_f% which will return a given amount either in plain format or formatted according to your myCred installation.

To add our own template tags we will need to use the mycred_parse_tags_amount fiter. In the code snippet below, we will add a new template tag %dollars% which will return the given amount with a dollar sign added as a prefix.

add_filter( 'mycred_parse_tags_amount', 'my_custom_amount_template_tags', 10, 2 );
function my_custom_amount_template_tags( $content, $amount )
{
	$content = str_replace( '%dollars%', '$ ' . $amount, $content );
	return $content; 
}

This code goes into your themes functions.php file.

Post Related Template Tags

Post related template tags are used when there is a reference to a post id allowing us to grab that posts details and build template tags like %link_with_title% which will return the permalink to the given post.

To add our own template tags we will need to use the mycred_parse_tags_post fiter. In the code snippet below, we will add a new template tag %post_status% which will return the given post’s current status.

add_filter( 'mycred_parse_tags_post', 'my_custom_post_template_tags', 10, 3 );
function my_custom_post_template_tags( $content, $post, $data )
{
	$content = str_replace( '%post_status%', $post->post_status, $content );
	return $content; 
}

This code goes into your themes functions.php file.

Comment Related Template Tags

These template tags are only used in the “Points for Commenting” hook by default and contains template tags for the most commonly required comment details.

To add our own template tags we will need to use the mycred_parse_tags_comment fiter. In the code snippet below, we will add a new template tag %IP% which will return the IP address of the comment author.

add_filter( 'mycred_parse_tags_comment', 'my_custom_comment_template_tags', 10, 3 );
function my_custom_comment_template_tags( $content, $comment, $data )
{
	$content = str_replace( '%IP%', $comment->comment_author_IP, $content );
	return $content; 
}

This code goes into your themes functions.php file.

User Related Template Tags

User related template tags is the second most used template tag types in myCred and allows you to include a vast range of user details.

This filter is also used by for example the Rankings Add-on to add new custom template tags for rank names and logos.

To add our own template tags we will need to use the mycred_parse_tags_user fiter. In the code snippet below, we will add two new template tags %avatar_small% and %avatar_large%, which will return given users avatar either in a smaller version ( 32px by 32px ) or a larger version ( 64px by 64px ).

add_filter( 'mycred_parse_tags_user', 'my_custom_user_template_tags', 10, 3 );
function my_custom_user_template_tags( $content, $user, $data )
{
	$content = str_replace( '%avatar_small%', get_avatar( $user->ID, 32 ), $content );
	$content = str_replace( '%avatar_large%', get_avatar( $user->ID, 64 ), $content );
	return $content; 
}

This code goes into your themes functions.php file.

Final Notes

While you can add support for custom template tags though other filters as well, i.e. the myCred_ranking_row filter, if you want to add a custom template tags that can be used in more than one instance, you should use the above filters.

If you only want to add a shortcode for specific instances i.e. when showing ranking rows in the myCred Balance Widget, you should use that particular filter instead.

11