WordPress plugin options and jQuery

Variable can be passed to jQuery or any javascript using wp_localize_script. Example: add_action( ‘template_redirect’, ‘prefix_enqueue_vars’ ); function prefix_enqueue_vars() { $script_vars = array( ‘setting_1’ => get_option( ‘some_option’ ), ‘setting_2’ => get_option( ‘another_option’ ), ); wp_enqueue_script( ‘my_script’, ‘path/to/script.js’, array( ‘jquery’ ), true); wp_localize_script( ‘my_script’, ‘unique_name’, $script_vars ); } How to get the variables in your javascript: jQuery(document).ready(function($) … Read more

Creating custom AJAX requests

In WordPress the way of handling Ajax calls is a bit different the plain PHP but still very simple, all ajax calls should be to wp-admin/admin-ajax.php and you just define your own functions by hooks, Take a look at What’s the preferred method of writing AJAX in WordPress

Load comments per post on click with AJAX

Aha, I think my syntax was wrong. I changed: action: ‘do_ajax’, data: { ‘post_id’ : ’72’ //using a post id that I *know* has comments, for testing! }, to: data: { ‘action’ : ‘do_ajax’, ‘post_id’ : ’72’ }, and I’m getting the correct response from the console.log which shows: [{“comment_ID”:”1″,”comment_post_ID”:”1″,”comment_author”:”Mr WordPress”,”comment_author_email”:””,”comment_author_url”:”http:\/\/wordpress.org\/”,”comment_author_IP”:””,”comment_date”:”2012-08-28 19:55:20″,”comment_date_gmt”:”2012-08-28 19:55:20″,”comment_content”:”Hi, this is … Read more

Installing jQuery plugin to wordpress theme (Incredible)

You can load the scripts like this: function wpa_90150_scripts() { wp_enqueue_script( ‘contenthover’, get_template_directory_uri() . ‘/js/jquery.contenthover.js’, array(‘jquery’), ”, true ); wp_enqueue_script( ‘custom’, get_template_directory_uri() . ‘/js/custom.js’, array(‘jquery’,’contenthover’), ”, true ); } add_action(‘wp_enqueue_scripts’, ‘wpa_90150_scripts’); Which I think you’re mostly doing correctly, especially if they are being loaded on your site. And then initialize the jQuery scripts like this: … Read more

Add Infinite Scroll to Ajax Loaded Category Query

Update … Here’s the Final Code That Worked for Me This may not be perfect, but it seems to be working well for me with no errors. Feel free to add any suggestions for improvement if you have any. JS Code jQuery(document).ready(function($) { var count = 2; jQuery(“#la-chooser”).change(function() { loadArticlebyCat(this.value); }); function loadArticlebyCat(cat){ data = … Read more

Using an Image Slider twice on the same page

My approach (take it as such) would be simply generating a unique ID which will differentiate each slider. That’s anyway a good practice when the same functionality can potentially be displayed multiple times on the same page. First, find a way to generate an incremental $id to use in your template. Maybe an instanciation ID… … Read more

jQuery code not firing on page despite registering and enqueuing scripts

wp_enqueue_script() and wp_enqueue_style() should be called within a function attached to the wp_enqueue_scripts action: // Register scripts and styles. They can be optionally enqueued later on. add_action( ‘wp_loaded’, ‘wpse245419_register_scripts’ ); function wpse245419_register_scripts() { wp_register_style( ‘NLBBC_styles’, get_stylesheet_directory_uri() . ‘/NLBBC_styles.css’, array(), true ); wp_register_script( ‘service’, get_template_directory_uri() . ‘/js/service.js’, array( ‘jquery’ ), true ); } // Enqueue scripts … Read more