admin-ajax Firing Error 400 When Logged In

Did I set up that wp_ajax_nopriv_ hook wrong? No, you did not. But that hook is for logged-out or unregistered users only. For logged-in users, the hook is wp_ajax_<action>. So you just need to change the add_action(‘get_events’ in your code to add_action(‘wp_ajax_get_events’, and the error 400 would be gone: add_action(‘wp_ajax_get_events’, ‘get_events_func’); // for logged-in users … Read more

useSelect second parameter

Short Answer The second parameter for useSelect is the second parameter for useCallback, so check the React website and you’d understand what the heck that second parameter is.. 🙂 Long Answer useSelect is a custom React hook which uses the core useCallback hook in React which returns a memoized callback which can avoid expensive calculations … Read more

How to display post content in the block editor

It is true that using RawHTML (or dangerouslySetInnerHTML) is discouraged and indeed prone to XSS attacks like <img src=x onerror=alert(1)//> and <p>abc<iframe//src=jAva&Tab;script:alert(3)>def</p>. But if you need to set HTML directly from React, then you’ll have to use dangerouslySetInnerHTML, either directly (e.g. <div dangerouslySetInnerHTML={ ({ __html: ‘foo <b>bar</b>’ }) } />) or via RawHTML in Gutenberg … Read more

Looping over wordpress meta to create “ ‘s?

Does select(“core/editor”).getEditedPostAttribute(“meta”) get the whole post meta? Only the meta registered using register_post_meta() or register_meta() with the show_in_rest argument set to true. So for example, the meta can be registered like so: register_post_meta( ‘post’, ‘_postcode_pricing_postal_town’, array( ‘show_in_rest’ => true, ‘single’ => true, ‘type’ => ‘string’, // I set this because the meta is *protected*, i.e. … Read more

is_page_template() written in jquery/javascript

Well, yes, in WordPress, different templates have a different body class name. If you inspect the body classes on a specific page in WordPress, there should be a body class page. You can detect the template by checking if the class exists like so: jQuery(document).ready(function($){ if ( $(‘body’).hasClass(‘page’)) { // add your logic here } … Read more

PHP echo inside javascript

Have a read through this article: Adding Scripts Properly to WordPress Part 2 – JavaScript Localization What you are doing here is passing your PHP values to the WordPress JS localization system. This then allows your JS code to get access to those variables you have passed.