获取文件,路径中文件名类似的文件。

huangapple go评论72阅读模式
英文:

Get a file from path where file name LIKE

问题

I am developing a WordPress that utilises gulp to build the frontend of the application (scss, js) files.

In my functions.php I am using enqueue to load my css and js so that they can be used in the editor.

add_action( 'enqueue_block_editor_assets', function() {
   wp_enqueue_style( 'editor-styling', get_theme_file_uri( '/dist/styles/main.css', __FILE__) );
   wp_enqueue_script( 'editor-scripts', get_theme_file_uri( '/dist/scripts/main.js', __FILE__) );
} );

Running a simple gulp command, I can do the above as the file will be named main.css. However, I am facing an issue that when I use gulp --production, the style and JavaScript are suffixed with a random value.

For example, my main.scss will (once I run the above command) turn into main-9acd4829.css.

My question is, how can I get a file from a certain directory where the file name is like main<whatever>.css.

I have tried using something such as:

get_theme_file_uri(glob('/dist/styles/main*.css'), __FILE__)

However, that returned null.

英文:

I am developing a WordPress that utilises gulp to build the frontend of the application (scss, js) files.

In my functions.php I am using enqueue to load my css and js so that they can be used in the editor.

add_action( &#39;enqueue_block_editor_assets&#39;, function() {
   wp_enqueue_style( &#39;editor-styling&#39;, get_theme_file_uri( &#39;/dist/styles/main.css&#39;, __FILE__) );
   wp_enqueue_script( &#39;editor-scripts&#39;, get_theme_file_uri( &#39;/dist/scripts/main.js&#39;, __FILE__) );

} );

Running running a simple gulp command I can do the above as the file will be named main.css. However, I am facing an issue that when I use gulp --production the style and javascript are suffixed with a random value.

For example my main.scss will (once I run the above command) turn into main-9acd4829.css.

My question is, how can I get a file from a certain directory where file name like main&lt;whatever&gt;.css.

I have tried using something such as

get_theme_file_uri(glob(&#39;/dist/styles/main*.css&#39;), __FILE__)

However that returned null

答案1

得分: 1

我猜你需要检查不同文件夹中的内容,受到 get_theme_file_uri 代码的启发,类似于以下方式(注意 glob 返回一个数组,而 get_theme_file_uri 需要一个字符串):

add_action('enqueue_block_editor_assets', function() {
    $style_file = glob(get_stylesheet_directory() . '/dist/styles/main*.css');
    if (!$style_file || !count($style_file)) {
        $style_file = glob(get_template_directory_uri() . '/dist/styles/main*.css');
    }
    //注意:如果您的 glob 返回多个文件并且这是您想要的,您可以使用 foreach
    //注意2:理论上,您可以跳过此处对 get_theme_file_uri 的使用,因为您已经测试了它在哪个文件夹,这只是一个示例
    if ($style_file && count($style_file)) {
        wp_enqueue_style('editor-styling', get_theme_file_uri('/dist/styles/' . $style_file[0], __FILE__));
    }

    $script_file = glob(get_stylesheet_directory() . '/dist/scripts/main*.js');
    if (!$script_file || !count($script_file)) {
        $script_file = glob(get_template_directory_uri() . '/dist/scripts/main*.js');
    }
    //注意:如果您的 glob 返回多个文件并且这是您想要的,您可以使用 foreach
    //注意2:理论上,您可以跳过此处对 get_theme_file_uri 的使用,因为您已经测试了它在哪个文件夹,这只是一个示例
    if ($script_file && count($script_file)) {
        wp_enqueue_script('editor-scripts', get_theme_file_uri('/dist/scripts/' . $script_file[0], __FILE__));
    }
});
英文:

I guess you have to check yourself in the different folders, inspired by the code of get_theme_file_uri, something like this (note that glob returns an array while get_theme_file_uri takes a string):

add_action( &#39;enqueue_block_editor_assets&#39;, function() {
    $style_file = glob(get_stylesheet_directory() . &#39;/dist/styles/main*.css&#39;);
    if(!$style_file || !count($style_file)){
        $style_file = glob(get_template_directory_uri() . &#39;/dist/styles/main*.css&#39;);
    }
    //NOTE: you can use foreach if your glob returns multiple files and this is what you want
    //NOTE2: in theory you could skip the use of get_theme_file_uri here because you already tested in which folder it is, this is just an example
    if($style_file &amp;&amp; count($style_file)){
        wp_enqueue_style( &#39;editor-styling&#39;, get_theme_file_uri( &#39;/dist/styles/&#39; . $style_file[0], __FILE__) );
    }
    
    $script_file = glob(get_stylesheet_directory() . &#39;/dist/scripts/main*.js&#39;);
    if(!$script_file || !count($script_file)){
        $script_file = glob(get_template_directory_uri() . &#39;/dist/scripts/main*.js&#39;);
    }
    //NOTE: you can use foreach if your glob returns multiple files and this is what you want
    //NOTE2: in theory you could skip the use of get_theme_file_uri here because you already tested in which folder it is, this is just an example
    if($script_file &amp;&amp; count($script_file)){
        wp_enqueue_script( &#39;editor-scripts&#39;, get_theme_file_uri( &#39;/dist/scripts/&#39; . $script_file[0], __FILE__) );
    }
} );

huangapple
  • 本文由 发表于 2023年5月10日 17:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定