Posts

Showing posts with the label Plugin Development

Wordpress - Ajaxurl Not Defined On Front End

Answer : In backend there is global ajaxurl variable defined by WordPress itself. This variable is not created by WP in frontend. It means that if you want to use AJAX calls in frontend, then you have to define such variable by yourself. Good way to do this is to use wp_localize_script . Let's assume your AJAX calls are in my-ajax-script.js file, then add wp_localize_script for this JS file like so: function my_enqueue() { wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') ); wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); } add_action( 'wp_enqueue_scripts', 'my_enqueue' ); After localizing your JS file, you can use my_ajax_object object in your JS file: jQuery.ajax( { type: "post", dataType: "json", url: my...

Wordpress - Correct Way To Enqueue Jquery-ui

Answer : First of all, WordPress registers jQuery UI via wp_default_scripts() . Dependencies are already set, so you only need to enqueue the script you really need (and not the core). Since you're not changing version number or anything, it is ok to only use the handle. // no need to enqueue -core, because dependancies are set wp_enqueue_script( 'jquery-ui-widget' ); wp_enqueue_script( 'jquery-ui-mouse' ); wp_enqueue_script( 'jquery-ui-accordion' ); wp_enqueue_script( 'jquery-ui-autocomplete' ); wp_enqueue_script( 'jquery-ui-slider' ); As for the stylesheets: WordPress does not register jQuery UI styles by default! In the comments, butlerblog pointed out that according to the Plugin Guidelines Executing outside code within a plugin when not acting as a service is not allowed, for example: Calling third party CDNs for reasons other than font inclusions; all non-service related JavaScript and CSS must be included locally This means loading ...