Created
September 19, 2017 15:03
-
-
Save ahmedofali/708bacec7f20f5a0c78029ab30fd25e9 to your computer and use it in GitHub Desktop.
Wordpress Custom Taxonomy Additional Fields Used with any number of Taxonomies High Usability
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 | |
/** | |
* Base class | |
**/ | |
if ( ! class_exists( 'CustomTaxonomyAdditionalFields' ) ) { | |
abstract class CustomTaxonomyAdditionalFields { | |
protected $taxonomy_slug; | |
public function __construct( $taxonomy_slug ) | |
{ | |
$this->taxonomy_slug = $taxonomy_slug ; | |
$this->init(); | |
} | |
/* | |
* Initialize the class and start calling our hooks and filters | |
* @since 1.0.0 | |
*/ | |
public function init() { | |
add_action( "{$this->taxonomy_slug}_add_form_fields", array ( $this, 'add_additional_fields' ), 10, 2 ); | |
add_action( "created_{$this->taxonomy_slug}", array ( $this, 'save_additional_fields' ), 10, 2 ); | |
add_action( "{$this->taxonomy_slug}_edit_form_fields", array ( $this, 'update_taxonomy_add_additional_fields' ), 10, 2 ); | |
add_action( "edited_{$this->taxonomy_slug}", array ( $this, 'update_taxonomy_save_additional_fields' ), 10, 2 ); | |
add_action( 'admin_enqueue_scripts', array( $this, 'load_media' ) ); | |
add_action( 'admin_footer', array ( $this, 'add_script' ) ); | |
} | |
public function load_media() { | |
wp_enqueue_media(); | |
} | |
/* | |
* Add a form field in the new category page | |
* @since 1.0.0 | |
*/ | |
public function add_additional_fields ( $taxonomy ) { | |
do_action( "create_{$this->taxonomy_slug}_term_add_additional_form_fields" , $taxonomy ); | |
} | |
/* | |
* Save the form field | |
* @since 1.0.0 | |
*/ | |
public function save_additional_fields ( $term_id, $t_id ) { | |
do_action( "create_{$this->taxonomy_slug}_term_save_additional_form_fields" , $term_id, $t_id ); | |
} | |
/* | |
* Edit the form field | |
* @since 1.0.0 | |
*/ | |
public function update_taxonomy_add_additional_fields ( $term, $taxonomy ) { | |
do_action( "update_{$this->taxonomy_slug}_term_add_additional_form_fields" , $term, $taxonomy ); | |
} | |
/* | |
* Update the form field value | |
* @since 1.0.0 | |
*/ | |
public function update_taxonomy_save_additional_fields ( $term_id, $t_id ) { | |
do_action( "update_{$this->taxonomy_slug}_term_save_additional_form_fields" , $term_id, $t_id ); | |
} | |
/* | |
* Add script | |
* @since 1.0.0 | |
*/ | |
public function add_script() { | |
do_action( "create_or_update_{$this->taxonomy_slug}_custom_scripts" ); | |
} | |
} | |
} | |
/** | |
* Class to Use the base Class added actions | |
* | |
* Class anyTaxonomyAdditionalFields | |
*/ | |
class anyTaxonomyAdditionalFields extends CustomTaxonomyAdditionalFields{ | |
protected $taxonomy_slug = "custom_tax_name" ; | |
function __construct() | |
{ | |
parent::__construct( $this->taxonomy_slug ); | |
$this->add_actions(); | |
} | |
public function add_actions() | |
{ | |
add_action("{$this->taxonomy_slug}_add_form_fields", array( $this, "add_form_fields" ) ); | |
add_action("created_{$this->taxonomy_slug}", array( $this, "save_added_fields" ) ); | |
add_action("{$this->taxonomy_slug}_edit_form_fields", array( $this, "update_form_fields" ) ); | |
add_action("edited_{$this->taxonomy_slug}", array( $this, "save_updated_form_fields" ) ); | |
add_action("create_or_update_{$this->taxonomy_slug}_custom_scripts" , array( $this, "add_footer_script" ) ); | |
} | |
public function add_form_fields( $taxonomy ) | |
{ | |
// put html here | |
} | |
public function save_added_fields( $term_id, $t_id ) | |
{ | |
// save from $_POST Request | |
} | |
public function update_form_fields( $term ) | |
{ | |
// put html here | |
} | |
public function save_updated_form_fields( $term_id, $t_id ) | |
{ | |
// save from $_POST Request | |
} | |
public function add_footer_script() | |
{ | |
// js code if you want | |
} | |
} | |
new anyTaxonomyAdditionalFields(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment