WordPress Plugin Get Taxonomy Name From ID


About The Author
Avatar Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







WordPress Plugin Get Taxonomy Name From ID

Here’s a pretty useful code snippet for you today in the form of a WordPress plugin.  Recently, I had a client where we implemented some advanced search functionality on the site.  The search was run by several variables passed through the URL such as:

/search-results/?taxid=35&taxtype=45&term=wordpress&region=14659

The client also wanted to use Google Analytics to track the search terms being queried to see the most popular searches on the site.  Once again, no problem to setup but then we ran into a snag.  With URLs like the above passing the data as a taxonomy term ID, how would my client know what the actual taxonomy term was?  After some deliberation, I decided to write a simple plugin that would add a page to the wp-admin area and allow the client to find a taxonomy term name based on the ID.

Below is the plugin source:

<?php
/**
* Plugin Name: Get Tax Term Name
* Plugin URI: https://www.boldcitydesign.com
* Description: Lookup a taxonomy term name by ID
* Version: 1.0 
* Author: Cabe Nolan
* Author URI: https://www.boldcitydesign.com
* License: GPL12
*/

/*---------------------------------------------------
register settings
----------------------------------------------------*/
function theme_settings_init(){
register_setting( 'theme_settings', 'theme_settings' );
}
 
/*---------------------------------------------------
add settings page to menu
----------------------------------------------------*/
function add_settingss_page() {
add_submenu_page( 'tools.php', 'Name From ID', 'Name From ID', 'manage_options', 'run-function', 'run_function' );
}

/*---------------------------------------------------
add actions
----------------------------------------------------*/
add_action( 'admin_init', 'theme_settings_init' );
add_action( 'admin_menu', 'add_settingss_page' );

function run_function() {
	
	
?>
<h1>Get Term Name From ID</h1>
<?php
	if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "update_general" && check_admin_referer( 'verify_update_general', 'nonce_update_general' )) { 
		$taxid = strip_tags(htmlspecialchars($_POST['tax-id']));
		
		if(!empty($taxid)) { 
			$regionterm = get_term_by('id', $taxid, 'region');
			if(!empty($regionterm)) {
				$foundid = $regionterm->name;
			}
			$serviceterm = get_term_by('id', $taxid, 'service');
			if(!empty($serviceterm)) {
				$foundid = $serviceterm->name;
			}
			$typeterm = get_term_by('id', $taxid, 'type');
			if(!empty($typeterm)) {
				$foundid = $typeterm->name;
			}
		}
	}
?>
<?php if(!empty($foundid)) { ?>
<div style="font-size: 24px; font-weight: 700; padding: 20px 0;">
The term is named "<?php echo $foundid; ?>".
</div>
<?php } ?>
<form class="" id="run-function" role="form" action="" method="post">
	<input type="text" name="tax-id">
	<input type="submit" name="submit-general" value="Get ID Name" class="btn btn-primary copyeditor">
	<input type="hidden" name="action" value="update_general" />
	<?php wp_nonce_field('verify_update_general','nonce_update_general'); ?>
</form>	
	

<?php
}

Overall, the plugin is fairly simple. If you want to utilize this functionality, you will need to modify each recurrence of the following with your custom taxonomy name:

$typeterm = get_term_by('id', $taxid, 'type');
			if(!empty($typeterm)) {
				$foundid = $typeterm->name;
			}

There’s definitely better ways to write this plugin but it worked well for my scenario and I was in a big time crunch. Hope it helps!


Share Your Thoughts

Leave a Reply

Your email address will not be published. Required fields are marked *


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!