Make WordPress Core

Opened 7 years ago

Last modified 2 years ago

#41558 new enhancement

create a (reusable) wp_default_admin_scripts

Reported by: drzraf's profile drzraf Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.6
Component: Administration Keywords:
Focuses: ui, administration Cc:

Description

I had to create an admin-like form in the frontend, for guests submitting content.
I needed to provide meta/post-box, autocompletion and needed media-views, wp-color-picker, ...

I thought wp_enqueue_script() would do the trick... until I discovered the if(is_admin()) inside wp_default_scripts(). I had no other choice than copy/pasting ~210 WP LoC in my own PHP files

Is there a really good reason for reserving all that cool client-side stuff to admin dashboard only?
plugin/themes developers may want to load and use it for good and having all that libs already registered by core is useful.

I suggest:

  1. moving all the admin-oriented script into wp_default_admin_scripts() which should be reusable (even if non-admin), and run it if (is_admin).
  2. moving all the localize() corresponding to these admin scripts into another wp_default_admin_scripts_settings() function somehow wrapped by if(! is_admin())
  3. Document how to correctly bootstrap the dashboard in the frontend and a couple of common gotchas of these scripts (like that ajax-dashboard-pingback feature forcefully pulled as JS-dependencies and which can't be stopped, ...)

Change History (4)

#1 @Clorith
7 years ago

  • Version changed from 4.8.1 to 2.6

#2 @dingo_bastard
7 years ago

Can't you just enqueue default scripts listed in wp_enqueue_script() instead of loading every script that is loaded on admin screen?

#3 @drzraf
7 years ago

1) to my knownledge $script->add() just make scripts available without actually enqueing them
2) for wp_enqueue_script() to work, scripts would have to be registered. They are not.
3) I do enqueue selectively. This is what I enqueue to get a roughly working dashboard-style page avaiable to the frontend (it's acf_form() based):

  foreach( [ 'thickbox',
             'media-views',
             'imgareaselect',
             'colors',
             'ie',
             'utils',
             'svg-painter',
             'acf-global',
             'wp-auth-check',
             'acf-input',
             'acf-pro-input',
             'select2',
             'acf-datepicker',
             'acf-timepicker',
             'buttons',
             'wp-color-picker' ] as $s) {
    wp_enqueue_style($s);
  }

But still, I *had* to copy the 210 LoC of wp_default_admin_scripts in a custom wp_enqueue_scripts hook in order to get these styles actually added.

#4 @drzraf
2 years ago

  • Component changed from Themes to Administration

ping

Note: See TracTickets for help on using tickets.