Celebrate WordCamp Asia! - 30% OFF with coupon WC30 at checkout - Terms

How to Block Submissions by User Agent

It is possible to block WordPress form submissions by a particular user agent by using the wsf_submit_field_validate filter hook.

An example filter hook function is shown below. This function will run for all forms on your form when they are submitted.

// My validation function
function wsf_submit_block_user_agent( $field_error_action_array, $field_id, $field_value, $section_repeatable_index, $post_mode, $submit ) {

    // Only process validation if the form is submitted and not saved
    if ( $post_mode !== 'submit' ) {
        return $field_error_action_array;
    }

    // User agents to block
    $user_agents_to_block = array(

    	'Linux aarch64'
    );

    // Check if $_SERVER and the User-Agent header exist
    if (
    	isset( $_SERVER ) &&
    	isset( $_SERVER[ 'HTTP_USER_AGENT' ] )
    ) {
    	// Get user agent
        $user_agent = $_SERVER[ 'HTTP_USER_AGENT' ];

        // Check user agents to block
        foreach( $user_agents_to_block as $user_agent_block ) {

            // Check if user agent is present in the User-Agent string
            if ( stripos( $user_agent, $user_agent_block ) !== false ) {

                // Send a 403 Forbidden response and exit
                header( 'HTTP/1.1 403 Forbidden' );
                exit;
            }
        }
    }

    // Return $field_error_action_array to WS Form
    return $field_error_action_array;
}

// Add a callback function for the wsf_submit_block_agent filter hook
add_filter( 'wsf_submit_field_validate', 'wsf_submit_block_user_agent', 10, 6 );