Tag Info

Hot answers tagged

5

W3 Total Cache has 4 locations to include the minified files. Since wp_localize_script() hooks into wp_head() (unless you specify in footer in your enqueue) you can use the before </body> minify location and your script will have access to the variables set. On some occasions this has failed for me so I just exclude the script from minify.


5

You can simply put your init code within the constructor of the class. For example: class myWidget extends WP_Widget{ function myWidget(){ // Init code here } function widget( $args, $instance ) { // The widget code wp_enqueue_script(...); wp_enqueue_style(...); } // Over methods... } ...


4

wp_localize_script takes an array of data (in PHP) spits out a javascript. It's a way for you to add dynamic data for a specific script to the front end without having to hook into wp_head or wp_footer and echo it out yourself. More over, wp_localize_script outputs your data right above the script you enqueued. Hooking into wp_head or wp_footer won't do ...


3

The function wp_localize_script() is used to send variables to a script that has already been registered and enqueued. Do you have a js file that has been registered and enqueued and has the handle of 'ajax_URL'? If not, then that explains why it isn't working. Also, ajaxurl is already a js variable that is accessible via any scripts you enqueue, so I'm ...


3

You should be setting it to show in the footer with the register, so your code should look like this: wp_register_script( 'flowplayer_object', get_bloginfo( 'stylesheet_directory' ) . '/_/js/flowplayer/flowplayer-object-creator.js', array(), // these are your dependencies, if you need jQuery or something, it needs to go in that array false, ...


2

Your code is PHP4 style. PHP4 styled code should not be used anymore. And just putting some functions inside a class construct is not OOP. If you want to write reusable code, separate your code. For example: class Widget_Setup { public $widget_class = ''; public $admin_styles = array(); public $admin_scripts = array(); public ...


2

Ok you can do it this way. function my_ajax_scripts() { $data = array( 'ajaxurl' => admin_url('admin-ajax.php') ); wp_enqueue_script( 'ajax_url', get_stylesheet_directory_uri() . '/my-custom-ajax.js' ); wp_localize_script( 'ajax_url', 'MyAjax', $data ); wp_enqueue_script( 'ajax_url_2', get_stylesheet_directory_uri() . ...


2

One major principle of preventing security holes is "Escape late." That means that, ideally, esc_html() or esc_attr() should be used right when you echo or return the final HTML, not before. So, that's one thing that the example you gave gets wrong. As for the purpose of esc_html__(), as I understand it, is to prevent weird characters from translated ...


2

Sidestep the entire issue and just include fo_edit_script in the header of the page, rather than in the AJAX part. There's no need at all to do this. You might think that by only including it when its needed your optimising but your not, because it's having to load it every time you open an edit form. I'd go as far to say that what you're trying to do is ...


1

I don't know if there is really a right answer to this question, and I don't know what you find cumbersome about wp_localize_script, but you should be able to do it either way. The difference is that with wp_localize_script the data is printed to the source of the page, which you wouldn't want if the data were sensitive, and wp_localize_script should ...


1

I tested that code like this: wp_enqueue_script('jquery'); wp_localize_script( 'jquery', 'MS_Ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nextNonce' => wp_create_nonce( 'myajax-next-nonce' )) ); And it works but throws a Notice. It should be hooked to wp_enqueue_scripts like: function my_enqueue_scripts() { ...


1

It doesn't work because you haven't enqueued your script properly. If the script isn't printed, neither are the variables set by wp_localize_script. Please read a WP Codex entry on wp_localize_script function. You have to include a path to the script after the handle.


1

First issue- you can't access any data because your AJAX request is an entirely separate request from the one that loaded the page you're making the request from. This is not unique to WordPress. You have to pass the data you want to operate on along with your AJAX request. Second issue- calling your plugin file directly is not the correct way to handle ...


1

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 ...


1

How does core do it? After thinking again about it, I thought there might be a case where WP does the same thing internally. And right: It does it. Example ~/wp-admin/load-scripts.php $wp_scripts = new WP_Scripts(); wp_default_scripts($wp_scripts); // inside wp_default_scripts( &$scripts ) $scripts->add( $handle, $src, $dependencies, $version, ...


1

It really doesn't do much in that scenario. If you're going to leave that message as a string constant instead of something that is system or user generated, you're fine using one of the other translation functions: __() or _x(). esc_html__() is a little overkill for that particular line of code. Edit: The main reason for the esc_ functions is to protect ...


1

Good question, and one I've dealt with, but never quite satisfactorily. It would be very nice to figure out a core function that handles this. In the meantime, though, I would just bypass the whole localize_script function and add an action to wp_head that just defines a global javascript object with the info that all your plugins will need. localize_script ...


1

I do it this way and it works fine. Make sure you don't have jQuery defined anywhere else on your site (like manually in the header), as some browsers don't like that. /** * Load assets on initiation */ add_action('init', 'load_assets'); function load_assets(){ wp_enqueue_scripts('jquery'); }



Only top voted, non community-wiki answers of a minimum length are eligible