Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hazratbilal0079/8de2fcd1de69fdd42ae7eacb4288f8d5 to your computer and use it in GitHub Desktop.
Save hazratbilal0079/8de2fcd1de69fdd42ae7eacb4288f8d5 to your computer and use it in GitHub Desktop.
Creating Terms with or without Parent from Frontend in WordPress
--Hook Name
create_terms
--PHP Hook
add_action( 'jet-form-builder/custom-action/create_terms', function( $request, $action_handler ) {
$terms = explode(',', $request['terms']);
$taxonomy = $request['tax']; // Replace with your actual taxonomy name
foreach ($terms as $term) {
// Check if the term already exists
$existing_term = term_exists($term, $taxonomy);
// If the term doesn't exist, create it
if ($existing_term === 0 || $existing_term === null) {
$args = [];
// Check if the taxonomy is 'building-name-for-v2'
if ($taxonomy === 'location') {
$parent_id = $request['select_parent']; // Get parent ID from the request
// Add the parent ID to the term arguments
if (!empty($parent_id)) {
$args['parent'] = intval($parent_id);
}
}
// Insert the term with the specified taxonomy and arguments
wp_insert_term($term, $taxonomy, $args);
}
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment