Title: Is there an utility for admin_enqueue_scripts in class.settings-api.php ?
Last modified: May 22, 2024

---

# Is there an utility for admin_enqueue_scripts in class.settings-api.php ?

 *  Resolved [mathieu](https://wordpress.org/support/users/mathieu/)
 * (@mathieu)
 * [2 years ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/)
 * Hi,
   I wonder if there is an utility for that function and if there is… it should
   be smarter as it is loading on every page view even those who has no media uploader.
 * I noticed because I have a website with 20000+ images and when monitoring, this
   query shows up way too often.
   **SELECT** **DISTINCT** YEAR( post_date ) **AS**
   year, MONTH( post_date ) **AS** month**FROM** wp_posts**WHERE** post_type = ‘
   attachment’**ORDER** **BY** post_date **DESC**
 * It is triggered on the wp_enqueue_media() of admin_enqueue_scripts in class.settings-
   api.php.
 * I don’t see why you would enqueue the media uploader. By definition, in every
   instance that Stockpack would be triggered, it would already be loaded.
 * I have used this code to disable it and Stockpack is still working fine.
 *     ```wp-block-code
           add_action('admin_init', function () {
               if (class_exists('StockpackAdmin')) {
                   $stockpack_admin = StockpackAdmin::get_instance();
                   if (isset($stockpack_admin->settings_api)) {
                       remove_action('admin_enqueue_scripts', array($stockpack_admin->settings_api, 'admin_enqueue_scripts'));
                   }
               }
           }, 999);
       ```
   
 * Let me know what you think about this.
 * Best regards

Viewing 12 replies - 1 through 12 (of 12 total)

 *  Plugin Contributor [ionut.calara](https://wordpress.org/support/users/ionutcalara/)
 * (@ionutcalara)
 * [2 years ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17809376)
 * Hi [@mathieu](https://wordpress.org/support/users/mathieu/) sorry for the delay
   in response, I was out of the office,
 * This issue is triggered by the third party settings class I am using, and it 
   would only impact the color picker for settings, which I am not even using, but
   I didn’t notice it, and I didn’t modify the third party script. So removing this
   should be a good thing and I will investigate this deeper to see if I could disable
   it directly from the plugin.
 * I will update this post when an update for the plugin that fixes this is ready.
 * Thank you for reporting this issue!
 *  Thread Starter [mathieu](https://wordpress.org/support/users/mathieu/)
 * (@mathieu)
 * [2 years ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17809387)
 * Thanks 🙂
 *  Plugin Contributor [ionut.calara](https://wordpress.org/support/users/ionutcalara/)
 * (@ionutcalara)
 * [1 year, 12 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17812452)
 * Hey [@mathieu](https://wordpress.org/support/users/mathieu/) the latest release,
   3.3.9 automatically removes the action right after the class that adds them is
   called, so you can safely update and remove the code that you added.
 * Thank you for the report once again, I will close this issue, but feel free to
   reopen if needed.
 *  Thread Starter [mathieu](https://wordpress.org/support/users/mathieu/)
 * (@mathieu)
 * [1 year, 12 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17824319)
 * Hi,
 * Great, however, I discovered that it creates other issue on pages that doesn’t
   have the media player loaded. You seem to load pretty much everything on everypage
   even if there is no upload happening on a page.
 *     ```wp-block-code
       stockpack-load-admin.js?ver=3.2.4:1 Uncaught TypeError: Cannot read properties of undefined (reading 'search')    at Object.<anonymous> (stockpack-load-admin.js?ver=3.2.4:1:20702)    at i (stockpack-load-admin.js?ver=3.2.4:1:110)    at Module.<anonymous> (stockpack-load-admin.js?ver=3.2.4:1:51573)    at i (stockpack-load-admin.js?ver=3.2.4:1:110)    at Object.<anonymous> (stockpack-load-admin.js?ver=3.2.4:1:51525)    at i (stockpack-load-admin.js?ver=3.2.4:1:110)    at stockpack-load-admin.js?ver=3.2.4:1:903    at stockpack-load-admin.js?ver=3.2.4:1:912
       ```
   
 * I do however have a fix AND a tricks that solves the issue.
 * The idea is to check if did_action(“wp_enqueue_media”) is true
 * In class-stockpackmedia.php, you can do this, it works
 *     ```wp-block-code
               public function enqueue($admin = true, $no_dialog = false)        {            if (did_action('wp_enqueue_media')) {                $this->enqueue_script($admin);                $this->enqueue_style($no_dialog);                $this->enqueue_settings($admin);            }        }
       ```
   
 * But I think there’s a better way that keeps your exact same logic and doesn’t
   messed up anything.
 * The idea is to use stockpack_frontend_load later when enqueue scripts are called.
   That makes the “frontend_load_stockpack” filter much more useful.
 * Here’s what I tested successfuly.
 *     ```wp-block-code
               public function enqueue($admin = true, $no_dialog = false)        {            if (stockpack_frontend_load()) {                $this->enqueue_script($admin);                $this->enqueue_style($no_dialog);                $this->enqueue_settings($admin);            }        }                        public function templates()        {            if (stockpack_frontend_load()) {                include_once(__DIR__ . '/../templates/attachment.php');                include_once(__DIR__ . '/../templates/empty.php');                include_once(__DIR__ . '/../templates/details.php');                include_once(__DIR__ . '/../templates/downloader.php');                include_once(__DIR__ . '/../templates/dialog.php');                include_once(__DIR__ . '/../templates/attribution.php');                include_once(__DIR__ . '/../templates/no-search.php');            }        }        public function enqueue_frontend()        {            if (stockpack_frontend_load()) {                $this->enqueue(false);            }        }        public function load_frontend_style()        {            if (stockpack_frontend_load()) {                wp_enqueue_style('stockpack-frontend', plugins_url('/dist/css/stockpack-frontend.css', STOCKPACK_DIR), false, $this->version);            }        }
       ```
   
 * And then somebody can filter it out with :
 *     ```wp-block-code
           add_filter("frontend_load_stockpack", function ($load) {        if (!did_action("wp_enqueue_media")) {            return false;        } else {            return $load;        }    });
       ```
   
 * Let me know what you think
 *  Plugin Contributor [ionut.calara](https://wordpress.org/support/users/ionutcalara/)
 * (@ionutcalara)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17825208)
 * Hi [@mathieu](https://wordpress.org/support/users/mathieu/) thank you for looking
   into this, the `frontend_load_stockpack` was initially called later, but was 
   changed to improve compatibility with builders plugin that also work on the FE.
 * I will test the changes and see how I can make it also work with the builders
   and get back with a plugin update by the end of next week.
 *  Thread Starter [mathieu](https://wordpress.org/support/users/mathieu/)
 * (@mathieu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17825360)
 * Thanks 🙂 please let me know.
 * Have a nice weekend.
 *  Plugin Contributor [ionut.calara](https://wordpress.org/support/users/ionutcalara/)
 * (@ionutcalara)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17832936)
 * [@mathieu](https://wordpress.org/support/users/mathieu/) I have updated the plugin.
   I couldn’t easily change the loading to be later, as I needed to register the
   settings pages earlier, but I was able to not load the css and js if the `wp_enqueue_media`
   action was not called in the stockpackmedia class. This also removes the javascript
   error that you mentioned.
 * Thank you for diving deeper into this, let me know if you have any questions 
   or issues.
 * I am closing this ticket, but feel free to reopen.
 *  Thread Starter [mathieu](https://wordpress.org/support/users/mathieu/)
 * (@mathieu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17833080)
 * Yes it works well but you are still loading a tons of templates for nothing.
 *     ```wp-block-code
               public function templates()        {            if (did_action('wp_enqueue_media') || (isset($_GET['page']) && $_GET['page'] === 'stockpack')) {                include_once(__DIR__ . '/../templates/attachment.php');                include_once(__DIR__ . '/../templates/empty.php');                include_once(__DIR__ . '/../templates/details.php');                include_once(__DIR__ . '/../templates/downloader.php');                include_once(__DIR__ . '/../templates/dialog.php');                include_once(__DIR__ . '/../templates/attribution.php');                include_once(__DIR__ . '/../templates/no-search.php');            }        }
       ```
   
 * Otherwise it works very well.
 * Hopes I am not getting too annoying.
 * I am just in a phase where I am optimizing everything and your plugin is good
   enough that I am willing to go deep in the code to help you make it better.
 * Cheers.
 *  Plugin Contributor [ionut.calara](https://wordpress.org/support/users/ionutcalara/)
 * (@ionutcalara)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17834823)
 * [@mathieu](https://wordpress.org/support/users/mathieu/) I think this can definitely
   be added, but to make sure that the plugin only runs in just the situations it
   needs to run I will probably have to split the settings part from the rest of
   the plugin, which is not super easy at this time. The main focus of the plugin
   was compatibility, the performance footprint should now be considerably reduced
   if the scripts are not loaded, but I will also add this about templates soon.
   Are you also concerned about the php running on pages that it’s not needed?
    -  This reply was modified 1 year, 11 months ago by [ionut.calara](https://wordpress.org/support/users/ionutcalara/).
 *  Thread Starter [mathieu](https://wordpress.org/support/users/mathieu/)
 * (@mathieu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17839433)
 * If the php would noticably slow down the experience for the people I pay to write
   content, absolutely.
   But I don’t feel that is the case with your plugin anymore
   since you stop enqueuing the media player everywhere.If every plugin would always
   load everything on every call with no regards to performance it would get bloated
   quick.I run a tight ship 🙂
 *  Plugin Contributor [ionut.calara](https://wordpress.org/support/users/ionutcalara/)
 * (@ionutcalara)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17843108)
 * I removed the templates, so the performance footprint should be no longer noticeable.
   As you mentioned loading scripts and styles is what is that makes the experience
   for people in the admin worse.
 * Thank you for pointing these issues out, this will make a difference for all 
   the users.
 *  Thread Starter [mathieu](https://wordpress.org/support/users/mathieu/)
 * (@mathieu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17843300)
 * Thanks a lot 🙂 Have a nice weekend!

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Is there an utility for admin_enqueue_scripts in class.settings-api.php?’
is closed to new replies.

 * ![](https://ps.w.org/stockpack/assets/icon-256x256.png?rev=2206382)
 * [StockPack – Stock photos from Unsplash, Adobe Stock and more](https://wordpress.org/plugins/stockpack/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/stockpack/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/stockpack/)
 * [Active Topics](https://wordpress.org/support/plugin/stockpack/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/stockpack/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/stockpack/reviews/)

 * 12 replies
 * 2 participants
 * Last reply from: [mathieu](https://wordpress.org/support/users/mathieu/)
 * Last activity: [1 year, 11 months ago](https://wordpress.org/support/topic/is-there-an-utility-for-admin_enqueue_scripts-in-class-settings-api-php/#post-17843300)
 * Status: resolved