add_inline_script example

sprintf(
                /* translators: 1: <script>, 2: wp_add_inline_script() */
                __( 'Do not pass %1$s tags to %2$s.' ),
                '<code>&lt;script&gt;</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 );
}

/** * Registers a new script. * * Registers a script to be enqueued later using the wp_enqueue_script() function. * * @see WP_Dependencies::add() * @see WP_Dependencies::add_data() * * @since 2.1.0 * @since 4.3.0 A return value was added. * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array. * * @param string $handle Name of the script. Should be unique. * @param string|false $src Full URL of the script, or path of the script relative to the WordPress root directory. * If source is set to false, script is an alias of other scripts it depends on. * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL * as a query string for cache busting purposes. If version is set to false, a version * number is automatically added equal to current installed WordPress version. * If set to null, no version is added. * @param array|bool $args { * Optional. An array of additional script loading strategies. Default empty array. * Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. * * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. * } * @return bool Whether the script has been registered. True on success, false on failure. */
if ( is_string( $dependencies ) ) {
            $handle       = $dependencies;
            $dependencies = array();
        }

        $path    = "/wp-includes/js/dist/vendor/$handle$suffix.js";
        $version = $vendor_scripts_versions[ $handle ];

        $scripts->add( $handle$path$dependencies$version, 1 );
    }

    did_action( 'init' ) && $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );

    did_action( 'init' ) && $scripts->add_inline_script(
        'moment',
        sprintf(
            "moment.updateLocale( '%s', %s );",
            get_user_locale(),
            wp_json_encode(
                array(
                    'months'         => array_values( $wp_locale->month ),
                    'monthsShort'    => array_values( $wp_locale->month_abbrev ),
                    'weekdays'       => array_values( $wp_locale->weekday ),
                    
Home | Imprint | This part of the site doesn't use cookies.