How can I add a method to create files when in theme-editor.php
This is a concept of my goal
I want to
be able to create files in the current opened directory by entering the complete file name and and extension in a text field, click create and use the AJAX process to run the file_put_contents()
method.
I haven't tried any coding methods as I do not even have a starting point. After 18 hours of research, I have only found methods using add_theme_page()
to add a page where all the custom coding can be done. While that's a solid option, I'd like to keep it all in the core file editor. It's just an overkill to create an entire file editor which does all the same tasks as the core, with the only diff being a text field.
Update
In an act of desperation, I have used the admin_notices
action to insert the input field and button, and attached a function to show only on the specified theme, and run the file creation process. It's not the solution I desire so I don't think it's appropriate to post as an answer.
public static function makeFile($file, $data=null)
{
if( !is_null(mb::getPost('makecss')) ) {
file_put_contents(DEF_THEMEPATH.'/styles/'.$file.'.css', $data);
wp_redirect( add_query_arg(['file'='styles/'.$file.'.css','theme'='thor'],admin_url('theme-editor.php')) );
exit;
}
$newfile = '
div class=newfile-form
form method=post action=
pCreate New CSS File/p
spanFile name: input type=text name=newfile id=newfile value= //span
spanbutton type=submit name=makecss class=button button-primaryCreate File/button/span
/form
/div
';
return $newfile;
}
action
if( strstr(mb::urlVar('theme'), 'thor') ) {
add_action('admin_notices', function() {
echo mb::makeFile(mb::getPost('newfile'));
});
}
Result
Note: mb represents the name of my php class
Note: Though the .css
extension is shown in the image, I have since made it the default and only extension possible. A javascript method replaces any period and following string with null
Topic wp-filesystem Wordpress
Category Web