The use of wp_localize_script is an API in WordPress is to retrieve PHP values from the database to use in javascript/jquery scripts, it can also be utilized to make string values language translatable if required, but also used to pass user defined variables/settings say from a plugin to load via a jQuery init file for a jQuery plugin. By default the values are retrieved as strings even the boolean and integer values. How wp_localize_script works   Thewp_localize_script function takes 3 arguments, the $handle is the javascript/jquery init script file name that we need to target that will use the variables that we need to pass, the $name is a prefix value that is appended to the PHP values we are referencing which can be anything and the $data is the actual PHP values we are passing from the database. In the above is a plugin example that is using wp_localize_script so the $handle it is using is the ‘jsinit‘ script this is the script that we wish to transfer our PHP variables to. The $name is set to ‘phpVars‘ which will be used to prefix the PHP variables. The $data is set in an array and is pulling out 4 values from our ‘wpb_stored_options_test‘
Share This