WordPress user ID in SQL

WP Data Access version 5.2.3 adds a session variable containing the WordPress user ID to its database connections. This allows plugin users to access the WordPress user ID in SQL queries, database views, triggers, functions and procedures.

Open the Query Builder and enter:

select @wpda_wp_user_id

This returns your WordPress user.

Availability

Session variable @wpda_wp_user_id is available in all WP Data Access connections. It works with local and remote databases. It even works when you connect to SQL Server, MS Access or another database via a proxy.

Usage

Data Publisher

The WordPress user ID is available in custom queries:

select * from wpda_sas_student
where student_wp_user_id = @wpda_wp_user_id

Database triggers

The following triggers store the WP user ID in column student_wp_user_id of table wpda_sas_student on insert and update.

create trigger if not exists wpda_sas_student_insert before insert on wpda_sas_student
for each row
begin
	set new.student_wp_user_id = @wpda_wp_user_id;
end;
/
create trigger if not exists wpda_sas_student_update before update on wpda_sas_student
for each row
begin
	set new.student_wp_user_id=@wpda_wp_user_id;
end;
/

Make sure column student_wp_user_id is read-only in your Data Project (a new feature om version 5.2.3 as well). This prevents conflicting actions on the same column.

Usage in database procedures and function works similar.

Views

You CANNOT use a session variable in a view. This does NOT work:

create view v_error as
select * from wpda_sas_student
where student_wp_user_id = @wpda_wp_user_id

To use the WordPress user ID in a view we need to make the session variable available in a different way. Luckily the session variable can be accessed in a database function. When you start the Data Explorer, you will see a plug icon after the selected database name. When you press this plugin, the plugin creates a database function whih you can use in your views.

This works:

create view v_access_wp_user_id as
select * from wpda_sas_student
where student_wp_user_id = wpda_get_wp_user_id()

Now you can finally create lookups for your Data Projects using the WordPress user ID! 😊

Limitations

  • Session variable @wpda_wp_user_id is only available within a WP Data Access content. Developers can use the plugin API to access the session variable from their own code.
  • Function wpda_get_wp_user_id() can be accessed from external tools as well. When session variable @wpda_wp_user_id is undefined, the function returns null.
  • Function wpda_get_wp_user_id() needs to be created for every database separately.

Need more WordPress values in SQL?

WP Data Access gives you access to session variable @wpda_wp_user_id and function wpda_get_wp_user_id(). Other WordPress values can be added easily using hook wpda_dbinit.

The following code makes the WordPress user logon available in SQL from session variable @wpda_wp_user_login :

add_action(
	'wpda_dbinit',
	function( $wpdadb ) {
		if ( null !== $wpdadb ) {
			$suppress_errors = $wpdadb->suppress_errors( true );
			$current_user    = wp_get_current_user();
			$wpdadb->query( 'set @wpda_wp_user_login = "' . $current_user->user_login . '"' );
			$wpdadb->suppress_errors( $suppress_errors );
		}
	},
	10,
	1
);

This code can be download from the Code Manager website:
> Add WP user login to SQL session

Peter Schulz is a lecturer in information technology at the HAN university of applied sciences. He is an experienced programmer with more than 25 years experience in Oracle databases and tools. Peter is the developer of WordPress plugin WP Data Access.

Leave a Reply

Your email address will not be published.