Support » Developing with WordPress » wp_insert_term refresh page

  • When a user creates a term (category or tag) in dashboard I use pre_insert_term hook to modify the values and I manually insert term using the wp_insert_term function. This approach does not update the terms that are displayed in the user’s dashboard. The user has to reload the page to see the new term.

    Is there a way to make the newly created term appear for the user without having to refresh manually?

    • This topic was modified 6 days, 17 hours ago by owhited.
Viewing 6 replies - 1 through 6 (of 6 total)
  • you can use meta to refresh your site after few second’s

    I want to refresh immediately (and only once, not periodically) after the term is inserted.

    @bcworkz
    This is my code structure,

    add_filter('pre_insert_term', my_function);
    function my_function() {
        wp_insert_term();
        header("Location: ".$_SERVER['PHP_SELF']);
    }

    I tried different variants like header("Refresh: 3;"); The php function header() doesn’t seem to run.

    Is it possible to use ajax to update the newly created term (created through a code in functions.php) in the user’s dashboard without having to reload page?

    • This reply was modified 5 days, 21 hours ago by owhited.
    Moderator bcworkz

    (@bcworkz)

    PHP Location: headers will not work once any output has occurred. When you try, it generates a “Headers already sent” warning in your error log. When developing custom code, consider defining WP_DEBUG as true in wp-config.php. You’ll then see warnings right on the page so you can take corrective action. Just remember to set it as false on any publicly accessible site if you are not actively developing code.

    You can selectively refresh a page with JavaScript after any particular event. If you are refreshing only to update a meta value, using Ajax would be an ideal approach. Admin area Ajax is very similar to front end Ajax. The principal difference is your JavaScript gets enqueued from the “admin_enqueue_scripts” action instead of “wp_enqueue_scripts”.

    BTW, please don’t @ reference other members unless they are already involved in your topic. It’s contrary to the concept of community support to seek out the attention of a particular member. It’s even seen by some as a bit spammy. We know @ references are common practice in other forums, hence it’s tolerated if one is already contributing to a topic. It really unnecessary even then since members can subscribe to topics in order to receive notifications if they so choose. Moderators use @ references to help ensure members get notice of important moderator communication. Other usage is really unnecessary.

    Apologies. 🙂

    enqueing scripts and ajax seems more work. Nevertheless I am going to try it. If only pre_insert_term had the ability to modify slug!

    Moderator bcworkz

    (@bcworkz)

    You can add tags that way, but it will not help with page refresh. The point of Ajax is to avoid page refresh. Specific elements on the page are updated via script as a result of the Ajax call instead of requiring the refresh of the entire page.

    It is indeed more work to accomplish, but usually makes for a better user experience. It’s an important technique that anyone keen on coding websites should be able to utilize, so it’s worth learning how to do it regardless of it’s specific applicability here.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.