【问题标题】:Save Additional css in separate file dynamically - Wordpress将附加的 css 动态保存在单独的文件中 - Wordpress
【发布时间】:2021-10-07 14:20:04
【问题描述】:

当我们在 WordPress 中自定义页面时,它会在左侧面板中显示附加 CSS 选项,通过点击“保存并发布”按钮保存自定义设置。这也保存了我们在 Additional CSS 部分中添加的自定义 CSS,此自定义添加的 CSS 放置在前端的 HTML 头部部分中,以便可以将更改应用到前端。 (可以通过从前端查看源代码来检查。)

我的任务是,

  1. 将此 CSS 动态保存在上传文件夹中的文件中
  2. 从文件中渲染自定义 CSS。 (这只是在上面的第一步中动态生成的。)
  3. 将其从 HTML 头部部分中删除。否则,自定义 CSS 将被渲染两次。

但实际上它将整个 CSS 保存在数据库中,请任何人建议一些代码来执行此操作?谢谢

这是截图

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    这个问题的完美解决方案非常简单。我已经为 27 个主题 functions.php 文件做了这个。删除开始和结束的反引号...

           add_action('customize_register','mytheme_customizer_options');
    
           function mytheme_customizer_options($wp_customize){
    
         // 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 dot 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( wp_get_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 );
    
          }
         remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
          // /* How to Enqueues the external CSS file */
           add_action( 'wp_enqueue_scripts', 'my_external_styles' );
           function my_external_styles() {
          $upload_dir = wp_get_upload_dir();
          wp_register_style( 'my-css', sprintf( '%1$s/css', untrailingslashit( 
          $upload_dir['baseurl'] ) )  .'/my.css' );
          wp_enqueue_style( 'my-css' );
      }
    

    【讨论】:

      【解决方案2】:
      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');
      
      
      
      remove_action('wp_head', 'wp_custom_css_cb', 101);
      

      【讨论】:

      • 请避免只用代码回答。关于代码如何为原始问题提供解决方案的解释很有帮助。 stackoverflow.com/help/how-to-answer
      • 我明白了要点。
      • 如上所述,file_put_contents 在我的 Windows localhost 上不起作用。在它工作之前,我需要先分别使用 wp_mkdir_p 和 fopen 创建文件夹和文件。注意:move_uploaded_file($file, $upload['baseurl']) 仅适用于使用 PHP 的 HTTP POST 上传机制上传的文件。php.net/manual/en/function.move-uploaded-file.php
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多