create child page emplate dynamically in wordpress

Mixing some functions from here and here I wrote the code below in my functions.php:

// child page
function the_slug($pageID) { // Is there any other way to get the parent page slug?

	$post_data = get_post($pageID, ARRAY_A);
	$slug = $post_data['post_name'];
	return $slug;
}

function child_page_template(){
	global $post;
	$parent_page_slug = the_slug($post->post_parent);
	$page_template = 'page-'. $parent_page_slug . '-child.php'; //name it on your own
	$parents = get_post_ancestors($post->ID); // if is child
	if($parents){update_post_meta($post->ID,'_wp_page_template',$page_template);}

}

add_action('save_post','child_page_template'); 

Now, every time a child page is created or updated it will have as page template a file named “page-parentslug-child.php.

for example:
‘PARENT’ => page-slug.php (default)
‘parent’> ‘CHILD’ => page-‘PARENTSLUG’-child.php
‘parent’> ‘child’> ‘GRANDCHILD’ => page-‘CHILDSLUG’-child.php
‘parent’> ‘child’> ‘grandchild’ > ‘GRAND-GRANDCHILD’ => page-‘GRANDCHILDSLUG’-child.php (…) and so on.

That it