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;
@xnau
Copy link
Author

xnau commented Sep 12, 2020

This plugin will only work if you are using Participants Database with plain single record links. It will not work if you are using the Permalinks add-on.

If you are using the Directory Tools add-on you won't need this, it is already set up when using Proxy Posts.

@elvis-pazin
Copy link

Hello Roland!
I tried this plugin, but it doesn't work.
It does show the record data in the browser tab though.
The page title (the one with the pdb_single shortcode) doesn't change.
I'm not using Directory Tools add-on nor Permalinks add-on. My permalinks setting is default (simple post). I tried changing it to 'post name', but it's not working.
Is it possible to make it work somehow?
Thanks in advance.

@xnau
Copy link
Author

xnau commented Apr 23, 2021

Showing the record data in the browser tab is what this does, the browser tab is the page title. What do you want it to do that it is not doing?

@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