Example 1
The following is the sample code to show author name in media gallery.
To do that, first you need a function which will fetch the author’s name.
function custom_rtmedia_author_name() {
global $rtmedia_backbone;
if ( $rtmedia_backbone[ 'backbone' ] ){
echo '<%= author %>';
} else {
return stripslashes( htmlentities( rtmedia_author_name( false ) ) );
}
}
You can use this function in media gallery by modifying the media-gallery-item.php template in your theme. Add the following code in media-gallery-item.php template.
<h4 title="Author Name:">
<?php custom_rtmedia_author_name ();?>
</h4>
Using the above code, it will only show author name on page load but not when you click load more or any pagination links. rtMedia uses Backbone to handle load more / pagination. You also need to filter the JSON response which is handled by Backbone.
Filter name : ** rtmedia_media_array_backbone **
You can use the following sample code your theme’s function.php file :
function rtmedia_backbone_template_filter_author_name( $media_array ){
$author_name = get_the_author_meta( 'display_name', $media_array->media_author );
$media_array->author = $author_name;
return $media_array;
}
add_filter( 'rtmedia_media_array_backbone', 'rtmedia_backbone_template_filter_author_name', 10, 1 );
Example 2
Below is the sample code to show view count in media gallery.
//Function to fetch view count in theme's functions.php file
function custom_rtmedia_get_media_view_counts( $media_id ) {
global $rtmedia_backbone;
if ( $rtmedia_backbone[ 'backbone' ] ){
echo '<%= view_count %>';
} else {
return rtmedia_get_media_view_counts_custom( $media_id );
}
}
//This is your custom function to view counts
function rtmedia_get_media_view_counts_custom( $media_id = false ) {
global $rtmedia;
$options = $rtmedia->options;
$counts = 1;
if ( isset( $options[ 'general_viewcount' ] ) && ( $options[ 'general_viewcount' ] == "0" ) ){
// Get count from rtmedia interaction table
$counts = rtmedia_get_count( $media_id, 'view' );
}
if ( $counts != 0 ) {
return $counts;
} else {
return '0';
}
return $counts;
}
//Filter JSON response
function rtmedia_backbone_template_filter_custom_details( $media_array ) {
$media_array->view_count = rtmedia_get_media_view_counts_custom( $media_array->id );
return $media_array;
}
function rtmedia_get_count( $media_id, $key ){
global $wpdb;
if ( apply_filters( 'rtmedia_use_legacy_meta_function', false ) ) {
$rtmediameta = new RTMediaMeta();
return $rtmediameta->get_meta( $id, $key );
} else {
// Check whether to get single value or multiple
$single = ( false === $key ) ? false : true;
if( ! empty ( $key ) && ! is_array( $key ) ){
// Query to fetch count for individual media
$query_for_count = "SELECT `media_id`,action,sum(`value`) as count FROM `" . $wpdb->prefix . "rt_rtm_media_interaction` WHERE media_id='" . $media_id . "' and action='" . $key . "' group by media_id";
}
// Execute query and store in variable
$result = $wpdb->get_results( $query_for_count );
if( is_array( $result ) && ! empty( $result ) && 0 !== $result[0]->count ){
return $result[0]->count;
} else {
return 0;
}
}
}
add_filter( 'rtmedia_media_array_backbone', 'rtmedia_backbone_template_filter_custom_details', 10, 1 );
Code for media-gallery-item.php template file :
<h4 title="View Count">
<?php echo custom_rtmedia_get_media_view_counts( $media_id = rtmedia_id() ); ?>
</h4>