Skip to content

Add support for custom script parameters in alter_scripts hook - #214

Open
otacke wants to merge 3 commits into
h5p:masterfrom
otacke:allow-custom-script-parameters
Open

Add support for custom script parameters in alter_scripts hook#214
otacke wants to merge 3 commits into
h5p:masterfrom
otacke:allow-custom-script-parameters

Conversation

@otacke

@otacke otacke commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?
Feature: When merged in, will allow developers to pass custom parameters via the alter_scripts hook that the injected scripts can access via H5PIntegration.

What is the current behaviour?
Developers can add their own JavaScripts into the context of H5P libraries using the alter_scripts hook. That hook, however, only allows to add the paths to the scripts to inject. Developers cannot pass any parameters that the script might need to be configured dynamically based on the user role, the user name or any other parameter that the H5P integration can provide.

In particular, this becomes relevant if the script that is injected needs to establish communication with the server and requires to add a server-defined nonce/CSRF tokens/session based tokens to the request in order to avoid cross-site request forgery.

What is the new behaviour?
The hook's signature was amended to also accept a $custom_script_parameters argument (optional in order to not break existing hook use). It's an object passed by reference that developers can amend by setting key/value pairs, e.g.

// PHP code to use inside the hook
$custom_script_parameters->foo = "bar";

If any key/value pair was set by the hook, the H5P integration will then add the custom script object to window.H5PIntegration as window.H5PIntegration.customScriptParameters. The scripts that were injected by developers can then retrieve the values, e.g.

// JavaScript code to use inside an injected script
const myBar = window.H5PIntegration.customScriptParameters?.foo`.

This merge request was inspired by @pjotrsavitski (#80).

PR Checklist

Before submitting, please confirm:

  • I searched for existing issues/PRs first
  • I linked related issues/discussions
  • I tested my changes locally

Read the contribution guidelines to get your pr accepted more quickly.

@otacke otacke added the feature New or improved product feature label Aug 7, 2026
public function alterLibraryFiles(&$files, $libraries) {
$plugin = H5P_Plugin::get_instance();
$plugin->alter_assets($files, $libraries, 'editor');
$custom_script_parameters = (object) array();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@otacke do we have a standard for this? I noticed in the other PR https://github.com/h5p/h5p-wordpress-plugin/pull/218/changes#diff-136c1cb88ba22448ea89193777a49f07d2f68706fabd465e66c230d05f831799R1279 you are using [] modern array syntax but here is old school array() are we adhering some sort of non defacto standard? like using array() for initializers but using [] for other scopes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arzola Good catch. H5P Group does not have guidelines for PHP, no phpcs.xml or similar. I then usually fall back to using what the surrounding code dictates.

Cmp. h5p/h5p-php-library#290 (comment)

@arzola arzola moved this from Backlog to Verified by Community Maintainer in H5P Open Source Project Aug 25, 2026
@arzola

arzola commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

ok that makes sense I think I'll move this to verified by community maintainer, until we have a coding standard set up I think that's a good rule of thumb for that.

@otacke

otacke commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

@arzola Great! I'll try to push that subject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New or improved product feature

Projects

Status: Verified by Community Maintainer

Development

Successfully merging this pull request may close these issues.

2 participants