自定义自定义控件中的WP_FilesSystem

时间:2014-07-23 作者:user2019515

我目前正在尝试编辑以下文件,以便它使用WP\\u文件系统方法,而不是直接调用PHP文件系统:google-font-dropdown-custom-control.php

我当前的代码如下所示:

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\' );
但是,这会引发以下错误:

Call to undefined function request_filesystem_credentials()
您似乎不能在主题定制器中使用文件系统?解决方案是什么,有什么建议吗?

1 个回复
最合适的回答,由SO网友:user2019515 整理而成

要缓存某些内容,可以使用Transients API (谢谢@Otto),get_transient() 要查看它是否已经存在,如果不存在,则提取数据并将其存储在瞬态中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 );
    }

结束

相关推荐