Trigger wp_insert_post_data handler on every post in WordPress

19 May 2019 in TIL

Update: This site no longer uses WordPress, but the content in this post may still be relevant

I use prism to handle all of the code highlighting for this site, which works great apart from one thing - it expects the class on a <pre> element to be in the format language-<name> rather than just <name>.

As I write all my content in Markdown, I need to add a wp_insert_post_data handler to make this change as the HTML is being saved to the DB.

function filter_handler( $data , $postarr ) {
// Modify data as required
add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );

I didn't want to go back and trigger an update of every single post (there's quite a few on here now), so I worked out a way to update all published post states to published. This doesn't change the content but it does re-trigger the wp_insert_post_data filter.

So I present to you, my one liner (using WP CLI) that triggers a rebuild of every page:

for i in `wp post list --fields=ID,post_status | grep publish | awk '{print $1}'`; do
wp post update $i --post-status=publish;