Fetch the User-Agent String and Save as User Meta

Some times a client will complain that their site doesn’t look right or something equally as vague. Inevitable we ask “What browser are you using?” but getting the answer can be tricky. So, let’s just automatically get their user-agent and save it where we can access it.

Granted, this isn’t perfect. The client could have just logged in from someone else’s computer, and it is possible to fake the user-agent… but I’m counting on this being rare.

<?php
/*
Plugin Name: Last UserAgent
Plugin URI: https://trepmal.com/
Description: save user-agent as user-meta
Author: Kailey Lampert
Version: 1.0
Author URI: http://kaileylampert.com/
*/
/*
    Copyright (C) 2010  Kailey Lampert

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

$lastbrowser = new lastbrowser();

class lastbrowser {

	function lastbrowser() {
		add_action( 'admin_init', array( &$this, 'fetch_and_save' ) );
		add_action( 'show_user_profile', array( &$this, 'show_useragent' ) );
		add_action( 'edit_user_profile', array( &$this, 'show_useragent' ) );
	}
	
	function fetch_and_save() {
		$ua = $_SERVER['HTTP_USER_AGENT'];
		$id = get_current_user_id();
		update_user_meta( $id, 'Last UserAgent', $ua, get_user_meta( $id, 'Last UserAgent', true ) );
	
	}
	function show_useragent( $profile ) {
		echo '<h3>Last UserAgent</h3>';
		echo get_user_meta($profile->ID, 'Last UserAgent', true);
	}
		
}

One thought on “Fetch the User-Agent String and Save as User Meta”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: