Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active July 18, 2018 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/43f8206aeb6b91516e608a6e3b880062 to your computer and use it in GitHub Desktop.
Save xnau/43f8206aeb6b91516e608a6e3b880062 to your computer and use it in GitHub Desktop.
Shows how to set up a template for including meta tags in a Participants Database singe record page when using the Image Expansion add-on
<?php
/**
* @name pdb single opengraph template
* @version 0.1
*
* template for printing opengraph meta tags
*
* this variant is for use with the Participants Database Image Expansion add-on
*
* simplified information on using social media meta tags can be found here:
* https://css-tricks.com/essential-meta-tags-social-media/
*
* you will need to adapt this to the data fields you are using and how you are using them,
* please don't expect this to work for you without modifying it to match your setup!
*/
// first check to make sure we have a record loaded
if ( $this->participant_id > 0 ) :
if ( ! function_exists( 'get_attachment_url_by_slug' ) ) :
/**
* provides the full URL to a media file, given the media item name (or slug)
*
* @param string $slug name of the media item
* @return string URL to the media file
*/
function get_attachment_url_by_slug( $slug ) {
$args = array(
'post_type' => 'attachment',
'name' => sanitize_title($slug),
'posts_per_page' => 1,
'post_status' => 'inherit',
);
$_header = get_posts( $args );
$header = $_header ? array_pop($_header) : null;
return $header ? wp_get_attachment_url($header->ID) : '';
}
endif;
// set up the template object
$record = new PDb_Template( $this );
?>
<!-- Start Participants Database Meta Tags -->
<?php // this will be shown as the headline of the share ?>
<meta property="og:title" content="<?php echo $record->get_value('first_name') . ' ' . $record->get_value('last_name') ?>">
<?php // this will typically appear under the headline, it should be a short descriptive text ?>
<meta property="og:description" content="<?php echo $record->get_value('description') ?>">
<?php // this needs to be the URL to the image to show in the share ?>
<meta property="og:image" content="<?php echo get_attachment_url_by_slug( $record->get_value('photo') ) ?>">
<?php // this is the URL of the current page ?>
<meta property="og:url" content="<?php echo 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ?>">
<?php // for twitter, add a caption. This could be the same as the title or description ?>
<meta name="twitter:card" content="<?php echo $record->get_value('photo_caption') ?>">
<!-- End Participants Database Meta Tags -->
<?php endif;
@xnau
Copy link
Author

xnau commented Jul 18, 2018

To create a share link, you'll need a custom template for your single record display as well. In that template, put a link like this:

<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode( Participants_Db::single_record_url( $this->participant_id ) ) ?>">Share on Facebook</a>

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