Skip to content

Instantly share code, notes, and snippets.

@hiranthi
Created May 6, 2022 09: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 hiranthi/850bda4d8b0a62b97eccff17868466ae to your computer and use it in GitHub Desktop.
Save hiranthi/850bda4d8b0a62b97eccff17868466ae to your computer and use it in GitHub Desktop.
A few extras / tweaks for the WordPress plugin Glossary by Codeat: https://wordpress.org/plugins/glossary-by-codeat/
<?php
/**
* You can add this code inside your (child) theme's `functions.php`
* or within a PHP-file inside your `mu-plugins` folder
**/
/**
* Adds the shortcode [glossary-total]
*
* @returns the total number of published glossary items
**/
add_shortcode( 'glossary-total', function() {
return wp_count_posts('glossary')->publish;
});
<?php
/**
* You can add this code inside your (child) theme's `functions.php`
* or within a PHP-file inside your `mu-plugins` folder
**/
/**
* Filter to add the additional glossary terms beneath the singular glossary content
*
* @var (string) $the_content
*
* @returns (string) $the_content
**/
add_filter('the_content', function( $the_content ) {
/**
* Make sure we're on a singular glossary page and we're not inside the WordPress Dashboard
**/
if ( is_singular('glossary') && ! is_admin() )
{
/** Get all additional terms (the contents of the 'glossary_tag' post meta field) **/
$additional_terms = get_post_meta( get_the_ID(), 'glossary_tag', true );
$synonyms = [];
/** Add the main term to the 'additional terms' (as first in the array $synonyms) **/
$synonyms[] = sprintf( '<span class="glossary-additional-term">%s</span>', get_the_title() );
/** Make sure we have additional terms before we're going to loop through them **/
if ( '' != $additional_terms && null != $additional_terms )
{
/**
* Loop through the additional terms
*
* Whitespace before & after each (comma-separated) additional term is removed
**/
foreach ( explode( ',', $additional_terms ) as $additional_term )
$synonyms[] = sprintf( '<span class="glossary-additional-term">%s</span>', trim( $additional_term, " ") );
}
/**
* Add a spacer widget (height: 5px) and then the additional terms inside a p-element to $the_content
*
**/
$the_content .= sprintf( '<div class="ct-widget widget widget_block">
<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>
</div><p class="glossary-tags has-small-font-size">%s</p>', implode( ' ', $synonyms ) );
}
/**
* Return the contents of $the_content
*
* Omitting this would mean nothing will be displayed, on all posts/pages/etc!
**/
return $the_content;
}, 1, 9998);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment