Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nextab/19a58f80e2ee63da3b5eabea689780f8 to your computer and use it in GitHub Desktop.

Select an option

Save nextab/19a58f80e2ee63da3b5eabea689780f8 to your computer and use it in GitHub Desktop.
/* Key-Name für E-Mail wäre "user_email" => entspricht den Bezeichnern des Standard WP User Objects
* user_email
* user_login
* user_nicename
* user_firstname
* user_lastname
* user_description
*/
#region Add new Macro Option for JetEngine
/**
* Note!
* Register macros on jet-engine/register-macros action only,
* as the base macro class \Jet_Engine_Base_Macros is not available before that action;
* after it - all macros are registered already
*/
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_prop';
}
/**
* Macros name in UI
*/
public function macros_name() {
return 'Current user property';
}
/**
* 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(
'prop_key' => array(
'label' => 'Property',
'type' => 'text',
'default' => '',
),
);
}
/**
* Macros callback - gets existing property from the current logged in user
*/
public function macros_callback( $args = array() ) {
$prop_key = ! empty( $args['prop_key'] ) ? $args['prop_key'] : null;
$object = wp_get_current_user();
if ( $object && 'WP_User' === get_class( $object ) ) {
$user = $object;
}
if ( ! $user || ! isset( $user->$prop_key ) ) {
return 'prop not found';
}
return $user->$prop_key;
}
}
/**
* 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();
} );
#endregion Add new Macro Option for JetEngine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment