Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active January 29, 2018 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/393c66c50bddf8a92077854dce8af370 to your computer and use it in GitHub Desktop.
Save damiencarbery/393c66c50bddf8a92077854dce8af370 to your computer and use it in GitHub Desktop.
Edit WordPress post during save - Edit post content just before it is recorded in the database - could be used to add, remove or edit the content.
<?php
/*
Plugin Name: wp_insert_post() modifications
Plugin URI: https://www.damiencarbery.com/2018/01/edit-wordpress-post-during-save/
Description: Change post data before save - remove #hashtags from post content before save.
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'wp_insert_post_data', 'change_post_data_before_save', 10, 2 );
function change_post_data_before_save( $data, $postarr ) {
//error_log( 'Data: ' . var_export( $data, true ) );
//error_log( 'PostArr: ' . var_export( $postarr, true ) );
if ( 'publish' == $data[ 'post_status' ] ) {
# Find the first # and extract the content up to that point.
$first_hashtag = strpos( $data[ 'post_content' ], '#' );
$before_hashtags = substr( $data[ 'post_content' ], 0, $first_hashtag );
$data[ 'post_content' ] = $before_hashtags;
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment