term_upload_name = 'term_image_id';
// Image fields and actions
add_action( $this->slug . '_add_form_fields', [ $this, 'add_taxonomy_image' ] );
add_action( 'created_' . $this->slug, [ $this, 'save_taxonomy_image' ] );
add_action( $this->slug . '_edit_form_fields', [ $this, 'update_taxonomy_image' ] );
add_action( 'edited_' . $this->slug, [ $this, 'updated_taxonomy_image' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'load_media_scripts' ] );
add_action( 'admin_footer', [ $this, 'add_image_script' ] );
}
/**
* Add a form field in the new taxonomy page
*
* @since 1.0.0
*/
public function add_taxonomy_image() {
wp_nonce_field( 'tax_image_nonce_action', 'tax_image_nonce' );
?>
term_upload_name ] ) && ( '' !== $_POST[ $this->term_upload_name ] ) ) {
add_term_meta( $term_id, $this->term_upload_name, absint( $_POST[ $this->term_upload_name ] ), true );
}
}
/**
* Edit the form field
*
* @since 1.0.0
*/
public function update_taxonomy_image( $term ) {
wp_nonce_field( 'tax_image_nonce_action', 'tax_image_nonce' );
?>
|
|
term_id, $this->term_upload_name, true );
?>
|
term_upload_name ] ) && ( '' !== $_POST[ $this->term_upload_name ] ) ) {
update_term_meta( $term_id, $this->term_upload_name, absint( $_POST[ $this->term_upload_name ] ) );
} else {
update_term_meta( $term_id, $this->term_upload_name, '' );
}
}
/**
* Enqueue scripts
*
* @since 1.0.0
*/
public function load_media_scripts() {
// Check if we're on a taxonomy page
if ( ! isset( $_GET['taxonomy'] ) ) {
return;
}
// Check if the current taxonomy is one of our dynamic taxonomies
$current_taxonomy = sanitize_text_field( $_GET['taxonomy'] );
$is_dynamic_taxonomy = strpos( $current_taxonomy, 'adqs_' ) === 0 &&
( strpos( $current_taxonomy, '_categories' ) !== false ||
strpos( $current_taxonomy, '_locations' ) !== false ||
strpos( $current_taxonomy, '_tags' ) !== false );
// Also check for our specific taxonomy patterns
if ( ! $is_dynamic_taxonomy ) {
$is_dynamic_taxonomy = preg_match( '/^[a-z0-9_-]+_(category|location|tag)$/', $current_taxonomy );
}
if ( ! $is_dynamic_taxonomy ) {
return;
}
wp_enqueue_media();
}
public function add_image_script() {
// Check if we're on a taxonomy page
if ( ! isset( $_GET['taxonomy'] ) ) {
return;
}
// Check if the current taxonomy is one of our dynamic taxonomies
$current_taxonomy = sanitize_text_field( $_GET['taxonomy'] );
$is_dynamic_taxonomy = strpos( $current_taxonomy, 'adqs_' ) === 0 &&
( strpos( $current_taxonomy, '_categories' ) !== false ||
strpos( $current_taxonomy, '_locations' ) !== false ||
strpos( $current_taxonomy, '_tags' ) !== false );
// Also check for our specific taxonomy patterns
if ( ! $is_dynamic_taxonomy ) {
$is_dynamic_taxonomy = preg_match( '/^[a-z0-9_]+_(category|location|tag)$/', $current_taxonomy );
}
if ( ! $is_dynamic_taxonomy ) {
return;
}
?>