0

I'm currently trying to edit the following file so it uses the WP_Filesystem methods instead of direct PHP filesystem calls: google-font-dropdown-custom-control.php

My current code looks as follows:

function mytheme_customize_register( $wp_customize ) {

    $wp_customize->add_section('fonts', array(
        'title' => 'Font',
        'priority' => 40
        ));

    $wp_customize->add_setting( 'google_webfont' , array(
        'default'     => 'Open Sans',
        'transport'   => 'refresh',
    ) );


    if (false === ($creds = request_filesystem_credentials('customize.php', '', false) ) ) {

        // if we get here, then we don't have credentials yet,
        // but have just produced a form for the user to fill in, 
        // so stop processing for now

        return true; // stop the normal page form from displaying
    }

    // now we have some credentials, try to get the wp_filesystem running
    if ( ! WP_Filesystem($creds) ) {
        // our credentials were no good, ask the user for them again
        request_filesystem_credentials('customize.php', '', true);
        return true;
    }

    global $wp_filesystem;
    require_once('inc/google-font-dropdown-custom-control.php');

    $wp_customize->add_control( new Google_Font_Dropdown_Custom_Control( $wp_customize, $wp_filesystem, 'google_font_setting', array(
        'label'   => 'Title Font',
        'section' => 'fonts',
        'settings'   => 'google_webfont'
    ) ) );


}
add_action( 'customize_register', 'mytheme_customize_register' );

This however throws me the following error:

Call to undefined function request_filesystem_credentials()

It seems like you can't use the Filesystem in the Theme Customizer? What would the solution be, any tips?

  • 1
    What exactly are you doing with the filesystem that requires you to do this in the customize_register hook? This makes no real sense I can think of. What is the need for this chunk of code to write to the filesystem? – Otto Jul 23 '14 at 2:13
  • I'm caching the Google Fonts API output, seems mandatory otherwise I'll hit the limit depending on how many users use the theme of course. – user2019515 Jul 23 '14 at 13:52
  • Why would you write to the filesystem to cache anything? Transients exist. Or, just store the css code in an option or theme mod. Clearly, we're missing some reasoning here. – Otto Jul 24 '14 at 2:51
  • Thanks Otto, you're not really missing anything but you shouldn't assume that we all know all the APIs. I've never heard of the Transients API until now. Below is the solution I've come up with, if you have any more suggestions/tips I'd love to hear them. – user2019515 Jul 24 '14 at 12:32
1

To cache something you can use the Transients API (thanks @Otto), get_transient() to see if it exists already exists, if not then fetch the data and store it in a transient with set_transient().

    if (get_transient('mytheme_webfonts')) {
        $content = get_transient('mytheme_webfonts');
    }
    else{
        $googleApi = 'https://www.googleapis.com/webfonts/v1/webfonts?sort=alpha&key={API_KEY}';

        $fontContent = wp_remote_get( $googleApi, array('sslverify'   => false) );

        $content = json_decode($fontContent['body']);

        set_transient( 'mytheme_webfonts', $content, WEEK_IN_SECONDS );
    }

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.