【问题标题】:Trying to generate a CSS file in wordpress uploads folder尝试在 wordpress 上传文件夹中生成 CSS 文件
【发布时间】:2023-04-06 16:15:01
【问题描述】:

当用户保存一些自定义字段的值时,我想在 wordpress 上传文件夹中动态创建一个文件。这是我尝试的,但文件不会被写入:

function generate_options_css() {
    $upload_dir = wp_upload_dir();
    $ss_dir = get_stylesheet_directory();
    ob_start(); // Capture all output into buffer
    require($ss_dir . '/custom-styles.php'); // Grab the custom-style.php file
    $css = ob_get_clean(); // Store output in a variable, then flush the buffer
    file_put_contents($upload_dir . '/custom-styles.css', $css, LOCK_EX); // Save it as a css file
}
add_action( 'acf/save_post', 'generate_options_css' ); //Parse the output and write the CSS file on post save

【问题讨论】:

  • 网页用户对目标文件夹是否有足够的写入权限?
  • 嗯,知道如何给用户写权限。在以前的版本中,我已经将文件写入主题文件夹,没有问题。
  • 取决于您的服务器运行的操作系统
  • 每个项目都在另一个共享主机上。

标签: php wordpress upload


【解决方案1】:
function my_custom_css() {
$upload_dir = wp_upload_dir(); 
$base_dir = wp_get_upload_dir(); 
$styles = wp_get_custom_css();
if ( $styles || is_customize_preview() ) :
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';

$file = file_put_contents($upload_dir['basedir']."/new-style-function.css", strip_tags( $styles ) );
move_uploaded_file($file, $upload_dir['baseurl']);

endif;
}

add_action( 'save_post', 'my_custom_css' );

$upload_path = wp_upload_dir(); 
wp_enqueue_style ('custom-style',$upload_path['baseurl'] .'/new-style-function.css');

【讨论】:

  • 请在您的回答中提供一些关于您已修复问题的反馈
  • 如果可能,请努力提供额外的解释,而不仅仅是代码。此类答案往往更有用,因为它们可以帮助社区成员,尤其是新开发人员更好地理解解决方案的推理,并有助于避免需要解决后续问题。
猜你喜欢
  • 1970-01-01
  • 2014-08-14
  • 2015-06-06
  • 2013-11-30
  • 2019-06-16
  • 2014-10-27
  • 1970-01-01
  • 2019-04-11
  • 2023-04-01
相关资源
最近更新 更多