对非对象调用成员函数Put_Contents()

时间:2014-01-17 作者:Toniq

我有一个插件,我正在wp\\U内容中创建一个css文件。

我用了这个:

$this->content_dir = WP_CONTENT_DIR . "/some_folder";

$path = $this->content_dir . \'options.css\';
$css=\'some string\';
global $wp_filesystem; 
if(!$wp_filesystem->put_contents( $path, $css, 0644) ) {
    return __(\'Failed to create css file\');
}
但是,我遇到了以下错误:

致命错误:对非对象调用成员函数put\\u contents()

var_dump($css) 返回字符串。

是将内容写入现有文件还是创建类似的文件file_put_contents

我正在寻找与此类似的:

if(!file_put_contents($path, $css)){
    return __(\'Failed to create css file\');
};
谢谢大家!

2 个回复
SO网友:berdi

初始化WP文件系统,不再使用file\\u put\\u contents函数。尝试以下操作:

[...]

global $wp_filesystem;
// Initialize the WP filesystem, no more using \'file-put-contents\' function
if (empty($wp_filesystem)) {
    require_once (ABSPATH . \'/wp-admin/includes/file.php\');
    WP_Filesystem();
}

if(!$wp_filesystem->put_contents( $path, $css, 0644) ) {
    return __(\'Failed to create css file\');
}

SO网友:Jahirul Islam Mamun

require_once( ABSPATH . \'wp-admin/includes/file.php\' ); // you have to load this file

global $wp_filesystem;
$upload_dir = wp_upload_dir(); // Grab uploads folder array
$dir = trailingslashit( $upload_dir[\'basedir\'] )  . \'cutomizer-css/\'; // Set storage directory path
WP_Filesystem(); // Initial WP file system
$wp_filesystem->mkdir( $dir ); // Make a new directory folder if folder not their

$wp_filesystem->put_contents( $dir . \'news24-customize.css\', $css, 0644 ); // Finally, store the file :D
在函数中添加上述代码。php。在那之后,把那种风格排成队

$uploads = wp_upload_dir();
wp_enqueue_style( \'newstweentyfour-customize\', trailingslashit($uploads[\'baseurl\']) . \'newstweentyfour-customize.css\', array()  );
它的作品我已经测试过了。。

结束

相关推荐