Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dantetesta/053ab6b8ca35014dbfe78e490b961b13 to your computer and use it in GitHub Desktop.
Save dantetesta/053ab6b8ca35014dbfe78e490b961b13 to your computer and use it in GitHub Desktop.
<?php
/*
Author: Dante Testa
Date: 26/06/2023
www.dantetesta.com.br
*/
add_action( 'jet-form-builder/custom-action/add_tax', function( $request, $action_handler ) {
if (!isset($request['nome']) || !isset($request['taxonomia'])) {
return;
}
$terms = array_map('sanitize_text_field', explode(',', $request['nome']));
$taxonomy = sanitize_text_field($request['taxonomia']);
if (!taxonomy_exists($taxonomy)) {
return;
}
foreach ($terms as $term) {
// Check if the term already exists
$existing_term = term_exists($term, $taxonomy);
// If the term doesn't exist and there was no error, create it
if ($existing_term === null) {
$result = wp_insert_term($term, $taxonomy);
if (is_wp_error($result)) {
// Handle the error here
}
}
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment