Skip to content

Instantly share code, notes, and snippets.

@jeherve
Last active July 9, 2022 14:41
Show Gist options
  • Save jeherve/676efc0716722f39cf4af4b7ca3db184 to your computer and use it in GitHub Desktop.
Save jeherve/676efc0716722f39cf4af4b7ca3db184 to your computer and use it in GitHub Desktop.
Traktivity plugin: shortcode to display tv shows and movies watched in the 7 days prior to a given date. https://wordpress.org/support/topic/using-trackt-details-on-frontend/
<?php
/**
* Plugin Name: Weekly Traktivity shortcode
* Plugin URI: https://wordpress.org/support/topic/using-trackt-details-on-frontend/
* Description: A shortcode to display tv shows and movies watched in the 7 days prior to a given date.
* Author: Jeremy Herve
* Version: 1.0.0
* Author URI: https://jeremy.hu
* License: GPL2+
*/
/**
* Display a list of Traktivity events from the past 7 days.
*
* Use it like this: [traktivity_weekly date="March 2, 2016" max="5" movies="false"]
* 20 items by default if you do not specify a maximum.
* Both movies and TV episodes are displayed by default, but you can opt to exclude movies with the movies attribute.
* The list items can have 2 different formats:
* - Movies are displayed like so: Movie Name (Year)
* - TV episodes are displayed like so: Episode Name (Show name, season x)
*
* @param array $atts An array of shortcode attributes.
*
* @return string HTML output of the shortcode.
*/
add_shortcode( 'traktivity_weekly', function ( $atts ) {
$atts = shortcode_atts(
array(
'max' => 20, // How many posts do we want maximum?
'date' => date( 'F j, Y' ),
'movies' => true,
),
$atts,
'traktivity_weekly'
);
// Search for events in the 7 days prior to our chosen date.
$from_date = $atts['date'] . ' - 7 days';
// Start the list we'll return.
$html = '<ul class="traktivity_weekly">';
// Query for events.
$query_args = array(
'post_type' => 'traktivity_event',
'posts_per_page' => (int) $atts['max'],
'date_query' => array(
'after' => $from_date,
'before' => $atts['date'],
'inclusive' => true,
),
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $post ) {
$query->the_post();
// Event metadata (TV show or movie, year and season).
$trakt_type = get_the_terms( $post, 'trakt_type' );
$is_episode = is_array( $trakt_type ) && 'tv-series' === $trakt_type[0]->slug;
$trakt_show = get_the_terms( $post, 'trakt_show' );
$trakt_year = get_the_terms( $post, 'trakt_year' );
$trakt_season = get_the_terms( $post, 'trakt_season' );
// Drop movies if they are excluded in shortcode attributes.
if ( 'false' === $atts['movies'] && ! $is_episode ) {
continue;
}
// Format our TV episode meta, e.g. " (The Fresh Prince of Bel-Air, season 1)"
$episode_meta = is_array( $trakt_show ) && isset( $trakt_show[0] ) && is_array( $trakt_season ) && isset( $trakt_season[0] )
? ' (' . $trakt_show[0]->name . ', season ' . $trakt_season[0]->name . ')'
: '';
// Build list item.
$html .= sprintf(
'<li><a href="%1$s">%2$s</a>%3$s%4$s</li>',
esc_url( get_permalink() ),
esc_html( get_the_title() ),
( $is_episode && ! empty( $episode_meta ) ? esc_html( $episode_meta ) : '' ),
( ! $is_episode && is_array( $trakt_year ) && isset( $trakt_year[0] ) ? ' (' . esc_html( $trakt_year[0]->name ) . ')' : '' )
);
}
}
wp_reset_postdata();
// Close our unordered list.
$html .= '</ul>';
return $html;
} );
@jeherve
Copy link
Author

jeherve commented Jul 9, 2022

This is an example shortcode, created following the discussion in https://wordpress.org/support/topic/using-trackt-details-on-frontend/

The date handling is quite simple, it would probably need to be made more robust to accept different date formats. Right now it should accept any strtotime-compatible dates, e.g. "March 2, 2016".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment