Edit post content just before it is recorded in the database - could be used to add, remove or edit the content.
A few suggestions were made: use settings or filters on the plugin, write your own plugin, use jQuery to change displayed text. I suggested modifying the displayed content via ‘the_content’ filter or modifying the content before it is saved. I also mentioned using add_filter(), if the plugin allowed for this.
When I got to my laptop I downloaded the plugin and searched the source for apply_filters() calls but there weren’t any in the section that imported the content. I did see that it used wp_insert_post() to create the new blog post so I dived into that function and found a significant number of do_action() and apply_filters() calls.
I read through the entire wp_insert_post() function and copied each do_action() and apply_filters() call to a text file along with the comments that explained each one.
I decided to try out the ‘wp_insert_post_data’ filter as it was run just before the data is written to the database. This means that all other changes to the content will have been completed by then.
I wrote a small plugin that wrote the $data and $postarr variables to the debug.log. I quickly saw that auto drafts passed through the wp_insert_post() function and that the ‘post_status‘ was different for drafts, auto drafts and published posts.
To edit the content I went to Regex101 – online regex editor and debugger with some sample content. My regex skills are rusty and I just couldn’t get it to strip out hashtags, especially on a multiline string. I eventually gave up and went with strpos() to find the first hashtag and substr() to extract the content before it. This was simple and worked well.
More to do
I added the strpos/substr calls to the plugin, disabled the error_log() calls and posted the plugin for the member, with a note that the error_log() calls should be uncommented to see the differences between imported Instagram posts and normal posts. The differences can be added to the code to ensure that it only runs for Instagram posts.