Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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