最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp filesystem - Call to a member function put_contents() on a non-object

programmeradmin0浏览0评论

I have a plugin and I am creating a css file in wp_content.

I used this:

$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');
}

However I get this error:

Fatal error: Call to a member function put_contents() on a non-object

var_dump($css) return string.

Does put_contents write to an existing file or does it create a file like file_put_contents does?

I am looking for an equivalent of this:

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

Thank you!

I have a plugin and I am creating a css file in wp_content.

I used this:

$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');
}

However I get this error:

Fatal error: Call to a member function put_contents() on a non-object

var_dump($css) return string.

Does put_contents write to an existing file or does it create a file like file_put_contents does?

I am looking for an equivalent of this:

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

Thank you!

Share Improve this question edited Jun 8, 2015 at 5:29 s_ha_dum 65.6k13 gold badges84 silver badges174 bronze badges asked Jan 17, 2014 at 22:13 ToniqToniq 4476 silver badges15 bronze badges 5
  • 1 My guess is that either $this or $wp_filesystem are not properly declared, as both of these should be calling other classes. Make sure you're calling the global $wp_filesystem; – Jeremiah Prummer Commented Jan 17, 2014 at 22:24
  • Yes, I do call global $wp_filesystem; just before I execute put_contents. Does put_contents write to an exisiting file or does it create a file like file_put_contents does? – Toniq Commented Jan 17, 2014 at 22:39
  • Try var_dump($wp_filesystem);. What do you get? – s_ha_dum Commented Jan 17, 2014 at 23:37
  • I have place this after I call global $wp_filesystem; and I get NULL. If I place WP_Filesystem(); I also get Fatal error: Call to undefined function WP_Filesystem() – Toniq Commented Jan 18, 2014 at 0:04
  • Ok, I get it now, I call these in my add_shortcode function, so its executed in shortcode, which means its not an admin? What are my options? – Toniq Commented Jan 18, 2014 at 0:11
Add a comment  | 

2 Answers 2

Reset to default 9

Initialize the WP filesystem and no more using file_put_contents function. Try this:

[...]

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');
}
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

Add above code in functions.php. After that enqueue that style

$uploads = wp_upload_dir();
wp_enqueue_style( 'newstweentyfour-customize', trailingslashit($uploads['baseurl']) . 'newstweentyfour-customize.css', array()  );

Its works i have tested..

发布评论

评论列表(0)

  1. 暂无评论