How to modify post content before writing to database?

Hi~ I’m back again and I’m trying to add another function to the script I was working on a plugin that notify my friends of new post that mentions(@) them. Now I want to change the plain text of my friends’ names that I mentioned into links to their own wordpress blog address (if they have) once I hit publish. So @David will become David.

I search this question many times but all results are like following:

add_filter('the_content', 'replace_custom_word');

which doesn’t really modify the database, right? Edit: I don’t know, maybe I’m wrong.

Below is the code I’ll use to get David’s blog address.

        $friend_url = $wpdb->get_var( $wpdb->prepare( "

                                                       SELECT comment_author_url
                                                       FROM $wpdb->comments
                                                       WHERE comment_author
                                                       LIKE %s ",
                                                       $friendCorrectName
                                                       )) ;

So please tell me what’s the best way of doing this. Thanks a lot in advance!

The complete code for this script is pasted below too, please feel free to comment on what I have now too. :)
You can check out my last post if you see something weird, or you can just ask me.

function email_friend()  {

    // get post object
    $post = get_post($id);
    // get post content
    $content = $post->post_content;
    // get how many people is mentioned in this post
    $mentionCount = preg_match_all('/(@[^\s]+)/', $content, $matches);


    // if there is at least one @ with a name after it
    if (0 !== $mentionCount) {

        $friendList = array();//for storing correct names

        for ($mentionIndex=0; $mentionIndex < $mentionCount; $mentionIndex++) {

            $mentionName = $matches[0][$mentionIndex];  
            $mentionName = str_replace('_',' ',$mentionName); //change _ back to space
            $mentionName = substr($mentionName, 1); //get rid of @
            //for security and add wildcard
            $friend_display_name_like = '%' . like_escape($mentionName) . '%'; 

            global $wpdb;

            // get correct name first
            $friendCorrectName = $wpdb->get_var( $wpdb->prepare( "

                                                           SELECT comment_author
                                                           FROM $wpdb->comments
                                                           WHERE comment_author
                                                           LIKE %s ",
                                                           $friend_display_name_like
                                                           )) ;

            // get friend email by comment author name
            $friend_email = $wpdb->get_var( $wpdb->prepare( "

                                                           SELECT comment_author_email
                                                           FROM $wpdb->comments
                                                           WHERE comment_author
                                                           LIKE %s ",
                                                           $friendCorrectName
                                                           )) ;

            // get friend's blog address
            $friend_url = $wpdb->get_var( $wpdb->prepare( "

                                                       SELECT comment_author_url
                                                       FROM $wpdb->comments
                                                       WHERE comment_author
                                                       LIKE %s ",
                                                       $friendCorrectName
                                                       )) ;

            //if we have David's blog address in database
            if ($friend_url) {


                //this is where I need help with. 
                //I need to modify post content before writing it into database now
                //I need to change the plain text name after @ to a link to his blog










            }

            if($friend_email) {// if found email address then email

                $postTitle = get_the_title($id);
                $post_permalink = get_permalink( $id );
                $to =   $friend_email;
                $subject =   'Arch!tect mentioned you in his new post 《'.$postTitle . 
                '》';
                $from = "noreplay@swotong.com";
                $headers = "From:" . $from;
                $message = "Arch!tect mentioned you in his new post《".$postTitle . 
                "》 check it out?\n\n"  ."Post link:".$post_permalink
                ."\n\n\nPlease don't reply this email.\r\n";

                if(mail($to, $subject, $message, $headers)) {
                    //if send successfully put his/her name in my list
                    array_push($friendList, $friendCorrectName);
                }

            } 

        } 
        $comma_separated_list = implode(",", $friendList); //friend list array to string 

        // now send an email to myself about the result
        $postTitle = get_the_title($id);
        $post_permalink = get_permalink( $id );
        $to =    'myOwn@email.com';
        $subject =   "Your new post《".$postTitle . 
        "》has notified ".count($friendList)."friends successfully";
        $from = "noreplay@email.com";
        $headers = "From:" . $from;
        //list all friends that received my email
        $message = "Your new post《".$postTitle . 
        "》has notified ".count($friendList)."friends successfully:\n\n".
        $comma_separated_list;
        mail($to, $subject, $message, $headers);
    }

}//end of email_friend function

add_action ( 'publish_post', 'email_friend' );

EDIT2: to be more specific, the code above where I comment ” //this is where I need help with. ” is the place I need help with.

Solutions Collecting From Web of "How to modify post content before writing to database?"

You generally use add_filter before displaying information. For your case you can use add_action('publish_post', 'your_function') and then you intercept $_POST values. The content of the WYSIWYG editor is available through $_POST['content'].

The hook is of type publish_{post_type} btw. You should change accordingly.

Example:

function wpse_89292_save_my_post()
{
print_r( $_POST['content'] ); # will display contents of the default wysiwyg editor
die;
}
add_action('publish_post', 'wpse_89292_save_my_post');

Using wp_insert_post_data filter:

function wpse_89292_save_my_post( $content ) {
  global $post;
  if( isset($post) && get_post_type( $post->ID ) == 'post' ){
    $content['post_content'] = function_to_manipulate_the_content();
  }
  return $content;
}
add_filter( 'wp_insert_post_data', 'wpse_89292_save_my_posts' );

maybe You can use wp_update_post (if you are editing post) or wp_insert_post (if it is new post)function to edit what you want in the post. It really works.