Minimal AJAX in WordPress

Minimal AJAX in WordPress

David Nash code, php Leave a Comment

Here’s what it takes to create minimal AJAX in WordPress. First, let’s create a button: Then functions.php include a Javascript file called do_ajax.js. It has the dependencies jquery and wp-util: Also in functions.php, we set up a function to handle the AJAX call, and add the actions: Finally, in our do_ajax.js file: And then in your console you should see “Hello!”. That string is being sent from do_ajax.js to the server, and the server simply sends it back again. That’s it – we have minimal AJAX in WordPress up and running! This post was based on this StackOverflow answer, and …

Debugging in WordPress

Debugging in WordPress

David Nash code, wordpress 1 Comment

I know, debugging in WordPress is boring! We’re creators – we want to build cool stuff without worrying about stepping on any toes! But debugging in WordPress is important! Last week I came across this tweet from Jonathan Bossenger (@jon_bossenger): My first instinct was to reply that enabling debugging in WordPress makes in impossible to use, until I looked more closely. You see, every time I set up a WordPress site I have to edit wp-config.php, and that line define( ‘WP_DEBUG’, false ); occasionally tempts me to change it to TRUE. I soon regret it, because my next step is …

Autocomplete with AJAX in WordPress

Autocomplete with AJAX in WordPress: 2019

David Nash code, tutorial, wordpress 3 Comments

This is the second part of Create an autocomplete field in WordPress: 2019, which works well if you don’t have that many options. If you have less than 10 options, use a select element. Less than 100-1000 (depending), you don’t need AJAX. If you have more than 100-1000, you’ll probably need to use autocomplete with AJAX. This means the page will load faster, and we’ll limit the data to entries that more closely match what the user is looking for. We didn’t even need Javascript for the previous part. What we’re doing now is a bit more complex, so we’ll …

Pretty print fractions as HTML entities using PHP

David Nash code, html, php 1 Comment

I used this code in a WordPress site but it’s pure HTML. I wanted to take a string, for example “3/4” and convert it to a HTML entity ¾. When there’s no HTML entity I still wanted it to look nice, using <sup> and <sub> for the numerator and denominator respectively. I couldn’t find anything on the web that I could copy and paste so I wrote it myself. Here’s what I came up with: //supply a string, eg 1/2, return eg ½ function str_to_fraction( $str ) { $f = explode(‘/’, $str); if( count($f) != 2 ) { return $str; …

Appearance Customize example code

Appearance Customize – WordPress theme Quick Start

David Nash code, wordpress Leave a Comment

There’s a lot of detailed information on how to add settings under the WordPress Appearance Customize menu. It’s far too detailed if you just want to add a couple of settings. In this example I just wanted to add some simple fields. One for the company title, which differs from the site title. And another for their logo which appears in the WordPress header. Modifying Appearance Customize with functions.php add_action(‘customize_register’, ‘yourtheme_customize_register’); function yourtheme_customize_register( $wp_customize ) { //first we create a section $wp_customize->add_section( ‘yourtheme_theme_options’, array( ‘title’ => __( ‘Theme options’, ‘yourtheme’ ), ‘priority’ => 1000, //(probably) make it the last item …