Ich habe ein Plugin und erstelle eine CSS-Datei in wp_content.
Ich habe das benutzt:
$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');
}
Allerdings bekomme ich diesen Fehler:
Schwerwiegender Fehler: Aufrufen einer Mitgliedsfunktion put_contents () für ein Nicht-Objekt
var_dump($css)
return string.
Schreibt put_contents in eine vorhandene Datei oder erstellt es eine Datei wie file_put_contents
?
Ich suche ein Äquivalent dazu:
if(!file_put_contents($path, $css)){
return __('Failed to create css file');
};
Vielen Dank!
Initialisieren Sie das Dateisystem WP und verwenden Sie nicht mehr die Funktion file_put_contents. Versuche dies:
[...]
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
Füge den obigen Code in functions.php hinzu. Danach wählst du diesen Stil aus
$uploads = wp_upload_dir();
wp_enqueue_style( 'newstweentyfour-customize', trailingslashit($uploads['baseurl']) . 'newstweentyfour-customize.css', array() );
Seine Werke habe ich getestet ..