Create, register and use shortcodes in WordPress
18/03/2023Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.
Have you ever needed to get the difference between 2 numbers in JavaScript? This problem appears fairly silly at first because surely you just subtract the smallest number from the largest number; whatever is left is the difference. But what happens when you don’t know which number is the larger?
I’m going to show you 2 methods to solve this.
First we could use Math.abs(). Math.abs() will return the absolute value of the number passed to it.
const diff = (a, b) => {
return Math.abs(a - b);
}
console.log(diff(12, 17)); // Output: 5
In the example we subtract 17 from 12 (-5) and pass the value to Math.abs(). Math.abs() converts this to an absolute value and returns 5.
Another way to get the difference between 2 numbers, and perhaps a more purist way, is to check which number is larger and then subtract the smaller number from the larger. We can do this by writing our own function.
const diff = (a, b) => {
return (a > b) ? (a - b) : (b - a);
}
console.log(diff(12, 17)); // Output: 5
For those who don’t know, the above example uses a ternary operator. The condition is evaluated and on ‘true’ the first value is returned, on ‘false’ the second. In this case we check whether ‘a’ is greater than ‘b’ and then return either ‘(a – b)’ or ‘(b – a)’ depending on the result.
You could alternatively write this as an ‘if/else’ conditional statement and achieve the same result. The main benefit of this approach is that it is more ‘human readable’ and probably easier to follow for new developers beginning their journey.
const diff = (a, b) => {
if (a > b) {
return a - b;
} else {
return b - a;
}
}
The execution speeds of the 2 methods are practically the same.
Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.
Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.
Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.
In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.
Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.
Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.
Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.
Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.
Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!