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.
I often find that simple things are the ones most forgotten or unknown. I’ve recently been helping colleagues learn PHP, and I frequently get asked how to move up and down directory levels in relation to the current file.
I know of two relatively simple ways to do this; one using dirname()
and one using realpath().
We will also be using ‘Magic Constants’ in this post, but handily I’ve explained these a bit here.
If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it. In PHP 7 and above we can specify how many levels we would like to move. To use this we first pass the file or directory path as a string, and then specify how many levels we would like to move up from that directory.
Let’s assume that our script is running in /path/to/file/index.php:
$dir = dirname(__DIR__, 2);
echo $dir; // Result = "/path"
By using the ‘Magic Constant’ ‘__DIR__’ and specifying a level of 2, we are asking dirname() to return 2 levels up from the parent of ‘__DIR__’. In this case ‘__DIR__’ is ‘/path/to/file’, so 2 levels higher is ‘/path’.
Remember: dirname() returns the parent directory, so using ‘dirname(__DIR__)’ will return the parent directory of ‘__DIR__’.
If we use ‘__FILE__’ it is slightly different:
$dir = dirname(__FILE__, 2);
echo $dir; // Result = "/path/to"
Because ‘__FILE__’ returns the file in the string, the parent directory is ‘/path/to/file’. By specifying a level of 2 we now move to ‘/path/to’ instead.
If you happen to be using an older version of PHP then luckily we can use realpath().
The ‘Magic Constant’ ‘__DIR__’ was added to PHP in 5.3, so for people 5.3 to 5.6 we can move directory levels like this:
Again, assuming that our script is running in /path/to/file/index.php:
$dir = realpath(__DIR__ . '/..');
echo $dir; // Result = "/path/to"
In this example, realpath() resolves the reference ‘/..’ in relation to ‘__DIR__’ and moves us 1 level higher. If we were to use ‘/../..’ then we would move 2 levels higher, and so on.
If we are going back even further than PHP 5.3 then we would have to use realpath(), dirname() and the ‘Magic Constant’ ‘__FILE__’.
Essentially, realpath() won’t accept a file, only a path. This means that we have to remove the ‘index.php’ from our ‘__FILE__’ string. There are a few ways that we could achieve this, one of them is using dirname().
$dir = realpath(dirname(__FILE__) . '/..');
echo $dir; // result = "/path/to"
This example takes the parent directory of ‘__FILE__’ and then resolves ‘/..’ using realpath() to move it 1 level higher.
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!