Skip to content

Instantly share code, notes, and snippets.

@theukedge
Last active January 12, 2021 00:24
Show Gist options
  • Save theukedge/f9bf33317568c89462f6bafa366d3194 to your computer and use it in GitHub Desktop.
Save theukedge/f9bf33317568c89462f6bafa366d3194 to your computer and use it in GitHub Desktop.
<?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