Skip to content

Instantly share code, notes, and snippets.

@allysonsouza
Last active January 24, 2023 19:27
Show Gist options
  • Save allysonsouza/9a55cbd02d0a1035ba03184d4e1af783 to your computer and use it in GitHub Desktop.
Save allysonsouza/9a55cbd02d0a1035ba03184d4e1af783 to your computer and use it in GitHub Desktop.
JetEngine Macro - Current user email
<?php
/*
Plugin Name: JetEngine Macro - Current user email
Plugin URI:
Description:
Author: Allyson Souza
Version: 1.0.0
Author URI: https://www.allysonsouza.com.br
*/
add_action( 'jet-engine/register-macros', function(){
/**
* Current_User_Prop class.
* Adds macro, that returns given property of the current user.
*
* Has methods (all JetEngine macros classes should have those):
* macros_tag() - sets macros tag
* macros_name() - sets human-readable macros name for UI
* macros_callback() - function that returns needed value
* macros_args() - optional argument list for the macro; argument format is the same as in Elementor
* https://developers.elementor.com/docs/controls/regular-control/
* In this example, the macro has one control: 'prop_key'
* which is the property, that should be get from the current user
*
*/
class Current_User_Prop extends \Jet_Engine_Base_Macros {
/**
* Macros tag - this macro will look like %current_user_prop|ID% if typed manually
*/
public function macros_tag() {
return 'current_user_email';
}
/**
* Macros name in UI
*/
public function macros_name() {
return 'Current user email';
}
/**
* Macros arguments - see this https://developers.elementor.com/docs/controls/regular-control/ for reference
* An empty array may be returned if the macro has no arguments
*/
public function macros_args() {
return array();
}
/**
* Macros callback - gets existing property from the current logged in user
*/
public function macros_callback( $args = array() ) {
$user = wp_get_current_user();
return $user->user_email ?? 'Email not found.';
}
}
/**
* Create an instance of Current_User_Prop to add current_user_prop macro
* of course you may create a separate file with the class, include it instead of declaring the class right in the action,
* and then create an instance of it
*/
new Current_User_Prop();
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment