With gimp fu, I can save the content of one layer (at least, that is how I interprete the definition of gimp_file_save
because it takes the parameter
What is done internally, even by GIMP file-exporter plug-ins for all formats is: duplicate the image, merge all visible layers, them save the resulting drawable.
This is easier, and take less resources than it sounds. Effectively you just have to replace your save line
pdb.gimp_file_save(img, background_layer, '/temp/tq84_write_text.png', '?')
by
new_image = pdb.gimp_image_duplicate(img)
layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
pdb.gimp_file_save(new_img, layer, '/temp/tq84_write_text.png', '?')
pdb.gimp_image_delete(new_image)
(The last call just "deletes" the new image from program memory, freeing up the resources, of course)