Skip to content

feat(new): insert code at the start of the body tag#391

Open
rami-elementor wants to merge 6 commits into
core-betafrom
body-content
Open

feat(new): insert code at the start of the body tag#391
rami-elementor wants to merge 6 commits into
core-betafrom
body-content

Conversation

@rami-elementor
Copy link
Copy Markdown
Contributor

@rami-elementor rami-elementor commented May 8, 2026

Currently the plugin supports 2 locations:

Screenshot 2026-05-08 at 20 31 01

However, WordPress supports 3 locations:

WordPress location Insert code to... Code snippets support
wp_head() Insert HTML/JS code to site header (to the <head> tag) Supported
wp_body_open() Insert HTML/JS code to site content (start of <body> tag) Not supported (yet)
wp_footer() Insert HTML/JS code to site footer (end of <body> tag) Supported

This PR aims to add support for wp_body_open to code-snippets. To allow users the ability to insert code at the start of the <body> tag.

In pro:

image

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a third “site insertion” location by supporting WordPress’s wp_body_open hook so HTML snippets can be output at the start of the <body> tag.

Changes:

  • Added a new snippet scope/location for body-start insertion (body-content) and wired it to wp_body_open.
  • Extended scope lists/icons/names across PHP + TS so the new location is selectable and validated.
  • Updated Playwright E2E helpers/tests to cover the new body-start location.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/e2e/helpers/constants.ts Adds SITE_BODY location constant for E2E location selection.
tests/e2e/code-snippets-evaluation.spec.ts Adds E2E coverage for body-start HTML insertion.
src/php/Utils/i18n.php Adds translation strings for new body-start scopes.
src/php/snippet-ops.php Ensures active-snippet cache invalidation includes body-content.
src/php/Model/Snippet.php Adds new scopes to the allowed scope list + icons + display names.
src/php/Integration/Evaluate_Content.php Hooks wp_body_open and prints body-content snippets.
src/js/types/Snippet.ts Extends TS scope unions to include new body-start scopes.
src/js/components/EditMenu/SnippetForm/fields/SnippetLocationInput.tsx Exposes body-start location in the snippet editor location picker.

Comment thread tests/e2e/helpers/constants.ts Outdated
Comment thread tests/e2e/code-snippets-evaluation.spec.ts Outdated
rami-elementor and others added 2 commits May 8, 2026 22:33
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Comment thread src/php/Integration/Evaluate_Content.php
Comment thread tests/e2e/code-snippets-evaluation.spec.ts
Comment thread tests/e2e/code-snippets-evaluation.spec.ts
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread tests/e2e/helpers/SnippetsTestHelper.ts
Comment thread tests/e2e/helpers/SnippetsTestHelper.ts
@rami-elementor rami-elementor added the run-tests Trigger automated tests label May 12, 2026
@rami-elementor rami-elementor marked this pull request as ready for review May 13, 2026 07:48
@rami-elementor rami-elementor requested a review from a team May 13, 2026 08:10
@sheabunge
Copy link
Copy Markdown
Member

How well supported is wp_body_open? My understanding is that it relies on the theme author to actually use the tag – can we be sure that it is now as widely-supported as wp_head and wp_footer? If not, do we need to include a check to ensure it's being used, and display a message/disable the feature if not.

@louiswol94
Copy link
Copy Markdown
Contributor

Thank you @rami-elementor . I needed this for one of my tasks :D

@rami-elementor
Copy link
Copy Markdown
Contributor Author

@sheabunge All 3 functions relies on the theme author.

wp_head() introduced in WordPress 1.5.0
wp_footer() introduced in WordPress 1.5.1
wp_body_open() introduced in WordPress 5.2.0

WordPress 5.2.0 was released on May 2019, over 7 years ago.

I think that after 7 years it's safe to assume that this function is widely used.

@louiswol94
Copy link
Copy Markdown
Contributor

louiswol94 commented May 18, 2026

The HTML half of this (body-content) integrates correctly with Flat_Files - the existing Content_Snippet_Handler writes any HTML scope to …/html/<id>.php, and Evaluate_Content is updated to read body-content back.

The JS half (site-body-js) does not. It's added to scope lists and the UI, but Evaluate_Assets (Pro) - both the DB-backed enqueue_frontend() and the flat-file enqueue_js_flat_files() - only handle site-head-js and site-footer-js. With file-based execution enabled, a site-body-js snippet will be written to …/js/<id>.js and then silently never enqueued. get_asset_url() and process_code() also need a third branch. Needs an extension of this PR in Pro to add a wp_body_open-driven enqueue path for site-body-js, plus a Playwright test.

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

Labels

run-tests Trigger automated tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants