Last active
January 12, 2021 00:24
-
-
Save theukedge/f9bf33317568c89462f6bafa366d3194 to your computer and use it in GitHub Desktop.
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 | |
function invman_generate_location_tag( $term_id, $term_tax_id ) { | |
$existing_tag = get_term_meta( $term_id, 'invman_location_tag' ); | |
if ( $existing_tag ) { | |
return; | |
} | |
$characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; | |
$characters_length = strlen( $characters ); | |
$random_string = 'B'; | |
for ( $i = 0; $i < 3; $i++ ) { | |
$random_string .= $characters [ wp_rand( 0, $characters_length - 1 ) ]; | |
} | |
/** | |
* Now check if this tag already exists | |
*/ | |
$existing_tags = get_terms( | |
array( | |
'taxonomy' => 'inventory_location', | |
'meta_key' => 'invman_location_tag', | |
'meta_value' => $random_string, | |
) | |
); | |
if ( $existing_tags ) { | |
return invman_generate_location_tag( $term_id, $term_tax_id ); | |
} else { | |
add_term_meta( $term_id, 'invman_location_tag', $random_string ); | |
return $random_string; | |
} | |
} | |
add_action( 'edited_inventory_location', 'invman_generate_location_tag', 10, 2 ); | |
add_action( 'created_inventory_location', 'invman_generate_location_tag', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment