Last active
July 6, 2021 19:04
-
-
Save birgire/22a57ad1eed45e366cc1d47575bba0b1 to your computer and use it in GitHub Desktop.
WordPress Plugin: Add custom search fields to the term editor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Term Editor Search Field | |
* Description: Adds Search fields to the term editor. | |
* Plugin URI: https://gist.github.com/birgire/22a57ad1eed45e366cc1d47575bba0b1 | |
* Author: birgire | |
* Version: 1.0.2 | |
*/ | |
/** | |
* Setup custom term editor fields | |
*/ | |
add_action( 'load-term.php', function() | |
{ | |
/** | |
* Multiple Search Links | |
*/ | |
$fieldGoogle = new TermEditorSearchField; | |
// Adjust to your needs | |
$fieldGoogle | |
->setFieldLabel( 'Search' ) | |
->setFieldDescription( '' ) | |
->setSearchApi( 'https://www.google.de/search/?q=TERMNAME+logo&tbm=isch' ) | |
->setSearchApi( 'https://duckduckgo.com/?q=TERMNAME+logo' ) | |
->setSearchApi( 'https://plus.google.com/s/TERMNAME' ) | |
->display( 'post_tag' ); | |
} ); | |
/** | |
* Class TermEditorSearchField | |
* @since 1.0.0 | |
*/ | |
class TermEditorSearchField | |
{ | |
private $urls; | |
private $label; | |
private $description; | |
/** | |
* Collect search api urls | |
* | |
* @param string $url | |
* @return TermEditorSearchField | |
*/ | |
public function setSearchApi( $url = '' ) | |
{ | |
$this->urls[] = $url; | |
return $this; | |
} | |
/** | |
* Set field label | |
* | |
* @param string $label | |
* @return TermEditorSearchField | |
*/ | |
public function setFieldLabel( $label = '' ) | |
{ | |
$this->label = $label; | |
return $this; | |
} | |
/** | |
* Set field description | |
* | |
* @param string $description | |
* @return TermEditorSearchField | |
*/ | |
public function setFieldDescription( $description = '' ) | |
{ | |
$this->description = $description; | |
return $this; | |
} | |
/** | |
* Get Search Requests for a single term | |
* | |
* @param \WP_Term $term | |
* @return array $requests | |
*/ | |
public function getSearchRequestsForTerm( \WP_Term $term ) | |
{ | |
// Generate requests | |
foreach( $this->urls as $key => $url ) | |
{ | |
$url = str_replace( | |
array( 'TERMNAME', 'TERMSLUG' ), | |
array( $term->name, $term->slug ), | |
$url | |
); | |
$requests[] = $url; | |
} | |
return $requests; | |
} | |
/** | |
* Display by hooking into the dynamic {$term}_edit_form_fields filter | |
* | |
* @param string $term | |
* @return void | |
*/ | |
public function display( $term = 'post_tag' ) | |
{ | |
add_filter( "{$term}_edit_form_fields", array( $this, 'add_field' ), 10, 2 ); | |
} | |
/** | |
* Add table field | |
* | |
* @param \WP_Term $term | |
* @param array $taxonomy | |
* @return void | |
*/ | |
public function add_field( $term, $taxonomy ) | |
{ | |
if( ! ( $term instanceof \WP_Term ) ) | |
return; | |
// Generate the search links list | |
$li = ''; | |
$requests = $this->getSearchRequestsForTerm( $term ); | |
foreach( (array) $requests as $request ) | |
{ | |
$li .= sprintf( | |
'<li><a href="%s" target="blank">%s</a></li>', | |
esc_url( $request ), | |
esc_url( $request ) | |
); | |
} | |
// Display the table field | |
printf( | |
'<tr class="form-field term-search-wrap"> | |
<th scope="row"><label for="search">%s</label></th> | |
<td> | |
<ul class="term-search-list">%s</ul> | |
<p class="description">%s</p></td> | |
</td> | |
</tr>', | |
esc_html( $this->label ), | |
$li, | |
esc_html( $this->description ) | |
); | |
} | |
} // end class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment