Skip to content

Instantly share code, notes, and snippets.

@phoopee3
Last active December 6, 2022 17:56
Show Gist options
  • Save phoopee3/650f67fde1b1ebbd42954c493c65e005 to your computer and use it in GitHub Desktop.
Save phoopee3/650f67fde1b1ebbd42954c493c65e005 to your computer and use it in GitHub Desktop.
Another AI generated WP plugin - this time to add a term to the taxonomy

Prompt

Write a wordpress plugin that will allow a person to add a term to the category taxonomy. The form will ask the user for the name of the category. The form will be presented on a page on the front end of the site. The form should be implemented via a shortcode. The form should submit the data via admin-ajax.

jQuery(function($) {
// listen for the form submission
$('#add-category-term-form').on('submit', function(e) {
e.preventDefault();
// get the category name
var categoryName = $('#category-name').val();
// make the AJAX request
$.ajax({
type: 'POST',
url: add_category_term_ajax_url,
data: {
action: 'add_category_term',
'category-name': categoryName
},
success: function(response) {
// check for errors
if (response.success) {
// show a success message
alert('Category added: ' + response.data.name);
} else {
// show the error message
alert(response.data.message);
}
}
});
});
});
<?php
/*
Plugin Name: Add Category Term
Description: Allows users to add terms to the category taxonomy via a front-end form.
*/
// register the shortcode
function add_category_term_shortcode() {
// create the form HTML
$html = '<form id="add-category-term-form">';
$html .= '<label for="category-name">Category Name:</label>';
$html .= '<input type="text" id="category-name" name="category-name">';
$html .= '<input type="submit" value="Add Category">';
$html .= '</form>';
// return the form HTML
return $html;
}
add_shortcode('add_category_term', 'add_category_term_shortcode');
// register the AJAX action for the form submission
function add_category_term() {
// check that the user has permission to add a term
if (!current_user_can('manage_categories')) {
wp_send_json_error(array('message' => 'You do not have permission to add a term to the category taxonomy.'));
}
// get the submitted category name
$category_name = sanitize_text_field($_POST['category-name']);
// check that the category name is not empty
if (empty($category_name)) {
wp_send_json_error(array('message' => 'Please enter a category name.'));
}
// add the term to the category taxonomy
$term = wp_insert_term($category_name, 'category');
// check for errors
if (is_wp_error($term)) {
wp_send_json_error(array('message' => $term->get_error_message()));
}
// return the term ID and name
wp_send_json_success(array('term_id' => $term['term_id'], 'name' => $category_name));
}
add_action('wp_ajax_add_category_term', 'add_category_term');
// enqueue the script that handles the form submission
function add_category_term_enqueue_scripts() {
wp_enqueue_script('add-category-term', plugins_url('add-category-term.js', __FILE__), array('jquery'));
wp_localize_script('add-category-term', 'add_category_term_ajax_url', admin_url('admin-ajax.php'));
}
// need to add the hook to enqueue the scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment