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.
Quite often you will need to create or edit files in a PHP application. The problem is that you will probably need to create the directory structure at some stage. Therefore, it is useful to know how to recursively make directories easily.
To do this we are first going to check whether the current directory structure exists. If it doesn’t, we are going to recursively create all the folders using mkdir(). You might have guessed, but the purpose of this function is creating directory paths. You can directly create a directory or recusively create the directory structure by passing a boolean flag.
We’ll start with a function, and pass it a path variable. This will serve as our base:
function makeDirPath($path) {
}
We can check whether a directory exists using either file_exists() or is_dir(). The thing is, is_dir() is actually telling us whether the given path/file is a directory, and therefore it returns false if the directory doesn’t exist. file_exists() checks whether the file or directory exists, and as we are checking whether a directory exists I strongly suggest we use it.
function makeDirPath($path) {
return file_exists($path);
}
Note: We are checking whether the file exists first to avoid errors later.
Our function is currently pretty useless. It returns true or false depending on whether a directory exists or not. The next step is to use mkdir() when file_exists() returns false.
To do this we are going to use an or (||) operator:
function makeDirPath($path) {
return file_exists($path) || mkdir($path, 0777, true);
}
We are passing mkdir the path, the file permissions (0777 is default, and it is ignored in windows), and setting the recursive flag to ‘true’. If the file doesn’t exist, the entire directory structure will be recursively created.
Drop the function into your project and call it whenever you need a new directory.
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!
How is this article written on 05/03/2021 when today in only 03/16/2021 ?????
because : DD/MM/YYYY
Nope, he’s come from the future 😎 haha
He comes from* Sorry, my english is not pretty good 🙁
How comes you guys notice such details in web pages …. ?????