Document and Data Automation: Forwarding data from WordPress to a Docxpresso template

In some advanced use cases it may be necessary, or just convenient for user experience, to forward data from your WordPress website to a Docxpresso template that has been embedded with our “Document and Data” plugin (you may download it for free from here).

From v1.3 onwards we allow advanced users to do so via the forwardDocxpressoData function.

To offer maximum flexibility this forwardDocxpressoData function should be defined in the very same WordPress installation because of the miriad possible scenarios that may need to be supported:

  • send specific user data,
  • send post data,
  • send the value of advanced custom fields or shortcodes,
  • send data coming from external data sources or services,

or any combination of the above among many other possibilities.

Anyhow as we will see in what follows that the procedure is simple enough for anyone with minimum programming skills.

But let’s learn by example: imagine that the Docxpresso template that we want to insert in our WordPress post has id = 22 and a field called username that we want to automatically fill with the name of the current registered user. In this case we will define something like:

function forwardDocxpressoData($data){
    //if we wish to send the username only to the template with id 22  
    //wrap your statement in an if clause
    if ($data['template'] == 22){
        $current_user = wp_get_current_user();
	$username =  $current_user->user_firstname . ' ';
        $username .= $current_user->user_lastname;
	//build an object with the required data and following structure
	$send = new stdClass();
	$send->varValues = new stdClass();
	$send->varValues->username = [$username];
	//now we should json encode it and add it to the $data array
	$data['varData'] = json_encode($send); 
    //do not forget to return the $data array
    return $data;

Now you should only include this function in any of the following places:

  • The functions.php of your WP theme or much better in a child theme to avoid losing it upon theme update.
  • The custom.php file included in the plugin. Once again if you update the plugin you should first backup that file so you can keep its changes.

Of course, all of the above is just an example and many other possibilities are opened by using:

  • get_post() to provide any specific post data.
  • get_remote_request($url, $args) to access data from an external service.
  • WPDB objet to directly query an external database.
  • WP_Meta_Query( $meta_query_args ) in order to do complex queries to WP databases.

In case you still have any doubt about how to use this new plugin functionality, please, do not hesitate to contact us.