On this page
wp_add_inline_script( string $handle, string $data, string $position = 'after' ): bool
Adds extra code to a registered script.
Description
Code will only be added if the script is already in the queue.
Accepts a string $data containing the Code. If two or more code blocks are added to the same script $handle, they will be printed in the order they were added, i.e. the latter added code can redeclare the previous.
See also
Parameters
$handlestring Required-
Name of the script to add the inline script to.
$datastring Required-
String containing the JavaScript to be added.
$positionstring Optional-
Whether to add the inline script before the handle or after. Default
'after'.Default:
'after'
Return
bool True on success, false on failure.
Source
File: wp-includes/functions.wp-scripts.php. View all references
function wp_add_inline_script( $handle, $data, $position = 'after' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
if ( false !== stripos( $data, '</script>' ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: <script>, 2: wp_add_inline_script() */
__( 'Do not pass %1$s tags to %2$s.' ),
'<code><script></code>',
'<code>wp_add_inline_script()</code>'
),
'4.5.0'
);
$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
}
return wp_scripts()->add_inline_script( $handle, $data, $position );
}
Related
Uses
| Uses | Description |
|---|---|
| stripos() wp-includes/class-pop3.php | |
| WP_Scripts::add_inline_script() wp-includes/class-wp-scripts.php | Adds extra code to a registered script. |
| wp_scripts() wp-includes/functions.wp-scripts.php | Initialize $wp_scripts if it has not been set. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| _doing_it_wrong() wp-includes/functions.php | Marks something as being incorrectly called. |
Used By
| Used By | Description |
|---|---|
| block_editor_rest_api_preload() wp-includes/block-editor.php | Preloads common data used with the block editor by specifying an array of REST API paths that will be preloaded for a given block editor context. |
| enqueue_editor_block_styles_assets() wp-includes/script-loader.php | Function responsible for enqueuing the assets required for block styles functionality on the editor. |
| the_block_editor_meta_boxes() wp-admin/includes/post.php | Renders the meta boxes forms. |
| WP_Privacy_Policy_Content::notice() wp-admin/includes/class-wp-privacy-policy-content.php | Add a notice with a link to the guide when editing the privacy policy page. |
| wp_enqueue_code_editor() wp-includes/general-template.php | Enqueues assets needed by the code editor for the given settings. |
| WP_Widget_Text::_register_one() wp-includes/widgets/class-wp-widget-text.php | Add hooks for enqueueing assets when registering all widget instances of this widget class. |
| WP_Widget_Media_Gallery::enqueue_admin_scripts() wp-includes/widgets/class-wp-widget-media-gallery.php | Loads the required media files for the media manager and scripts for media widgets. |
| WP_Widget_Custom_HTML::_register_one() wp-includes/widgets/class-wp-widget-custom-html.php | Add hooks for enqueueing assets when registering all widget instances of this widget class. |
| WP_Widget_Custom_HTML::enqueue_admin_scripts() wp-includes/widgets/class-wp-widget-custom-html.php | Loads the required scripts and styles for the widget control. |
| WP_Widget_Text::enqueue_admin_scripts() wp-includes/widgets/class-wp-widget-text.php | Loads the required scripts and styles for the widget control. |
| WP_Widget_Media_Audio::enqueue_admin_scripts() wp-includes/widgets/class-wp-widget-media-audio.php | Loads the required media files for the media manager and scripts for media widgets. |
| WP_Widget_Media_Video::enqueue_admin_scripts() wp-includes/widgets/class-wp-widget-media-video.php | Loads the required scripts and styles for the widget control. |
| WP_Widget_Media_Image::enqueue_admin_scripts() wp-includes/widgets/class-wp-widget-media-image.php | Loads the required media files for the media manager and scripts for media widgets. |
| wp_localize_jquery_ui_datepicker() wp-includes/script-loader.php | Localizes the jQuery UI datepicker. |
| WP_Customize_Widgets::enqueue_scripts() wp-includes/class-wp-customize-widgets.php | Enqueues scripts and styles for Customizer panel and export data to JavaScript. |
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_add_inline_script