I'm using shortcodes to insert some image links to my posts I grab from XML files (by an ID). As this needs to be executed just once it's not necessary to this everytime a user loads the page. So how can I replace the Shortcode with the needed content as soon as i save/publish/update the page?


That's not how shortcodes work. Shortcodes are meant to be interpreted whenever the page is rendered - they're used when WordPress filters the page/post content.

What you're looking for is a content template. A tag the user can add to the page that will be converted into something else when the post is saved, like an RTF tag in a mail merge.

There's a filter that's run before posts are inserted into the database: wp_insert_post_data. This filter will pass the data for the to-be-inserted post as an array. You can take that array, parse your custom tags into whatever content you need, and pass it back before it's saved to the database.

Just filter $data["post_content"] and $data["post_content_filtered"] to make your replacement before passing the $data array back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.