Add a wordpress admin account programmatically

In cases when you have access to code and not to an existing admin account, here is a way to create an admin account for yourself without waiting for someone else to create it for you.

In your `functions.php` file, use the following code:

function wpAddAdmin() {
	$user  = 'fahad';
	$pass  = 'mypassword';
	$email = '[email protected]'; // password in plain text
	if ( ! username_exists( $user ) && ! email_exists( $email ) ) {
		$user_id = wp_create_user( $user, $pass, $email );
		$user    = new WP_User( $user_id );
		$user->set_role( 'administrator' );

add_action( 'init', 'wpAddAdmin' );

Save the file and access your wordpress admin area. Use the settings you updated in above code i.e the same username and password and you’d be able to access WordPress as admin. That’s all!


With great power comes great responsibility.
~ Not spiderman

PHP: Adding hours to timestamp fields while printing the formatted date

So imagine your MySQL is on a shared host and you have time stamps which are automatically inserted in the table. In this case, updating MySQL’s time zone settings is beyond your control. Or even if you can set the time zones, for any records that are in a specific time zone, you can add ¬†hours or minutes to the timestamp you get from the database in the PHP code.

Assuming your record object is $record and the timestamp field¬†is ‘timestamp’, the following code will allow you to add 5 hours to your timestamp and print in the d/m/Y format which you can update in the code below.

echo date('d/m/Y h:i:s A', strtotime($record['timestamp'] . '+5 hours'));

Hope you are going to find this tip useful.