0

Let's say I want to write a plugin that replaces (i.e.: overwrites) every src-tag of every iframe-element on a WP-Website with a custom string AND the plugin shall watch every new post or page-edit whenever a new iframe is inserted, how would I go about it? Is there something like an event-api for WP every time an edit visible on the frontend occurs?

1
  • 1
    If you're doing this for some kind of lazy loading, which is the reason I can think of that you'd want to do this, be aware that browsers now support native lazy loading with the loading attribute and WordPress already set this to enable lazy loading. Commented Jan 15, 2023 at 9:38

1 Answer 1

2
  1. Use the "save_post" action hook, which is triggered every time a post or page is created or updated. In the function called by the hook, you can check the post content for new iframes, and if found, replace the src attributes of iframe elements before they are saved to the database.

  2. Use the "the_content" filter hook, which allows you to modify the post content before it is displayed on the frontend. In the function called by the filter, you can replace the src attributes of all iframe elements on the website with a custom string.

  3. To limit the plugin functionality to certain post types, you can check the post type before making the replacement in both the "save_post" and "the_content" functions.

  4. Once the plugin is ready, activate it in the WordPress backend and it will start listening for post and pages changes and replacing the iframe src attributes as per your custom string.

1
  • Great! Any performance issues? Esp. when content of the web page is rendered by "the_content"? Commented Jan 21, 2023 at 4:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.