Skip to content

Instantly share code, notes, and snippets.

@phoopee3
Created December 6, 2022 17:21
Show Gist options
  • Save phoopee3/0a6582df9fc2828d7b8272eacb5e3fe2 to your computer and use it in GitHub Desktop.
Save phoopee3/0a6582df9fc2828d7b8272eacb5e3fe2 to your computer and use it in GitHub Desktop.
wordpress plugin for havign a shortcode present a form for adding a term to a taxonomy - generated via openai chatbot
<?php
/*
Plugin Name: Category Term Updater
Prompt given : Write a wordpress plugin that will present a list of category terms,
and allow a person to update them via a form. The form will be presented on a page
on the front end of the site. The form should be implemented via a shortcode.
*/
function category_term_updater_shortcode() {
// Get the current category terms
$terms = get_terms( array(
'taxonomy' => 'category',
) );
// Build the form HTML
$form = '<form method="post" action="">';
foreach ( $terms as $term ) {
$form .= '<label>' . $term->name . '</label>';
$form .= '<input type="text" name="category_terms[' . $term->term_id . ']" value="' . $term->name . '" />';
}
$form .= '<input type="submit" value="Update Terms" />';
$form .= '</form>';
// Return the form HTML
return $form;
}
add_shortcode( 'category_term_updater', 'category_term_updater_shortcode' );
function category_term_updater_submit() {
// Check if the form has been submitted
if ( isset( $_POST['category_terms'] ) ) {
// Update each of the terms
foreach ( $_POST['category_terms'] as $term_id => $name ) {
wp_update_term( $term_id, 'category', array(
'name' => $name,
) );
}
}
}
add_action( 'admin_init', 'category_term_updater_submit' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment