Save Additonal CSS to a file [duplicate]
Question
I am trying to save the Additional CSS to a file not into the database and enqueue it. I created a my.css file in upload folder but stuck in the process to save/ edit the file. I am facing errors in the following code. Any suggestions plz?
add_action('customize_register', 'theme_footer_customizer');
function theme_footer_customizer($wp_customize){
//adding section in wordpress customizer
$wp_customize->add_section('custom_css', array(
'title' => 'New Additional CSS',
'ID' => 'custom_css',
'priority' => 200,
));
//adding setting for footer text area
$wp_customize->add_setting('custom_css', array(
'default' => 'Default Text For Footer Section',
));
// Get upload directory
$upload_dir = wp_get_upload_dir();
// Create style file path
$style_file_path = sprintf( '%1$s/css', untrailingslashit( $upload_dir['basedir'] ) );
// Create directories if they do not exist
if( ! file_exists( $style_file_path ) ) {
wp_mkdir_p( $style_file_path );
}
// Sanitize contents ( probably needs to be sanitized better, maybe with a CSS specific library )
$contents = wp_filter_nohtml_kses( wp_strip_all_tags( $_POST['custom_css'] ) );
// Replace the contents of the file with the new saved contents.
$style_file = $style_file_path . '/my.css';
file_put_contents( $style_file, $contents );
// Redirect back to page with $_GET successes and errors
wp_safe_redirect( add_query_arg( array(
'success' => true,
), wp_get_referer() ) );
exit();
// /* How to Enqueues the external CSS file */
add_action( 'wp_enqueue_scripts', 'my_external_styles' );
function my_external_styles() {
wp_register_style( 'my-css', $style_file_path .'/my.css' );
wp_enqueue_style( 'my-css' );
}
}
0
4 months
0 Answers
11 views
0
Leave an answer
You must login or register to add a new answer .