Are elements auto-refreshed
-
I’m seeing in my log that the setUp() functions on all my elements are being called every few seconds. Is there a way to disable that?
For example, I have a page full of meta boxes, and I’m mainly using them for display info (not input fields) and Ajax to update them.
But the setUp() functions keep on getting called, even if there is no page activity, causing all the initial data to be reloaded and re-queried and slowing everything down.
I couldn’t find anything in the docs, so I’m hoping you have some insight into this. Thanks!
-
Hi,
Sounds like
admin-ajax.php
is calling thesetUp()
method. Instead oferror_log();
, you can useAdminPageFramework_Debug::log( 'your input' );
to see which page calls it. The log file is created in thewp-content
directory and log format is something like this.{date} {time} {elapsed time since last call} {page load id} {version} {metod name} {hook_name} {url} {input}
Then check the part
{url}
. Also, check{page load id}
to determine duplicate calls within a single page load.In order to prevent the class from loading its necessary components, you can override the
_isInstantiatable()
method in your extended class.For example, in order to stop the meta box class from doing anything in
admin-ajax.php
, you can do something like thisprotected function _isInstantiatable() { if ( isset( $GLOBALS[ 'pagenow' ] ) && 'admin-ajax.php' === $GLOBALS[ 'pagenow' ] ) { return false; } return true; }
Or, you can do something like
if ( ! isset( $GLOBALS[ 'pagenow' ] ) || 'admin-ajax.php' !== $GLOBALS[ 'pagenow' ] ) { new AdminPageFramework_PageMetaBox( ... ); }
Having said that, I’m guessing that you have some heavy tasks in the
setUp()
method. If you are not creating a form, thecontent()
method is another option to display custom outputs. This method is only called when the metabox output needs to be generated and not be called fromadmin-ajax.php
. In your metabox class, you can add something like this.public function content( $sContent ) { return $sContent . "<h3>TESTING</h3>"; }
Then the method is called automatically when only necessary.
Thanks. The
AdminPageFramework_Debug::log()
is so useful!Ajax is definitely calling the page’s
replyToLoadPage()
multiple times on each page load, which is instantiating all the Meta Boxes multiple times.Weird, since I disabled all my Ajax add_action and it’s still happening. In fact anything in the php outside of functions is also being executing. I’ll have to see what else is causing it.
Thanks also for the tip on moving logic to the
content()
function. I’ll do that when I find the source of this problem.-
This reply was modified 5 years, 8 months ago by
zuroma.
Also, is there a method anywhere to get the current page load id?
Ajax is definitely calling the page’s replyToLoadPage() multiple times on each page load, which is instantiating all the Meta Boxes multiple times.
I also ran some tests and it seems like a bug introduced in v3.8.14. Could you open an issue on the GitHub repository? I suppose it only occurs with page meta boxes. If you observe cases with other factory types, please add that information as well. Thank you.
Also, is there a method anywhere to get the current page load id?
The page load id that the debug method gives is just a made-up value to identify each page load. And it is a private method that generates the value so you cannot access it. You can do similar thing like
function getPageLoadID() { static $_sPageLoadID; $_sPageLoadID = $_sPageLoadID ? $_sPageLoadID : uniqid(); return $_sPageLoadID; }
Will do, and will look out for any future updates that resolve it. Thanks for your time and responses!
I thought there are multiple
setUp()
calls in a single page load but I cannot reproduce it now. I must have seen it wrong.If you can still reproduce it, let me know.
Something’s definitely triggering the load_page_slug action multiple times on my page during a cycle.
If I do nothing on the interface and just stare at the AdminPageFramework_Debug log, new lines from the page load get added in bursts every few seconds.
But I have no idea if it’s the Admin Page Framework or something else. I tried disabling most other plugins and it’s still there. I’ll keep digging.
Maybe, you should check the referrer of
admin-ajax.php
. You can insert something like this in yoursetUp()
method.AdminPageFramework_Debug::log( array( 'method' => __METHOD__, 'referrer' => wp_get_referer(), ) );
Can you post the log contents here? (You can remove the sensitive part like the site url domain)
Also, is this for a page mate box or a post meta box?
Thanks for the tip. Below is the log. I closed all other tabs to the server, and no one else is on it, and all I did was click the menu item to open the page once. The page contains about 10 page meta boxes. Those meta boxes’ setUps are no longer being called, since I moved their instantiation out of the page setUp and into into load_.
Also, is there a way to make hidden fields not take up space on the page?
2019/02/17 08:05:46.8651 +6.865 5c69865ad3390 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin.php?page=gf_userq_single_page (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (string, length: 81) https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 ) 2019/02/17 08:06:51.3232 +1.323 5c69869b4eef2 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin-ajax.php (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (string, length: 81) https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 ) 2019/02/17 08:08:51.3212 +1.321 5c6987134e6e7 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin-ajax.php (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (string, length: 81) https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 ) 2019/02/17 08:10:51.6079 +1.608 5c69878b94703 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin-ajax.php (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (string, length: 81) https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 ) 2019/02/17 08:12:51.8062 +1.806 5c698803c4da9 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin-ajax.php (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (string, length: 81) https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 ) 2019/02/17 08:14:51.5723 +1.572 5c69887b8bbd9 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin-ajax.php (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (string, length: 81) https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 ) 2019/02/17 08:16:51.6037 +1.604 5c6988f3936b2 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin-ajax.php (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (string, length: 81) https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 ) 2019/02/17 08:16:52.9653 +2.965 5c6988f4ebacc 3.8.18 AdminPageFramework_Debug::log wp_loaded https://site_url/wp-admin/admin.php?page=gf_userq_single_page&user_id=4 (array, length: 2) Array ( [method] => (callable) GFUserQueries::setUp [referrer] => (boolean) false )
The log indicates that no duplicate calls in a page load. They are all separate calls.
Most likely it is WordPress’s heartbeat.
Can you add the following method in your meta box class and see what happens?
protected function _isInstantiatable() { if ( isset( $GLOBALS[ 'pagenow' ] ) && 'admin-ajax.php' === $GLOBALS[ 'pagenow' ] ) { $_sAction = isset( $_POST[ 'action' ] ) ? $_POST[ 'action' ] : ''; if ( 'heartbeat' === $_sAction ) { return false; } } return true; }
Thanks, it all seems fine. It is indeed just the heartbeat and no extra calls being made.
I appreciate all your help and looking into this!
Glad to hear that.
I should have mentioned that you can also check the contents of
$_POST
inadmin-ajax.php
. The element of theaction
key holds the request action name.Besides
heartbeat
which is passed by the Heartbeat API, you may findclosed-postboxes
andmeta-box-order
as well. They are passed by WordPress core scripts that store meta box visibility states like the meta box positions and column layout configured with the screen option at the top right corner of the screen. Each time you change the meta box position or the column option, WordPress sends an Ajax request toadmin-ajax.php
to save the changes.I uploaded v3.8.19b03 and this will not call
setUp()
in Ajax page load called by those WordPress core actions. So please try that and see if excessivesetUp()
calls are gone.Awesome, so far the new beta version is not calling setUp from those actions.
The only minor issue is that now the side and normal page meta boxes are not aligned at the top of the page. See: https://imgur.com/a/qQALRRL
I cannot reproduce it.
I guess it is not specific to the development version posted above. For different issues, can you create a new topic? It helps future visitors to find their information.
Thank you.
-
This reply was modified 5 years, 8 months ago by
- The topic ‘Are elements auto-refreshed’ is closed to new replies.