是否在上传文件夹中动态保存自定义css文件?

时间:2021-08-01 作者:thedebuggger

当我们在自定义CSS的WordPress中自定义主题时,它会创建后端自定义CSS文件,我必须完成这些我无法公开理解的任务:

动态将此CSS保存在上载文件夹中的文件中

1 个回复
SO网友:Ahmed Ali

请使用此方法:

// Lets create some test CSS code
$css = \'/* Some test CSS */ body {background:red;}\';

// Stash CSS in uploads directory
require_once( ABSPATH . \'wp-admin/includes/file.php\' ); // We will probably need to load this file
global $wp_filesystem;
$upload_dir = wp_upload_dir(); // Grab uploads folder array
$dir = trailingslashit( $upload_dir[\'basedir\'] ) . \'some-folder/\'; // Set storage directory path

WP_Filesystem(); // Initial WP file system
$wp_filesystem->mkdir( $dir ); // Make a new folder for storing our file
$wp_filesystem->put_contents( $dir . \'style.css\', $css, 0644 ); // Finally, store the file :D