Sometimes you might find yourself working for a client that doesn’t know about FTP. They may have installed WordPress via their cPanel installation script, and don’t know how to create FTP users. Now it’s difficult (but not impossible), to develop and maintain WordPress without FTP. Let’s say you want to create a child theme, but don’t know how to get the WordPress theme folder for the parent without FTP.
Well, this tutorial will show you how!
Step 1: Open the WordPress Theme Editor and Open functions.php
Starting with the recent versions of WordPress, the core functionality is discouraging people from editing theme files directly. So if you want to add a new style rule for example, it’s better to do it from within the theme customizer. However, there are still situations where using the theme editor is useful. And that’s for entering WordPress code.
Open the WordPress dashboard and go to Appearance->Editor on the left-hand side as shown here:
At first, you’ll get a scary warning telling you not to edit your theme files directly. Click “I Understand” and proceed. On the right-hand size, you’ll see a list of files belonging to your theme. One of them will be labeled “Theme Function” and will have the filename “functions.php” like this:
Now let’s insert some code.
Step 2: Inserting Code (Safely) into functions.php
The window on the left-hand size might already have some text inside it. In which case, we’re going to add our own code snippet to it.
Warning for Older Themes
There’s a potentially disastrous bomb waiting to go off if you don’t add code to functions.php in a safe manner. Actually, there are many mines that will blow up your site if you trigger them – including making a syntax error. This is one of the big disadvantages of adding code through functions.php. Later on, I’ll show you a safer way of adding code.
At the bottom of functions.php, you might see the following symbol:
?>
Delete it.
This is the closing tag of a PHP script. Older themes tend to have these in functions.php and it’s dangerous, because anything after it – including a single empty space – will cause the site to crash. So get rid of it! We no longer need to explicitly close PHP tags anymore, so there’s no reason to have it.
If the symbol ?> doesn’t exist, just move to the next part – pasting the code.
Copy the following code and paste it into the end of functions.php:
function theme_folder(){ return get_bloginfo('stylesheet_directory'); } add_shortcode('theme_folder', 'theme_folder');
Now save your changes.
Step 3: Using the Shortcode to Get the Theme Directory
Once you’ve saved the changes to functions.php, start a new post and paste the following into the editor:
[theme_folder]
like this:
Now just view a preview of the page and you should get a URL that looks like this:
The last word in the URL is the directory name of your current theme! In the screenshot above, the name of the folder is “write” – aptly for the “Write” theme. Of course, not all directories will be so obviously linked to their folder names, so the above code is useful to make sure.
Step 4: Delete the Code Afterward
Once you know your theme folder’s name, go to functions.php and delete the code you entered in Step 2. There’s no need to have additional code cluttering up your PHP scripts! Our work is done, and we should try and keep our code pages as clean as possible. Besides, you don’t want anyone else to use the shortcode to get your theme folder name. So it’s a question of security as well.
Ideally, you should get FTP access from your clients. But if that’s not forthcoming, or you don’t want to wait until you get the credentials, this is a good way to get the WordPress theme folder without it!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply