Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active April 24, 2021 13:41
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/fb95fe04f9fab2c03781f4a52d8470ff to your computer and use it in GitHub Desktop.
Save xnau/fb95fe04f9fab2c03781f4a52d8470ff to your computer and use it in GitHub Desktop.
Shows how to set the page title according to the Participants Database record data.
<?php
/**
* Plugin Name: PDB Record Page Title
* Description: uses data from a Participants Database record to set the page title
*
* This plugin uses one or more fields from a participants database record to set
* the title of the page.
*/
class pdb_record_page_title {
/**
* @var array of field names to use to construct the page title
*/
private $title_fields = array( 'first_name', 'last_name' );
/**
* sets up the filter
*/
public function __construct()
{
// this filter is fired when the page gets its title
add_filter( 'pre_get_document_title', array( $this, 'page_title' ) );
}
/**
* sets the page title if the page is showing a PDB record
*
* @global WP_Post $post
* @param string $title
* @return string
*/
public function page_title( $title )
{
global $post;
// check to see if the page has a [pdb_single] shortcode
if ( strpos( $post->post_content, '[pdb_single' ) !== false ) {
// get the record ID from the URL
// this assumes you are using a URL that has the ?pdb=1234 format
$record_id = filter_input( INPUT_GET, Participants_Db::$single_query, FILTER_SANITIZE_NUMBER_INT );
// if we have a record ID, use it to make the title
if ( $record_id ) {
$title = $this->make_page_title( $record_id );
}
}
return $title;
}
/**
* provides the page title, given the record ID
*
* @param int $record_id
* @return string
*/
private function make_page_title( $record_id )
{
// get the record values
$record = Participants_Db::get_participant( $record_id );
$title = array();
// get the title values
foreach ( $this->title_fields as $fieldname ) {
$title[] = $record[$fieldname];
}
// assemble the values to form the title
return implode( ' ', $title );
}
}
new pdb_record_page_title;
@elvis-pazin
Copy link

elvis-pazin commented Apr 24, 2021

I expectet it would change (or at least show) the actual page title in the page content.
Let's say that the page title of the page containing the pdb_single shortcode is 'Record Data'. That title is shown for every record.
It would be nice if the title changed (or at least showed underneath) based on record data. For examle, if record 'John Doe' is opened as single record, the page/post title would change to record's lastname, name on top of all tabs.
I hope this makes sense.

Thanx for your reply, anyway.
Best regards,
Elvis.

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