how to send Ajax request in wordpress backend

When you enqueue or localize a script you’re doing it specifically for the front end or the admin. If you want to enqueue or localize a script in both, you have to specifically do it for both. This is used to enqueue/localize for the front end add_action( ‘wp_enqueue_scripts’, ‘your_function_front’ ); your_function_front() { wp_localize_script(‘scripts’, ‘myAjax’, array( … Read more

Localization of JavaScript which is only used in one page

You can add conditional statements inside the wp_enqueue_scripts action hook. add_action( ‘wp_enqueue_scripts’, function() { if( is_page( ‘page-slug’ ) ) { // Enqueued script with localized data. wp_enqueue_script( ‘register-validation-js’ ); } } ); You can refer to the is_page documentation for more information on what arguments can be pass.

Get Current User info using wp_localize_script, in functions.php

If you read about this action here. You will see that this hook provides access to two parameters, $user->user_login (string) and $user ( WP_User ). Using it like this you will see all the info you have access to: function add_to_login($user_login, $user) { echo “<pre>”.print_r($user, true).”</pre>”; } add_action(‘wp_login’, ‘add_to_login’, 10, 2); But after a little … Read more

wp_add_inline_script not adding when script_loader_tag filtered

The reason this is happening is because the markup that is filtered by script_loader_tag includes the inline scripts. So when you filter it and replace all the HTML tag for a particular script, your filter is removing those inline script tags. If you print out the original value of $tag from within your filter you … Read more

Can wp_localize_script be used within a shortcode?

Default parameters for wp_enqueue_script will output the script in the wp_head function. Shortcodes typically execute later, when main content is being output. In that case, calling wp_localize_script in the Shortcode handler will have no effect, because the script has already been output. Setting the $in_footer parameter to true when enqueueing the script will delay script … Read more

How to Globally Use wp_localize_script() Ajax URL

You can conditionally echo the code on only few templates or specific pages. Here is an example: add_action ( ‘wp_head’, ‘my_js_variables’ ); function my_js_variables(){ // for specific page templates $current_template = get_page_template(); // return if there is no page template, or if the page template is other than template-x1.php or template-x2.php if( !isset($current_template) || ( … Read more

How to get the post ID when creating JS variables with localize_script

You should declare global $post; before attempting to access this variable, but to answer your question regarding when it is created, the ‘wp’ action hook is the safest bet. As such I’d suggest the following in your functions.php file as a simple solution function my_localize_post_id(){ global $post; wp_register_script( ‘your_script’… /** other parameters required here **/ … Read more