Import js variables loaded via wp_localize_script() into js module without global scope connection
Import js variables loaded via wp_localize_script() into js module without global scope connection
Import js variables loaded via wp_localize_script() into js module without global scope connection
In your PHP you are localizing the script using wp_localize_script(). The way you’ve used it will create an object in JS named messageClip with the string as the copy property, but then in your JavaScript you’re not actually using this. In your JavaScript just get rid of const message etc. and use: buttonclip.on(‘success’, function(e){ e.trigger.textContent … Read more
wp_localize_script() doesn’t care much which script handle you use as long as it is a valid one. You could use your theme’s or Elementor’s main script handle with the function to make the data available for your script. You’ll need to check your theme’s or Elementors source coude to find what these handles are. Side … Read more
Use the JSON REST API – http://v2.wp-api.org – It lives as a plugin now but will eventually be part of WP Core – https://wordpress.org/plugins/rest-api/ To manually output JSON, use a combination of wp-ajax to handle requests and wp_json_encode() / wp_send_json_success for simple PHP Objects and Arrays. See https://codex.wordpress.org/AJAX_in_Plugins
If you mean when you access you variables in JS code just by name they are really undefined in JavaScript environment. In order to access variables in JS you need to type as in your example www_vars.whichcats instead of just whichcats Also it’s good to understand how wp_localize_script works. First param is your js file … Read more
This isn’t specific to WordPress, but I’ll answer it anyway 🙂 Don’t target the form by id, instead target it by its relationship to the button that was clicked. change: var str = $(“#ajaxForms”).serialize(); to: var str = $(this).parent().serialize();
It’s sort of a longshot, but maybe try something like this: $(‘#button-setup-league’).click(function() { $( “#dialog p” ).html(‘<span>’+wpslm_v_script_vars.delete_league+'</span>’); $( “#dialog” ).dialog({ modal: true }); }); This, of course, requires “#dialog p” to be initially empty, or assumes that its contents can be replaced entirely. If not, maybe you can use the solution above but with a … Read more
You can use the get_settings() member function: public function register_widget_scripts() { $options = $this->get_settings(); wp_localize_script( ‘widget-name’, ‘obj-name’, $options ); }
Your code looks correct. alert(wp_ajax.nonce); should display the nonce. You can also verify the existence of the wp_ajax object via console.log(wp_ajax) in your browser’s Javascript console. wp_localize_script adds the object(s) to the footer (a call to wp_footer() should be present in the theme’s footer.php file). It’ll look like so: <script type=”text/javascript”> /* <![CDATA[ */ var … Read more
Passing a location-dependent array via wp_localize_script within a shortcode