I've used a bit of code to try and write a dynamic.css file to a theme folder and it works fine. BUT, it doesn't work on certain shared hosting situations so the request_filesystem_credentials() function is invoked. This produces a really ugly ftp form with no connect/submit button.
I then followed the great Otto's tutorial on this to try and use a decent form instead but the end result is a nice looking form that gives the error "Error saving file!" after entering the correct ftp details. When installing plugins and themes on this site the same ftp credentials work perfectly. So I abandoned Otto's code, for now, and went back to my original code but I can't get it to work properly.
This is really frustrating as the plugin option-tree requires this css file to be stored in this location and writeable. I don't need to show Otto's code as it works for everyone else!! However, here's my original code that must have something important missing:-
function load_dynamic_css(){
global $wp_filesystem;
if( empty( $wp_filesystem ) ) {
require_once( ABSPATH .'/wp-admin/includes/file.php' );
WP_Filesystem();
}
$url = wp_nonce_url('themes.php?page=ot-theme-options','ot-theme-options');
if (false === ($creds = request_filesystem_credentials($url, '', false, false, null) ) ) {
return; // stop processing here
}
if ( ! WP_Filesystem($creds) ) {
request_filesystem_credentials($url, '', true, false, null);
return;
}
$css_file = get_template_directory() . '/dynamic.css';
if (!file_exists($css_file)) {
$wp_filesystem->put_contents( $css_file,'',0666);
}
}
add_action( 'after_setup_theme', 'load_dynamic_css' );
Can anybody please help? I just need to save the empty file, dynamic.css, with 0666 permissions if the file does not already exist on theme activation. The option-tree plugin will then be able to write css to this file. I'm sorry if this has been asked before but I can't see it anywhere on the web. Thanks.