Background of the issue:
I am a web designer and I would like to be able to create a CSV file with an estimate of the number of words that needs to be translated, so I can send it to the customer for approval. At the moment I create a screenshot (attached) with the Word count from the Translation Dashboard, but that sometimes needs multiple screenshots from multiple sections and that looks unprofessional.
I would like the report to have a list with the following columns, example attached:
- selected item
- item type (ie each page)
- the word count of the item
I want this to be in a CSV file, so I can use it in Excel to add a calculations of the expected costs for each language (depending on the translation engine or translation service).
Symptoms:
There is no direct feature in WPML to generate a CSV file with word count estimates for translation.
Questions:
How can I generate a CSV file with a list of selected items, item types, and word counts for translation estimates?
Our second-tier supporter suggests the following: You can write simple PHP code that will take the value of ID, Title, and the number of words in the post meta _wpml_word_count and output it to a CSV file. Here is a sample code generated by ChatGPT.
function export_word_count_to_csv() {
// Adjust your query args as needed
$args = array(
'post_type' => 'post', // Change to your desired post type
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'ASC',
);
$query = new WP_Query($args);
if ($query->have_posts()) {
// Set headers to force download CSV
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=word-count-export.csv');
$output = fopen('<em><u>hidden link</u></em>', 'w');
// Output header row
fputcsv($output, array('ID', 'Title', 'Word Count'));
// Loop through posts
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$title = get_the_title();
$word_count = get_post_meta($post_id, '_wpml_word_count', true);
// If no value, fallback to 0
if (!$word_count) {
$word_count = 0;
}
fputcsv($output, array($post_id, $title, $word_count));
}
fclose($output);
exit; // stop script after CSV output
} else {
echo 'No posts found.';
}
wp_reset_postdata();
}
// You can call this function via a custom admin page, WP-CLI command,
// or hook it to an action like 'init' with a conditional check for a URL param
// Example: <em><u>hidden link</u></em>
add_action('init', function () {
if (is_admin() || !isset($_GET['export_word_count'])) {
return;
}
export_word_count_to_csv();
});
The following are the steps you need to follow.
1. Add this code to your theme's functions.php file or in a custom plugin.
2. Visit your site with the ?export_word_count=1 query string in the URL (e.g., hidden link).
3. A CSV download should start with the structure:
I tried it on my test, and it works for posts.
Please note that you may need to add other post types to adjust it to your needs.
Hi Itamar, sorry for the late reply, I never received a notification from your reply because somehow email notifications were disabled for this ticket.
Although the given solution would probably work, I wasn't really looking for a custom function to make this happen right now. It was meant as a feature request, to get a button in the translation dashboard to download a csv file for the *selected* records.
--
Reading back my original question. This is where your support question rewriting tool went all wrong. The 'question' it wrote wasn't the question I asked nor my intention.
I hate that support AI from the bottom of my heart. It always completely rewrites my carefully written question into something with less detail and something I didn't want. To avoid that I have to correct the AI by rewriting my question all over again, I simply copy my original text before submitting the ticket, after that I replace the AI summary with my own text and I can't be bothered with the AI symptoms and questions anymore.
I already asked several time to please please remove that rewriting AI. I have never seen anything good coming out of it. Or at least, keep the original input as a reference instead of removing it from the ticket all together.
I can assure you that I passed it on to my superiors, and it is clearly a feature request. However, I can't promise it will be implemented. This is up to my supervisors to decide.
I am sorry for any inconvenience the AI support wizard caused you. You already expressed your dissatisfaction with it in a previous ticket, and I passed it on to our support manager.