If this is your first visit, check our documentation. To post, register or login.
Forums / Upgrading to 2.x.x / Best Practice for Adding JS to Admin Page via Encapsulated Plugin?

Best Practice for Adding JS to Admin Page via Encapsulated Plugin?

Started by techiant on 23 Jun 2025, 4:28 AM · Views: 1,031

Results 1 to 6 of 6
23 Jun 2025, 4:28 AM
#1
techiant avatar

techiant

New Zenner

Join Date:
Oct 2008
Posts:
82
Plugin Contributions:
0

Best Practice for Adding JS to Admin Page via Encapsulated Plugin?

Hi folks,

I'm developing an encapsulated plugin for Zen Cart 2.1.x and am trying to find the correct way to add JavaScript to a core admin page, specifically options_name_manager.php. I've run into a load-order issue and would appreciate any insight.

Here is what I have tried so far:

init_script: My first attempt was to use the plugin's admin/includes/auto_loaders/config.my_plugin.php to load an init_script at priority 139. This script failed with a fatal error: Call to undefined function zen_add_admin_js(). This suggests the init_script from an encapsulated plugin runs before the core admin functions are available.

Observer Class: My second attempt was to use an observer class, loaded at priority 200. Diagnostic logging shows the class's __construct() method runs successfully. However, the update() method is never called when listening for either NOTIFY_HTML_HEAD_END or NOTIFY_ADMIN_PAGE_CONTENT_PRE_OUTPUT_OPTIONS_NAME_MANAGER. It seems the observer is instantiated but is not receiving notifications on this specific admin page.

The only method that has worked so far is to directly edit options_name_manager.php to either echo the <script> block or call zen_add_admin_js(). This works, but it is not an encapsulated solution.

My question is: What is the correct, modern, encapsulated way to add JavaScript to a core admin page like this? Is there a known load-order issue with observers on options_name_manager, or is there a different event/hook I should be using?

Thank you for any tips you can offer!

**Case Badges** by Techiant

23 Jun 2025, 4:54 AM
#2
techiant avatar

techiant

New Zenner

Join Date:
Oct 2008
Posts:
82
Plugin Contributions:
0

Re: Best Practice for Adding JS to Admin Page via Encapsulated Plugin?

OK, in case someone else has the same problem, digging through the docs helped me figure it out. This document was the key (this definitely needs a link/mention on the Encapsulated Plugins help page): https://docs.zen-cart.com/dev/admin/templating/

**Case Badges** by Techiant

23 Jun 2025, 8:20 AM
#3
techiant avatar

techiant

New Zenner

Join Date:
Oct 2008
Posts:
82
Plugin Contributions:
0

Re: Best Practice for Adding JS to Admin Page via Encapsulated Plugin?

Techiant:

OK, in case someone else has the same problem, digging through the docs helped me figure it out. This document was the key (this definitely needs a link/mention on the Encapsulated Plugins help page): https://docs.zen-cart.com/dev/admin/templating/

I've successfully added a few new options to a dropdown menu on the "Option Name Manager" page using my plugin's JavaScript. So, that part is working great – the dropdown now shows all the choices.

My main headache now is this: When an admin user edits one of these options, I need my JavaScript to automatically select the correct choice in that dropdown if the saved value in the database is one of my new ones. Zen Cart's built-in code only handles selecting the original options.

I've hit a wall trying to get the saved database value from PHP over to my JavaScript. Here's why it's tough:

I tried using Zen Cart's observer system to inject the value into the HTML. My PHP code for the observer runs (I see some initial debugs), but the actual HTML (like a hidden input or a script variable) never actually shows up in the browser's source code.

I then tried using AJAX. My JavaScript makes a call to Zen Cart's ajax.php dispatcher. Confirmed with the browser that the call is made. All I ever get back is a "400 Bad Request" with an empty response, and absolutely no PHP error logs on the server to tell me why Zen Cart is rejecting the request.

So, for those working with Zen Cart 2.1 encapsulated plugins, my core question is:

What's the officially sanctioned, or at least the most reliable, way for my plugin's PHP code on the admin side to pass a dynamic value from the database to my plugin's JavaScript, so I can pre-select a dropdown option? I need a method that respects the encapsulated plugin rules and actually works!

Thanks!

**Case Badges** by Techiant

23 Jun 2025, 12:44 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: Best Practice for Adding JS to Admin Page via Encapsulated Plugin?

  1. There is no Zen Cart function named zen_add_admin_js
  2. For examples of encapsulated plugins providing changes to 'base' Zen Cart admin pages, see the built-in /zc_plugins/POSM and Edit Orders 5 (https://www.zen-cart.com/downloads.php?do=file&id=2400)
  3. What you ended up with (a javascript 'extension') is certainly the simplest way to go, so long as it 'works' for you.
23 Jun 2025, 1:03 PM
#6
techiant avatar

techiant

New Zenner

Join Date:
Oct 2008
Posts:
82
Plugin Contributions:
0

Re: Best Practice for Adding JS to Admin Page via Encapsulated Plugin?

lat9:

  1. There is no Zen Cart function named zen_add_admin_js
  2. For examples of encapsulated plugins providing changes to 'base' Zen Cart admin pages, see the built-in /zc_plugins/POSM and Edit Orders 5 (https://www.zen-cart.com/downloads.php?do=file&id=2400)
  3. What you ended up with (a javascript 'extension') is certainly the simplest way to go, so long as it 'works' for you.

Thanks! I will take a look at these. I wish there was a better documentation

**Case Badges** by Techiant