0

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.

0

First, remove that bit up front about calling WP_Filesystem(); by itself. You need to request_filesystem_credentials first, before invoking that.

Second, you need to use the $wp_filesystem->wp_themes_dir('themename') function call to get the proper "remote" directory, in order to be able to properly call the put_contents() function with the right filename. The "remote" directory won't necessarily match the "local" directory, and get_template_directory() is absolutely the wrong way to get that file path for remote writing. It's fine for local file checking though.

  • Thanks for the reply Otto. Makes perfect sense and I'll give it a try as soon as I get home later. – Mark Apr 25 '14 at 10:17
  • Hi Otto. I tried the amendments but no joy. I still get the "ugly" form with no submit button. Out of interest can you advise me as to what circumstances would a server be set up where your plugin (Filesystem Tutorial) returns "Error saving file!"? Obviously the credentials are ok or else the message "ERROR: There was an error connecting to the server, Please verify the settings are correct." appears. If your plugin can't save the test.txt file in the uploads directory then I'm struggling to see how I can achieve the same objective but in the theme's root. Thanks again Otto. – Mark Apr 25 '14 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.