Using the WordPress and WooCommerce REST APIs in the same Node app

I thought I’d share my solution for anyone working on a similar problem. I ended up switching to node-wpapi and using REST Autodiscovery to enable use of the WooCommerce & WordPress APIs from a single library. This can be done like so: const wpapi = require(‘wpapi’) let wp = new wpapi({ endpoint: ‘http://your.wp.site/wp-json/’, username: ‘user’, … Read more

Add crossorigin to SCRIPT tag

script_loader_tag or script_loader_src filters are there to let you tweak the HTML of the script easily so you can add custom attributes: add_filter(‘script_loader_tag’, function($tag, $handle){ switch ( $handle ) { case ‘foo’: $tag = preg_replace( ‘/src=[\’|”|]/i’, ‘crossorigin $0’, $tag ); break; } return $tag; }, 10, 2); To avoid conflicting with other plugins, pass unique … Read more

Incrementing PHP variable onclick to display posts by month

Try registering a function handling ajax calls first. // For logged out users add_action( ‘wp_ajax_nopriv_wp280265_pagination’, ‘wp280265_pagination’ ); // For logged in users add_action( ‘wp_ajax_wp280265_pagination’, ‘wp280265_pagination’ ); Define the function. function wp280265_pagination() { $month = $_POST[‘month’]; $year = $_POST[‘year’]; if ($month != ” && $year != ”) get_posts($month, $year); } Define JavaScript. (function($) { $(document.body).on(‘click’, ‘#previous’, … Read more

Issues loading Jquery on WordPress website

Don’t enqueue your own version of jQuery. You’re just going to cause conflict issues with other plugins and there’s nothing in your script that requires a different version. Even if you were, just load the https:// version, there’s no reason to bother checking what your own server is doing. Don’t bother checking !is_admin() on your … Read more

Call current post URL in ajax

you can retrieve the permalink of the current post in JavaScript with wp_localize_script. juste try this code after the call of wp_enqueue_script wp_localize_script( “script_id” , “object” , [ “permalink” => get_post_permalink($post), ] ); and then you can read the permalink in JavaScript in object[“permalink”]

Javascript asset not enqueuing with the rest

Your second wp_enqueue_script() contains the same handle (the same ID) as the first wp_enqueue_script(). The handle is the ID by which a registered script is known to WordPress internally. If you use this ID a second time, the second script won’t be registered. A working example could be this one: <?php add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles_and_scripts’ ); … Read more

Add to cart – button should be disabled

This behavior is being applied by your theme’s javascript. There’s a global variable being set in the head of the page by the theme: <script type=”text/javascript”> /* <![CDATA[ */ var edgeGlobalVars = {“vars”:{“edgeAddForAdminBar”:0,”edgeElementAppearAmount”:-100,”edgeAddingToCartLabel”:”Adding to Cart…”,”edgeTopBarHeight”:0,”edgeStickyHeaderHeight”:0,”edgeStickyHeaderTransparencyHeight”:60,”edgeLogoAreaHeight”:0,”edgeMenuAreaHeight”:96,”edgeMobileHeaderHeight”:60}}; var edgePerPageVars = {“vars”:{“edgeStickyScrollAmount”:0,”edgeHeaderTransparencyHeight”:0}}; /* ]]> */ </script> and a call to a minified/aggregate compiled theme js file: <script type=”text/javascript” … Read more

Theme JS is available but theme CSS isn’t

It seems to me best to separate styles, from scripts and other assets. This worked: function my_theme_enqueue_styles() { wp_enqueue_style( ‘parent-style’, get_parent_theme_file_uri() . ‘style.css’ ); } add_action( ‘my_theme_enqueue_styles’, ‘wp_enqueue_styles’ ); function my_theme_enqueue_assets() { wp_enqueue_script( ‘behavior’, get_theme_file_uri( ‘behavior.js’ ), array(), null, true ); } add_action( ‘wp_enqueue_scripts’, ‘my_theme_enqueue_assets’ );