Skip to content

Custom Code

Adds a JavaScript snippet to a page via an Elementor HTML widget. The plugin auto-wraps the snippet in <script> tags (optionally inside a DOMContentLoaded listener).

Input:

  • post_id
  • parent_id: container to add the HTML widget into
  • position: child index, -1 to append
  • js: JavaScript code (no <script> wrapper)
  • wrap_dom_ready: wrap in DOMContentLoaded listener (default true)

Returns: { element_id }.

Capability: edit_post + unfiltered_html on the calling user.

Adds custom CSS to a specific element (using Elementor Pro’s per-element custom CSS) or to the entire page (using the page-level custom CSS panel). The selector keyword is supported and rewritten to the actual element’s CSS selector.

Input:

  • post_id
  • target: page or an element_id
  • css: CSS code, can use selector keyword when targeting an element

Returns: { success: true }.

Requires Elementor Pro’s custom CSS module. Capability: edit_post + ownership.

Creates a site-wide Custom Code snippet using Elementor Pro’s Custom Code feature: injects code into <head>, <body> start, or <body> end of every page on the site.

Input:

  • title: descriptive name
  • code: full code including <script> / <style> tags
  • location: head, body_start, or body_end
  • priority: load order (1-10, default 10)
  • status: publish, draft

Returns: { snippet_id, edit_url }.

Requires Elementor Pro custom code module + manage_options + unfiltered_html.

Lists every Custom Code snippet on the site with location, priority, status.

Returns: array of { id, title, location, priority, status, modified }.

Requires Elementor Pro custom code module + manage_options.

Analytics on one page: add-custom-js with a page tracker snippet. Stays scoped to that page only.

Element-level CSS tweak: add-custom-css with target set to a specific element ID and css using the selector keyword:

selector .some-child {
background: red;
}

Site-wide third-party script: add-code-snippet with location: 'head' and the script tag. Loads on every page.

All three write tools require unfiltered_html (or manage_options for snippets). On multisite, only super-admins have unfiltered_html by default, so on multisite, regular admins can’t use these tools unless capabilities are explicitly granted. This matches WordPress’s own custom-HTML widget behavior.

All four sit in the Custom Code section of the Elementor tab on EMCP Tools → Tools (the tab appears while Elementor is the builder selected on EMCP Tools → Page Builders). add-custom-js is on by default. The three Pro tools register only while Elementor Pro is active, and on a new install they start switched off: switch them on in that section, then choose Save changes in the save bar. See Disabling tools.

These tools are separate from the PHP Snippets in the EMCP Sandbox, which run PHP rather than front-end code.