Saturday, September 28, 2024
Google search engine
HomeLanguagesHow to create a folder in WordPress if it doesn’t already exist...

How to create a folder in WordPress if it doesn’t already exist ?

In a few situations, you might need to check whether the folder already exists, and if not, you might have to create it.

In some instances, WordPress folders aren’t created on servers, so you might have to create them yourself to manage everything. The creation of additional folders is sometimes necessary, but you must ensure that they aren’t already present or errors will appear in the front end or back end.

In this article, we look at the solutions to solve this problem

How To Add Files To WordPress?

If your business site runs on WordPress, you’re probably familiar with the WordPress Dashboard. From the Dashboard, you can manage users, create blog posts and update themes. However, folders cannot be created via the dashboard. An FTP client is necessary for creating the folder in the root directory of your WordPress website.

Step1: Access your WordPress Dashboard using your username and password, and log in with those credentials.

WordPress Login

Step 2: Click the “Settings” button on the left side of the Dashboard. WordPress displays the General Settings screen.

WordPress Settings Page

Step 3: Note the Web address displayed in the field labeled “WordPress address (URL)” for your WordPress site. This is the address of the root folder.

WordPress Site URL

Step 4: Log in to your Web hosting account using the username and password associated with the FTP application on your computer. WordPress recommends using FileZilla FTP client if you do not already have one installed

File Zilla FTP client

Step 5: Files on your computer are displayed in a left-hand window, while files on your website are displayed in a right-hand window. Use the right-hand window to navigate to the folder.

File Zilla Window

After this Within the folder, right-click and select the New Folder Creation option from the menu that appears. Depending on the FTP application you’re using, the folder creation link may have a slightly different label. In FileZilla, for instance, it’s called “Create Directory.” Now a new folder is created in the root folder of your WordPress blog when you click “OK” in your FTP client.

Check whether the file exists or not:

To check whether the file exists or not we can use a PHP function called file_exists(for checking if a folder exists) and mkdir(for creating the folder) can be used to accomplish the task. You can follow the steps mentioned below to add this PHP code to your website.

1. file_exists(): An existential check is made by file_exists to confirm whether the file is present or not

Syntax:

file_exists($filepath)

where $filepath is a string. A file exists if the function returns true otherwise a false value is returned

2. mkdir(): The mkdir command attempts to create a directory or folder

Syntax:

mkdir(
    string $directory,
    int $permissions = 0777,
    bool $recursive = false,
    resource $context = ?
)

Where, 

  1. $directory refers to the directory path
  2. $permissions have the default value 0777, which means the most access possible
  3. $recursive enables the creation of directories within directories
  4. $context provides context streams

Step 1: Choose PHPCode Snippets from the plugins section of WordPress Dashboard

Insert PHP Code Snippet Plugin

Step 2: To add a new PHP Code Snippet, click the Add New PHP Code Snippet button. Enter the tracking name for the PHP function you wish to add. The file exist() function will be added to this example to return whether the file exists or not. Click the Create button.

PHP Add Code Option

The code to be added is mentioned below

if (!file_exists('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);
}

RELATED ARTICLES

Most Popular

Recent Comments