Created
June 6, 2024 08:59
-
-
Save hazratbilal0079/8de2fcd1de69fdd42ae7eacb4288f8d5 to your computer and use it in GitHub Desktop.
Creating Terms with or without Parent from Frontend in WordPress
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
--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