• Is there anyway that I can auto refresh a single admin page? Backend pages such as order listing would be nice to auto load. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You can execute this javascript to reload the page:
    window.location.assign(window.location.href);

    But something needs to trigger it. Add an event listener perhaps?

    To get your script loaded call wp_enqueue_script() from a callback hooked to “admin_enqueue_scripts” action.

    The problem will be the browser will reload from its cache instead of fetching the page from the server. When the page initially loads we need to send a Cache-Control header telling the browser not to cache the page. This can be added in the “wp_headers” filter, but depending on several factors, this may not work without further adjustments to other headers, .htaccess, httpd.conf, etc.

    Thread Starter yuanfamily

    (@yuanfamily)

    Thanks for the reply, bcworkz.

    If it is a regular HTML page, I can use meta refresh tag to refresh. However, for a plugin which calls edit.php?post_type=<something>, how do I add any meta or javascript?

    Moderator bcworkz

    (@bcworkz)

    To get your javascript loaded call wp_enqueue_script() from a callback hooked to “admin_enqueue_scripts” action. To add tags in the head section, one way is to hook the “admin_head” action and output the tag from the callback. This will be just before the closing </head>. Checkout wp-admin/admin-header.php for other possible hooks that fire earlier.

    In some cases you can selectively control which page the hook fires for by determining the value for $hook_suffix of the page you are targeting. In other cases any conditional application needs to be done within the callback.

    I imagine the meta refresh will have the same cache issue that the script does. As you are likely aware, there is also a no-cache meta tag you could use. Managing it through headers is preferable IMO, but trickier to code.

    Thread Starter yuanfamily

    (@yuanfamily)

    Thanks again for the reply, bcworkz.

    I am a java guy, I know HTML too, but this is the first time I tried to customize a wordpress plugin. When user navigates to https://<domain>/wp-admin/edit.php?post_type=<something&gt;, how does it generate the order page? Where and how to I put the hook?

    I really appreciate your detailed help.

    Moderator bcworkz

    (@bcworkz)

    edit.php first loads admin-header.php, which handles all the head section content. It then fetches an instance of the WP_Posts_List_Table class. The object’s prepare_items() method is called, which executes a query for appropriate orders. The the display() method is later called which loops through the query results and outputs the orders for the page.

    The code I’ve been referring to needs to be placed somewhere safe from updates. Your only reasonable options are your own custom plugin or a child theme of your chosen theme. You can also place all future custom code you might come up with in the same place. I’ve found that once one learns how to tweak things in WP through hooks it’s hard to stop! There will be more custom code in your future ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Auto Refresh Admin Page’ is closed to new replies.