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 Answer
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.
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.
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.
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.
- Great! Any performance issues? Esp. when content of the web page is rendered by "the_content"?trippodi– trippodi2023-01-21 04:19:04 +00:00Commented Jan 21, 2023 at 4:19
loadingattribute and WordPress already set this to enable lazy loading.