Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Can you make your question more specific... –  Vinod Dalvi Mar 4 '13 at 10:52
add comment

2 Answers

up vote 2 down vote accepted

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' );
share|improve this answer
    
"and then you intercept $_POST values." thanks for answering but I don't quite get this part.. –  Arch1tect Mar 4 '13 at 11:24
    
Added a function to help you. –  RRikesh Mar 4 '13 at 11:55
    
thanks a lot. I can see how to display content now but how do I modify content before writing to database? For example, change all @ symbol to # ? –  Arch1tect Mar 4 '13 at 23:50
    
you need a function to replace the twitter name with the url... –  RRikesh Mar 5 '13 at 4:51
    
Is this the right way to modify content? I tried $_POST['content'] = "nothing" and $_POST['content'] = 0; neither work out, the content remains unmodified... @rrikesh –  Arch1tect Mar 7 '13 at 0:33
show 2 more comments

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.

share|improve this answer
    
I just want to add a few links to names.. –  Arch1tect Mar 4 '13 at 11:27
    
So you need to just preg_replace("!$mentionName!im", $friend_url, $content,$a); function. In this example $a is a number of replaces in the text. –  bigwolk Mar 4 '13 at 11:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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