I have a function hooked on created_term
and I'm trying to add a message a return success or error message. Categories are created with Ajax, I tried some things but it's overwritting Wordpress error and causing bugs.
My code so far:
add_action( 'created_term', 'push_term', 20, 3);
function push_term( $term_id, $tt_id, $taxonomy ) {
$contentarray = array();
$term = get_term_by( 'id', $term_id, $taxonomy);
if(!empty($term->name)){ $contentarray['category_name'] = $term->name; }
if(!empty($term->slug)){ $contentarray['category_slug'] = $term->slug; }
if(!empty($term->description)){ $contentarray['category_desc'] = $term->description; }
$category = $taxonomy.'_#'.$term_id;
if(post_content($apikey, $websitekey, $category, $contentarray)){
if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX )
{
}
}else{
if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX )
{
}
}
}
Thanks in advance, been trying to figure this out for a while...
So the categories in WordPress are created by AJAX already, and from my knowledge you cannot 'hijack' another AJAX call just like that (that would be a big security risk if you could).
The way I did it was with the following code placed in a category.js
file that I enqueued like this in theme's functions.php
file:
/********* Backend Scripts ***********/
if ( !function_exists( 'mythemeslug_backend_scripts' ) ) {
function mythemeslug_backend_scripts($menu_slug) {
if ($menu_slug == 'edit-tags.php') {
wp_enqueue_script( 'mythemeslug_cat_check', get_template_directory_uri().'/js/category.js' );
}
}
}
In the category.js
I added this code:
jQuery(document).ready(function($){
$('#submit').on('click', intercept_ajax);
function intercept_ajax(){
var $this = $(this);
//Name input field check
var cat_name_input_value = $('#tag-name').val();
var cat_names_array = [];
$('#the-list .name').each(function(){
var cat_name = $(this).find('.row-title').attr('aria-label');
if ( cat_name !== undefined ) {
cat_names_array.push(cat_name.match(/“(.*?)”/)[1]);
}
});
if($('.field_notice').length){
$('.field_notice').remove();
}
if (!cat_name_input_value) {
$('#tag-name').after('<p class="field_notice">Field empty!</p>');
} else{
if($.inArray(cat_name_input_value, cat_names_array) != -1) {
$('#tag-name').after('<p class="field_notice">Category '+cat_name_input_value+ ' already exists!</p>');
} else{
$('#tag-name').after('<p class="field_notice">Category '+cat_name_input_value+ ' created!</p>');
}
}
}
});
So this code will only work on edit-tags.php
page (category screen). Every time you click the submit button an AJAX call is made, so we can add our code on same submit click event.
In the intercept_ajax
function you can add your checks. First you get the value of the name field, and you need to get all the existing names in the table on the right. That's why I put all those names in an array. Here the thing to notice is that the .text()
or .html()
will return the names, but if you have subcategories, or sub subcategories you'll have —
dash in front. So I pick the name out of the aria-label
and since the name is in the curly quotes I just place the content within it in the array for check (I used simple regex check for curly quotes).
Then you check if the notice field exists - if it does remove it to avoid duplicate messages. And then the checks begin - first one is for the 'emptiness' of the field. If it's not empty, move on. Then if your name already exists - give that message. If not, give a success message.
This is it.
You can even prevent default action if the check fails - this should stop ajax from firing using
event.preventDefault();
You'll need to put event
in
function intercept_ajax(event)
Also you can localize your success and fail messages using wp_localize_script().
I hope this is what you were looking for. Hope this helped.