I’ve created a custom post submission form for a specific category on my WordPress site. The form works fine for submitting posts, but there’s an issue with translation.
When a user submits a post using the form, the post is created but not translated immediately. However, if I then manually edit and save the post from the backend, it automatically gets translated as expected.
It seems like the translation is only triggered when a post is updated, not on the initial submission.
What I’ve tried so far:
Checked the form code and confirmed the correct category is assigned.
Verified that translation settings are working correctly for posts submitted through the WordPress dashboard.
Looked into hooks like save_post and wp_insert_post, but haven’t been able to trigger the translation on submission.
It's been awhile and there may be a better way to accomplish what you are wanting to do. But the only way I found to do it (for a different purpose though) is something like the following.
I used OO. Here is how I get into the workflow you are trying to get into and how you can detect the various transition states of a WP post.
Step one.
Use the correct action.
This code is taken from a WP plugin I wrote a while back that bridged WP and Xenforo forum. The important parts is parameter 1, 3, 4. You can look all of the actions up in the WP developer docs. This was in the init method of one of my classes and simply makes the call to add_action. Here I am passing it $this and a method named process as a callable. How you set that up is up to you. Just remember, $this points to the same instance that the below code was taken from.
add_action(
'transition_post_status',
[$this, 'process'],
10,
3
);
Step Two.
Code that can check the transition state of the post. Here is my process method which did that. There is probably a more elegant way to write this but it worked for what I needed. I trimmed the code just to give an example of the $post properties/transition states you are probably interested in. ID, content. It's pretty well covered in the WP docs. I did not include the handleDelete($post) method from my code since its not relevant.
public function process(string $newStatus, string $oldStatus, object $post)
{
$transCount = did_action('transition_post_status');
$status = $post->post_status ?? null;
$result = match (true) {
($transCount === 1 && $oldStatus === 'publish' && $newStatus === 'publish') => $this->handlePost($post),
($transCount === 1 && $oldStatus === 'publish' && $newStatus === 'trash') => $this->handleDelete($post),
/**
* old = auto-draft | new = draft | trans count = 1
* old = new | new = inherit | trans count = 2
* old = draft | new = draft | trans count = ? 1
*/
default => $this->initMetaData($post->ID), // This just saved some pointers to the post as WP metadata.
};
}
protected function handlePost($post) // handle translation here
{
// $post->post_content;
// $post->ID;
}
For more information you can see the following links in the WP developer docs/wiki.
https://developer.wordpress.org/reference/hooks/transition_post_status/