0

I'm trying to make a wordpress plugin to auto-translate posts just before it's inserted, it's for a specific need where I want to write in a language, post in a different one, so far, I'm not able to hook the wp_insert_post, or I don't understand how to do this properly,

I posted my hook code here, I note it's my very first plugin, I appreciate any help or helpful remarks.

Mike,

function tr_insert_post($post, $wp_error = false)
{

    $post ['post_title'] = "the title: ".$post ['post_title'];

    return $post;
}


add_action('wp_insert_post', 'tr_insert_post',1,2); 
3
  • +1 for @Bainternet 's answer - Exactly! In that way you can reuse the same post for more translations, just translate the content. While using your way, you'd have to insert four different posts for four different languages! Oct 2, 2011 at 14:01
  • Thanks for advice, when I thought it this way I wondered about limitation or overload can alter the google or bing service while translating online, I would cache the translated content and use the_content() this need more work, I'm only making an experiment and not yet for a final production.
    – Mike
    Oct 2, 2011 at 15:59
  • You're welcome! No problem, anything that works for you! Oct 3, 2011 at 3:05

1 Answer 1

1

The right hook to use before inserting the post is wp_insert_post_data

function tr_insert_post($data){
    $post ['post_title'] = "the title: ".$post ['post_title'];
    return $post;
}

add_action('wp_insert_post_data', 'tr_insert_post',1,2); 

But I think that a better way to achieve that would be to save the post as is and translate later using a filter like the_content this way you save the post in the same language as you posted and you can edit it easily in that same language.

2
  • thanks for your answer, it's working now. Actually, Is there someway to modify Tags/Terms I can't see them in $data array? In my case, I have an automatic posting content from a publisher, so I don't really need to edit the original content.
    – Mike
    Oct 2, 2011 at 15:48
  • 1
    Post iit as a new question
    – Bainternet
    Oct 2, 2011 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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