Opened 10 years ago
Closed 10 years ago
#31636 closed defect (bug) (fixed)
4.2-beta1 - PHP warning when enqueuing script on specific pages only
Reported by: | harmr | Owned by: | ocean90 |
---|---|---|---|
Milestone: | 4.2 | Priority: | normal |
Severity: | normal | Version: | 4.2 |
Component: | Script Loader | Keywords: | has-patch |
Focuses: | Cc: |
Description
Within my plugin I use the following code to enqueue scripts only on specific pages:
[...] $page3 = add_submenu_page('leafletmapsmarker_markers', $menu_name . ' - ' . __('add/edit marker', 'lmm'), '<img src="' . LEAFLET_PLUGIN_URL_ENC . 'inc/img/icon-menu-add' . $mp6_icon . '.png"> ' . __('Add new marker', 'lmm'), $lmm_options[ 'capabilities_edit' ], 'leafletmapsmarker_marker', array(&$this, 'lmm_marker') ); [...] add_action('admin_print_scripts-'.$page3, array(&$this, 'lmm_admin_enqueue_scripts_jquerydatepicker')); [...] function lmm_admin_enqueue_scripts_jquerydatepicker() { $plugin_version = get_option('leafletmapsmarker_version_pro'); wp_enqueue_script( array ( 'jquery', 'jquery-ui-tabs','jquery-ui-datepicker','jquery-ui-slider' ) ); wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL_ENC . 'inc/js/jquery-ui-timepicker-addon.js', array('jquery', 'jquery-ui-tabs','jquery-ui-datepicker'), $plugin_version); } [...]
Since 4.2 I am getting the following PHP warning when accessing $page3 (=create new marker page):
WARNING: wp-includes/functions.wp-scripts.php:227 - explode() expects parameter 2 to be string, array given require_once('wp-admin/admin-header.php'), do_action('admin_print_scripts-maps-marker-pro_page_leafletmapsmarker_marker'), call_user_func_array, l4->lmm_admin_enqueue_scripts_jquerydatepicker, wp_enqueue_script, explode
I looked into the changed function.wp-scripts.php file to find out whats wrong, but did not succeed:
function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) { $wp_scripts = wp_scripts(); _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); $_handle = explode( '?', $handle ); if ( $src ) { $wp_scripts->add( $_handle[0], $src, $deps, $ver ); } if ( $in_footer ) { $wp_scripts->add_data( $_handle[0], 'group', 1 ); } $wp_scripts->enqueue( $handle ); }
The line
$_handle = explode( '?', $handle );
is responsible for creating this PHP warning.
Unfortunately I do not have an idea how to prevent this within my code.
Could a solution be to change that code into
$_handle = !is_array($_handle) : explode( '?', $handle ) ? $handle;
As I try not to generate any php error log entries with my plugin, any help here would be really appreciated!
Attachments (4)
Change History (15)
#2
@
10 years ago
wp_enqueue_script( array ( 'jquery', 'jquery-ui-tabs','jquery-ui-datepicker','jquery-ui-slider' ) ); wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL_ENC . 'inc/js/jquery-ui-timepicker-addon.js', array('jquery', 'jquery-ui-tabs','jquery-ui-datepicker'), $plugin_version);
The first line is redundant, you should add 'jquery-ui-slider'
to your second line, and that would be enough:
wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL_ENC . 'inc/js/jquery-ui-timepicker-addon.js', array( 'jquery', 'jquery-ui-tabs', 'jquery-ui-datepicker', 'jquery-ui-slider' ), $plugin_version );
#5
follow-up:
↓ 6
@
10 years ago
Thx! I changed my function to
function lmm_admin_enqueue_scripts_jquerydatepicker() { $plugin_version = get_option('leafletmapsmarker_version'); wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL . 'inc/js/jquery-ui-timepicker-addon.js', array( 'jquery', 'jquery-ui-tabs', 'jquery-ui-datepicker', 'jquery-ui-slider' ), $plugin_version ); }
but that did not eliminate the warning. Then I tried to apply the patch, but I guess there is a bug in it - shouldnt it be
if ( ! is_array( $handle ) ) { $_handle = explode( '?', $handle ); }
instead of
if ( ! is_array( $_handle ) ) { $_handle = explode( '?', $handle ); }
(as this threw an error?)
#7
follow-up:
↓ 8
@
10 years ago
- Keywords has-patch added
I think we should simply revert the code as in comment:1. Makes it consistent with wp_enqueue_style()
.
#8
in reply to:
↑ 7
;
follow-up:
↓ 9
@
10 years ago
Replying to ocean90:
I think we should simply revert the code as in comment:1. Makes it consistent with
wp_enqueue_style()
.
That would reintroduce #14488 though?
Looks like sorich87's patch there was more correct, should we reopen the ticket and commit that one instead?
#9
in reply to:
↑ 8
@
10 years ago
Replying to SergeyBiryukov:
That would reintroduce #14488 though?
Indeed. Seems like I have missed [31028] and not quite a fan of it. Anyway, I agree that 14488.diff:ticket:14488 is slightly better than 31636.2.patch.
It's
wp_enqueue_script( array ( 'jquery', 'jquery-ui-tabs','jquery-ui-datepicker','jquery-ui-slider' ) );
which is a "hidden feature" becauseWP_Dependencies->enqueue()
supports an array. You should use script dependencies for this.In 4.1: