Suppose you want to create any custom template for your WordPress website then you always need to create a new theme file but think, if you don't have FTP access then what will you do. I've been stuck in this situation many times and after searching I got a solution.
This solution is nothing more than a function, but you can create any file. I have applied this solution in three ways. So here I will mention all of them.
Through any existing file: Here we will use header.php. Now follow the below steps:
1. Go to Appearance -> editor.
2. Navigate to header.php.
3. Look for this tag </head> in the file.
4. Add the below function before this tag.
<?php touch('wp-content/themes/theme-folder/new-file-name.php'); ?>
5. Update the file.
6. Visit the website to run this code. It will create a new file in your theme folder.
7. Now go back to the header.php and remove this code.
Note: Add your own theme's folder and file name where I have marked red.
Via theme's functions.php: It is an another way to create a new theme file. The difference is that you don't need to visit the website and to remove the code. Simply paste the below code in functions.php of your theme.
What will this code do? As you are seeing I have used register_my_custom_menu_page function and linked it with admin_menu hook to create a menu item and second create_new_filepage will create a page contains input box. This box will use to create a new theme file. When you enter the file name and submit it, the touch will execute and create a file with the given name.
From plugin: I have converted this code in the form of plugin. You don't need to code anything, just download and install. It will give you all the functionality to create a new theme file in your active theme folder.
Hopefully, this tricky way will help you to accomplish your goal.
This solution is nothing more than a function, but you can create any file. I have applied this solution in three ways. So here I will mention all of them.
Through any existing file: Here we will use header.php. Now follow the below steps:
1. Go to Appearance -> editor.
2. Navigate to header.php.
3. Look for this tag </head> in the file.
4. Add the below function before this tag.
<?php touch('wp-content/themes/theme-folder/new-file-name.php'); ?>
5. Update the file.
6. Visit the website to run this code. It will create a new file in your theme folder.
7. Now go back to the header.php and remove this code.
Note: Add your own theme's folder and file name where I have marked red.
Via theme's functions.php: It is an another way to create a new theme file. The difference is that you don't need to visit the website and to remove the code. Simply paste the below code in functions.php of your theme.
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page(){
add_menu_page( 'custom menu title', 'New File', 'manage_options', 'newfilepage', 'create_new_filepage', plugins_url( 'Create-New-File/images/new-file-btn.png' ), 6 );
}
function create_new_filepage(){
echo '<style>
.new-form{
width: 50%;
margin: 0 auto;
margin-top:30%;
}
</style>';
if ( isset( $_POST['submit'] ) ) {
touch(get_template_directory().'/'.$_POST['name'] );
}
echo '<div class="new-form">';
echo '<h1>Enter the name</h1>';
echo '<h3>Your theme is '.wp_get_theme().'</h3>';
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post"><input type="text" name="name" class="regular-text"><input type="submit" name="submit" value="submit" class="button button-primary">';
echo '</div>';
}
function register_my_custom_menu_page(){
add_menu_page( 'custom menu title', 'New File', 'manage_options', 'newfilepage', 'create_new_filepage', plugins_url( 'Create-New-File/images/new-file-btn.png' ), 6 );
}
function create_new_filepage(){
echo '<style>
.new-form{
width: 50%;
margin: 0 auto;
margin-top:30%;
}
</style>';
if ( isset( $_POST['submit'] ) ) {
touch(get_template_directory().'/'.$_POST['name'] );
}
echo '<div class="new-form">';
echo '<h1>Enter the name</h1>';
echo '<h3>Your theme is '.wp_get_theme().'</h3>';
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post"><input type="text" name="name" class="regular-text"><input type="submit" name="submit" value="submit" class="button button-primary">';
echo '</div>';
}
What will this code do? As you are seeing I have used register_my_custom_menu_page function and linked it with admin_menu hook to create a menu item and second create_new_filepage will create a page contains input box. This box will use to create a new theme file. When you enter the file name and submit it, the touch will execute and create a file with the given name.
From plugin: I have converted this code in the form of plugin. You don't need to code anything, just download and install. It will give you all the functionality to create a new theme file in your active theme folder.
Hopefully, this tricky way will help you to accomplish your goal.
0 Comment