Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Last active August 29, 2015 14:23
Show Gist options
  • Save barryhughes/e50376648e9fd0088377 to your computer and use it in GitHub Desktop.
Save barryhughes/e50376648e9fd0088377 to your computer and use it in GitHub Desktop.
/**
* Helper to let users comment on venues.
*
* - Discussion must be enabled via WP settings
* - Default page template is recommended
* - Success is, to a degree, theme dependent!
*
* To launch, call VenueCommments::enable(). Note that this was written against
* The Events Calendar/Events Calendar PRO 3.9.3 - in future releases this may
* not be required or different strategies may need to be employed.
*/
class VenueComments {
protected $template = '';
public static function enable() {
return new self();
}
public function __construct() {
add_action( 'init', array( $this, 'setup' ), 20 );
}
public function setup() {
if ( ! class_exists( 'TribeEvents' ) ) return;
add_post_type_support( TribeEvents::VENUE_POST_TYPE, 'comments' );
add_action( 'wp_head', array( $this, 'on_venue_post' ) );
}
public function on_venue_post() {
if ( ! is_singular( TribeEvents::VENUE_POST_TYPE ) ) return;
add_filter( 'comments_open', array( $this, 'restore_venue_object' ) );
add_filter( 'comments_template', array( $this, 'capture' ), 5 );
add_filter( 'comments_template', array( $this, 'restore' ), 50 );
add_filter( 'comments_template', array( $this, 'restore' ), 500 ); // Double hooked deliberately!
}
public function capture( $path ) {
$this->template = $path;
}
public function restore( $path ) {
return empty( $this->template ) ? $path : $this->template;
}
public function restore_venue_object( $status ) {
$GLOBALS['post'] = get_post( tribe_get_venue_id() );
return $status;
}
}
VenueComments::enable();
@barryhughes
Copy link
Author

Notes:

  • We double-hook on comments_template because TEC code unhooks a callback on the same action - a core WP bug means our own comments_template callback would otherwise be skipped
  • We restore the venue post object since otherwise the final comments form will relate to the last listed upcoming event for that venue
  • Successfully tested with TEC/ECP 3.9.3, WP 4.2.2 and Twenty Fifteen default theme - default page template must be enabled in the event settings

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