How to enqueue script based on post category?

Inside your theme’s functions.php add something like this: function my_conditional_enqueue_script() { global $post; if (is_single($post->ID) && in_category(‘mouse’, $post->ID)) { wp_enqueue_script(‘mousescript’); } } add_action(‘wp_enqueue_scripts’, ‘my_conditional_enqueue_script’); Also, make sure you use wp_register_script before you attempt to enqueue.

Add custom form to theme

There are few ways how to make you’r form visible on your site: crate shortcode/widget with form create page template There are few ways how to process your form in php: adding processing directly to php file of page template adding processing in init/wp or any other action adding processing in ajax actions How i … Read more

Turn jQuery.ajax() request into XMLHttpRequest (vanilla JavaScript)

Here’s my take on it, although a bit late, but I am working on converting all jQuery.ajax() calls to vanilla JavaScript, so why not. // alpha JS request // no jQuery // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send#Example_POST // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest var request = new XMLHttpRequest(); request.open(‘POST’, fpm_ajax.ajax_url, true); request.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded;’); request.onload = function () { if (this.status >= 200 && … Read more

lazy load comments wordpress on click

Templates like the comments template need to be loaded on the page, as it relies on some global variables to know what post to load the comments for, so just pulling in the template with Javascript won’t work like that. You’ll need to create an AJAX wrapper that sets up the post data before getting … Read more

How to use the php if statement [closed]

You may do it this way using open/close bracket {} <?php if ( 1==2) { ?> <textarea> “Add multiple lines of text here and watch the scrollbars grow.”> </textarea> <?php } ?> In general rules you need to enclose all the if else statement using php tag and the html outside it. You may also … Read more

Vue.js + AJAX Shortcode

My solution was simpler than I thought…. Since I’m just trying to execute shortcodes in the frontend, I can just localize the script and pass the shortcodes as variables: functions.php wp_localize_script( ‘BUILD-script’, ‘sharedData’, array( ‘ajax_url’ => admin_url( ‘admin-ajax.php’ ), ‘mailchimpForm’ => do_shortcode(‘[mc4wp_form id=”100″]’), ‘contactForm’ => do_shortcode(‘[contact-form-7 id=”98″ title=”Contact form 1″]’) ) ); Basically I’m jus … Read more

Integrating PHP into Javascript to display map markers with Google API

Update 2: <?php $storeData = []; $args = [‘post_type’ => ‘store’]; $loop = new WP_Query($args); foreach ($loop->posts as $post) { $storeData[] = [ ‘title’ => apply_filters(‘the_title’, $post->post_title), ‘lat’ => types_render_field(‘lat’, [‘output’ => ‘raw’]), ‘long’ => types_render_field(‘long’, [‘output’ => ‘raw’]), ]; } wp_localize_script(‘jquery-core’, ‘storeData’, $storeData); ?> <?php get_header(); ?> <!– Row for main content area –> … Read more