Use wp_localize_script , It’s Amazing

Have you ever use wp_localize_script ?I'm going to share you a way to use PHP variable in javascript by using this amazing WordPress functionality. When you did this it enables your PHP code to be accessible on your registered script.

Here is a sample :

3 Important Parameters :

$handler - This the registered script that will handle the PHP variable that we attached to our javascript file.

$name - The name of our PHP variable container.

$data - This is mostly an Array if we want to attach multiple variables.

Example :

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.