Skip to content

Instantly share code, notes, and snippets.

@laxmariappan
Last active November 27, 2022 12:52
Show Gist options
  • Save laxmariappan/d49b5930875d1d8f134173742f37774d to your computer and use it in GitHub Desktop.
Save laxmariappan/d49b5930875d1d8f134173742f37774d to your computer and use it in GitHub Desktop.
Quick demo of how to use did_filter hook in WordPress
<?php
/*
Plugin Name: Did filter Check
Plugin URI: https://gist.github.com/laxmariappan/d49b5930875d1d8f134173742f37774d
Description: Check how did filter works
Version: 1.0
*/
add_filter('lax_plugin','plugin_title_change');
/**
* Prepend category name the post title
*
* @param string $post_title
* @param integer $post_id
* @return string $post_title
* @since 6.1
* @author Lax Mariappan <lax@webdevstudios.com>
*/
function plugin_title_change(string $post_title, int $post_id)
{
if( 'post' === get_post_type( $post_id ) ){
$cat = get_the_category();
$category = $cat[0]->cat_name;
return "($category) ".$post_title;
}
return $post_title;
}
add_filter('the_title', 'theme_title_change', 20, 2);
/**
* Check if lax_plugin filter has ran and Prepend category name the post title.
*
* @param string $post_title
* @param integer $post_id
* @return string $post_title
* @since 6.1
* @author Lax Mariappan <lax@webdevstudios.com>
*/
function theme_title_change(string $post_title, int $post_id)
{
if( 'post' !== get_post_type( $post_id ) ){
return $post_title;
}
if( did_filter( 'lax_plugin' ) ){
return $post_title;
}
$cat = get_the_category();
$category = $cat[0]->cat_name;
return "($category) ".$post_title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment