Register a Shortcode to Run Editor Scripts
Article Table of Contents
- What is a Shortcode?
- Register Your Shortcode with WordPress
- Apply Scripts by Shortcode in the Popup Editor
What is a Shortcode?
Register Your Shortcode with WordPress
- $tag - the name of the shortcode to be used in the WordPress content editor, and
- $callback - the name of a custom, shortcode processing function to run when the shortcode is found by WordPress.
If you need assistance with creating a shortcode, we recommend using the Shortcodes Generator at ‘GenerateWP’. Select the navigation elements 'Shortcode', 'Attributes' and 'Code' on the site, and complete the form to create your custom code snippet.
Best practice is to copy the generated code and paste it into a stand-alone plugin in order to register the shortcode within your site. It also encapsulates the code, so that if anything goes wrong, the code can be isolated and evaluated separate from the rest of your site.
Apply Scripts by Shortcode in the Popup Editor
WordPress runs the API function 'wp_kses' in the background to filter all content before it's saved to the database. That function strips out <script> tags before the editor content can be saved to the database. After 'wp_kses' runs, <script> tags appear in the editor as a string of characters that will not execute the intended script.
Executing PHP Scripts
Plugin users have inquired about modifying the PHP code in the popup template file rendered in the Popup Editor. We do not recommend direct modification of the file '/popup-maker/templates/template.php' to hook in custom functions. At some point in the future, that file may change. Any prior customizations to that file would be overwritten by the plugin update.
The better solution is to register a shortcode with WordPress (see above; 'Register Your Shortcode with WordPress' ) and use the shortcode in the popup content editor.
Use Case: Google AdSense
Here's a link to a simple plugin that you can add to your site in which to test out the registration of your shortcode.
Use Case: Email Provider's Form Code
Here is an example of how such code might be registered to a shortcode:
Notice that the function that registers the shortcode to WordPress contains the PHP functions 'ob_start' and 'ob_get_clean'. These refer to an 'output buffer' which temporarily stores the data passed to 'add_shortcode' and then returns it to WordPress for further processing.
Related Article: The Shortcode API (WordPress Codex).
Related Article: Documentation for 'add_shortcode' (KnowTheCode.io/Docx)