Can anyone tell me what the “$handle
” ( first parameter ) of wp_localize_script
is normally used for. Thanks.
P.s.: I have no idea why but stackexchange is telling me this question dosen’t meet quality standards.
Edit: When i put in my ps it accepted it so i suppose it’s the length of the quesion…. if you feel like this is an unacceptable question then apologies
It’s basically a unique id of the script you registered or enqueued before.
Let’s say we enqueued a two scripts with wp_enqueue_script():
Now you want to pass your
$data
to the script #2 with wp_localize_script():After this – when WordPress prints out your
my_script_2
it will print out your$data
variables along with it so you can use them in your script.As you can see
my_script_2
is our handle here – it makes the link between our script functions.The handle is arbitrary (as in not technically meaningful) name of script that is used for human convenience when declaring it with
wp_register_script()
/wp_enqueue_script()
.So first you declare script (or look up handle of already declared one) and then you can use handle to assign localization object to it with
wp_localize_script()
$handle
is for all thescript
andstyle
functions the given name.Then you can use it like this: