Setting the Default Camera Direction for the Camera Field

Would you like to set the default camera direction for the WPForms Camera field on mobile devices?

By default, mobile browsers decide which camera to open when using the Camera field. In some cases, you may want to force the front camera or rear camera as the default when the camera opens.

This can be done with a small snippet that sets the default facing mode before the WPForms Camera field initializes.


Setting the Default Camera Direction

Now it’s time to add the snippet to your site.

If you need help adding custom code to your site, please see our tutorial on adding code snippets.

/**
 * Set the default camera direction for the WPForms Camera field.
 *
 * user        = front camera
 * environment = rear camera
 *
 * @link /https://wpforms.com/developers/setting-the-default-camera-direction-for-the-camera-field/
 */
add_action( 'wpforms_wp_footer', function() {
	if ( is_admin() ) {
		return;
	}
	?>
	<script>
	document.addEventListener('DOMContentLoaded', function() {
		if ( window.WPForms && WPForms.Camera ) {
			WPForms.Camera.currentFacingMode = 'environment';
		}
	});
	</script>
	<?php
} );

Once you add this snippet, the Camera field will use the selected camera direction as the default on supported mobile devices.

Changing the Camera Direction

To use the front camera, change this line:

WPForms.Camera.currentFacingMode = 'environment';

to:

WPForms.Camera.currentFacingMode = 'user';

Use the following values:

  • user for the front camera
  • environment for the rear camera

And that’s all you need to set the default camera direction for the WPForms Camera field.