Pass jquery variable to php function and run it

Tested this locally and found the issues. When you reg/enq the JS, you need to localize the script. This is your chance to pass anything from PHP to the script. In this case, you should, at minimum, pass the WP ajax url like this: wp_localize_script( ‘my_js_library’, ‘my_local_var’, array( ‘ajax_url’ => admin_url( ‘admin-ajax.php’ ) ) ); … Read more

Orderby post__in Not Working Correctly?

If you look at the source of the Ajax Load More plugin, you can see a number of filters you can use to modify the query. You’ll likely want to use the alm_modify_query_args filter like so: $in_array = [ 115, 123, 66, 64 ]; add_filter( ‘alm_modify_query_args’, function( $args ) use ( $in_array ) { $args[ … Read more

Target a specific page/template in jQuery

Approach 1 Use the body class. all templates will have a class representing them. ie: body.archive, body.single, body.home, etc. It will even have specific tax/post type classes for more granular control. Wrap your js function in something that checks for that body class. The following should do the trick: jQuery if ( $( ‘body’ ).first().hasClass( … Read more

bundled jquery in theme js not working with wp_localize_script

You can’t use wp_localize_script with jquery. this function use the WP_Scripts::localize() function which have a condition right in the start. You can see it in the file \wp-includes\class.wp-scripts.php line 414 if ( $handle === ‘jquery’ ) $handle=”jquery-core”; You can do something else like add the script with other action like wp_head if your script is … Read more