Skip to content

Hooks and Filters

Ulrich Pogson edited this page Jun 6, 2020 · 2 revisions

The plugin provides a few hooks to customize certain parts to your needs.

rest_likes.enabled_object_types Filter

Allows you to filter the list of object types likes are allowed for. In this associative array, the object type is the key, while the value is the name of a class extending \Required\RestLikes\Controller

  • Default: [ 'post', 'comment' ]
add_filter( 'rest_likes.rest_likes.enabled_object_types', function( $object_types ) {
	unset( $object_types['comment'] );

	return $object_types;
} );

rest_likes.post.allowed_post_types Filter

Allows you to filter the list of post types likes are allowed for.

  • Default: [ 'post', 'page' ]
add_filter( 'rest_likes.post.allowed_post_types', function( $post_types ) {
	$post_types[] = 'foo' // Allow the 'foo' post type.

	return $post_types;
} );

rest_likes.script_data Filter

Allows you to filter the daa that is sent to the JavaScript, e.g. the URL to the REST API.

  • Default: [ 'root' => …, 'object_types' => … ]
add_filter( 'rest_likes.script_data', function( $script_data ) {
	$script_data['foo'] = 'bar';

 	return $script_data;
} );

rest_rest_likes.classnames Filter

Change the CSS classnames used inside the like count/button markup.

  • Default: [ 'count_classname' => 'rest-like-count', 'button_classname' => 'rest-like-button', 'liked_classname' => 'has-like', 'processing' => 'rest-like-processing' ]
add_filter( 'rest_rest_likes.classnames', function( $classnames ) {
	$classnames['count_classname'] = 'fancy-like-count';
	$classnames['button_classname'] = 'fancy-like-button';

	return $classnames;
} );

rest_likes.button_markup Filter

Allows you to change the entire markup of the button.

  • Default: <button class="%1$s" data-type="%2$s" data-id="%3$d" data-rest-like-button>%4$s %5$s</button>
  • %1$s: The CSS classnames
  • %2$s: The object type (post, comment, etc)
  • %3$d: Object ID
  • %4$s: Button text
  • %5$s: Formatted ike count
add_filter( 'rest_likes.button_markup', function( $markup, $object_id, $object_type ) {
	return sprintf( '<span data-id="%1$d" data-type="%2$s>Foo</span>', $object_id, $object_type );
}, 10, 3 );

rest_likes.button_text.like Filter

Change the text inside the like button.

  • Default: Like
add_filter( 'rest_likes.button_text.like', function( $button_text, $object_type ) {
	return '<span class="icon-thumb-up"></span>';
}, 10, 2 );

rest_likes.button_text.unlike Filter

Change the text inside the unlike button.

  • Default: Unlike
add_filter( 'rest_likes.button_text.unlike', function( $button_text, $object_type ) {
	return '<span class="icon-thumb-down"></span>';
}, 10, 2 );

rest_likes.count_markup Filter

Change the markup of the count element.

  • Default: <span class="%1$s" data-type="%2$s" data-id="%3$d" data-likes="%4$d">%5$s</span>
  • %1$s: The CSS classnames
  • %2$s: The object type (post, comment, etc)
  • %3$d: Object ID
  • %4$d: Like count
  • %5$s: Formatted ike count
add_filter( 'rest_likes.count_markup', function( $markup ) {
	return sprintf( __( 'Likes so far: %s', 'myplugin' ), $markup );
} );