Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When adding a new tag in the admin interface, WordPress doesn't reload the page. It appears to send the data with an ajax call, clear the fields and add the tag to the list on the right.

I am working with a custom taxonomy that operates like tags. I have added a custom field to that taxonomy. When the user adds a new term, the data in my custom field is not cleared.

I have tried using jQuery to clear the fields when the submit button is clicked, but of course this clears the fields even if the tag is not added (if, for instance, the name field hasn't been filled out).

Does WordPress offer any callback or hook-like system on the Javascript/jQuery side of things which I could use to execute a function when a new tag is added?

I believe I can hack a solution by searching for any fields with the class 'form-invalid' and not clearing my data. But I thought I would check first to see if there is a more stable and future-proof solution.

Thanks.

share|improve this question

2 Answers 2

Could you do something like this?

function tag-added-script()
{
    ?>
<script>

jQuery(function($) {
    $('.tagadd').click(function() {
        // Run Your Jquery Here
        alert('test');
    });
});
</script>
    <?php
}
add_action( 'admin_head', 'tag-added-script' );

The above will react whenever the "tagadd" button is clicked. Theoretically you could then grab the custom field ID and clear it via jquery.

share|improve this answer
    
Thanks for the response, Howdy_McGee, but as I mentioned I have tried this route. The problem is that it will still clear the field even if the user fails to enter a name (and the tag is not actually added). I've also tried binding to the form's submit event, but that also fires even if WordPress stops the submission because of a missing name. I guess I could try this route but throw in a check on the name field so my field isn't cleared if the name field is empty. I will try that out... –  NateWr Nov 1 '13 at 11:06
up vote 0 down vote accepted

After some experimentation, I found the best hack to be:

    $('#addtag #submit').click(function () {

    // Look for a div WordPress produces for an invalid form element
    if (!$('#addtag .form-invalid').length) {
        // hide elements and reset hidden field
    }
});

Binding to the click event on the submit button captures both click submissions and submissions where the user hits enter in a text field. The latter were not captured when bound to a submit event on the form element.

Checking for the .form-invalid element is not a perfect solution, as WordPress could easily change the class name in the future. But I found it the most comprehensive at catching an empty name field or a duplicate name -- neither of which WordPress will allow to go forward.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.