0

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.

1
  • For future readers, see this answer.
    – Walf
    Commented Aug 26, 2018 at 13:55

2 Answers 2

0

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.

1
  • 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
    Commented Nov 1, 2013 at 11:06
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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