wordpress 如何检查文件夹中是否存在文件

mnemlml8  于 2023-02-21  发布在  WordPress
关注(0)|答案(1)|浏览(427)

我有一个创建acf + gutenberg 块的类。它自动包含样式文件和块脚本。但不是所有的块我都需要脚本。并且当我删除脚本文件时,控制台中有一个错误。我如何检查一个文件,使它不包含一个空文件,但只有当它在文件夹中时?

acf_register_block(
                        [
                            'name'            => $slug,
                            'title'           => $file_headers['title'] ?: __('Unnamed Block:', 'bluegfx_dev') . ' ' . $slug,
                            'description'     => $file_headers['description'],
                            'category'        => $file_headers['category'] ?: 'formatting',
                            'icon'            => $file_headers['icon'],
                            'keywords'        => explode(' ', $file_headers['keywords']),
                            'supports'        => json_decode($file_headers['supports'], true),
                            'render_callback' => [$this, 'block_render_callback'],
                            'enqueue_style'   => get_template_directory_uri() . '/' . $this->get_block_dir_path($slug) . 'style.css',
                            'enqueue_script'  => get_template_directory_uri() . '/' . $this->get_block_dir_path($slug) . 'script.js',
                        ]
                    );
vxqlmq5t

vxqlmq5t1#

使用file_exists()

$script_file_path = get_template_directory() . '/' . $this->get_block_dir_path($slug) . 'script.js';

if (file_exists($script_file_path)) {
    $enqueue_script = get_template_directory_uri() . '/' . $this->get_block_dir_path($slug) . 'script.js';
} else {
    $enqueue_script = '';
}

acf_register_block(
    [
        'name'            => $slug,
        'title'           => $file_headers['title'] ?: __('Unnamed Block:', 'bluegfx_dev') . ' ' . $slug,
        'description'     => $file_headers['description'],
        'category'        => $file_headers['category'] ?: 'formatting',
        'icon'            => $file_headers['icon'],
        'keywords'        => explode(' ', $file_headers['keywords']),
        'supports'        => json_decode($file_headers['supports'], true),
        'render_callback' => [$this, 'block_render_callback'],
        'enqueue_style'   => get_template_directory_uri() . '/' . $this->get_block_dir_path($slug) . 'style.css',
        'enqueue_script'  => $enqueue_script,
    ]
);

相关问题