You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中H5P插件CSS覆盖的路径声明规范及配置疑问

解答:自定义H5P插件CSS的路径与代码疑问

Hey there! Let's break down your questions clearly since I know WordPress plugin path logic can feel tricky when you're not deep into development.

1. 什么是__FILE__?

__FILE__是PHP的魔术常量,它会返回当前正在执行的PHP文件的完整绝对服务器路径。举个例子,如果你的phpmods.php放在/wp-content/plugins/h5pmods/目录下,__FILE__的值会类似:

/home/your-website/public_html/wp-content/plugins/h5pmods/phpmods.php

而plugin_dir_url(__FILE__)是WordPress的内置函数,它会把这个绝对路径转换成网站可访问的URL格式,比如:

https://your-website.com/wp-content/plugins/h5pmods/

这样就能精准指向你存放自定义文件的目录,不用担心网站安装在子目录或者路径变动的问题。

2. 正确的代码格式是怎样的?

你需要修改h5pmods_alter_styles函数里的path值,替换成用plugin_dir_url(__FILE__)拼接的路径。同时要修正原代码里的转义字符(比如&要改成&)。修改后的完整代码如下:

/**
 * H5P Mods Plugin.
 * Alters the way the H5P plugin works.
 * @package H5P
 * @author Joubel <contact@joubel.com>
 * @license MIT
 * @link http://joubel.com
 * @copyright 2015 Joubel
 *
 * @wordpress-plugin
 * Plugin Name: H5P Mods
 * Plugin URI: http://www.h5p.org
 * Description: Overrides the H5P native H5P CSS with your own adjustments.
 * Version: 0.0.1
 * Author: H5P
 * Author URI: http://www.h5p.org
 * Text Domain: h5pmods
 * License: MIT
 * License URI: https://opensource.org/licenses/MIT
 * Domain Path: /languages
 * GitHub Plugin URI: https://github.com/h5p/h5pmods-wordpress-plugin
 */
// If this file is called directly, abort.
if (!defined('WPINC')) {
 die;
}
/**
 * Allows you to alter which stylesheets are loaded for H5P. This is
 * useful for adding your own custom styles or replacing existing once.
 *
 * @param object &$styles List of stylesheets that will be loaded.
 * @param array $libraries The libraries which the styles belong to.
 * @param string $embed_type Possible values are: div, iframe, external, editor.
 */
function h5pmods_alter_styles(&$styles, $libraries, $embed_type) {
 $styles[] = (object) array(
 'path' => plugin_dir_url(__FILE__) . '/custom-h5p.css',
 'version' => '?ver=1.0' // Cache buster
 );
}
add_action('h5p_alter_library_styles', 'h5pmods_alter_styles', 10, 3);

3. custom-h5p.css应该放在哪里?

按照最优建议,你应该把custom-h5p.css和phpmods.php放在同一个文件夹里,也就是/wp-content/plugins/h5pmods/目录下。这样plugin_dir_url(__FILE__)就能准确找到它,而且把所有自定义修改的文件放在一个独立子目录里,既整洁又方便维护(比如备份、修改都不用到处找文件)。

为什么这种方式更优?

  • 避免把自定义CSS放在uploads目录:uploads可能会被清理、或者被WordPress的媒体库逻辑干扰,安全性和稳定性不如插件目录。
  • 插件化管理:把你的H5P修改做成一个独立的小插件,更新H5P主插件时不会覆盖你的自定义代码,也能通过WordPress插件管理界面快速启用/禁用。

内容的提问来源于stack exchange,提问作者Alejandro Goffa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 11:42:56