Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active June 22, 2024 20:42
Show Gist options
  • Save Crocoblock/bf901715f1ca5c8dca58e4f4d8db9440 to your computer and use it in GitHub Desktop.
Save Crocoblock/bf901715f1ca5c8dca58e4f4d8db9440 to your computer and use it in GitHub Desktop.
Add custom macro for JetEngine. Example - get the current user property, such as ID, user_email, etc.
<?php
/**
* 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();
} );
@Emilracerbil
Copy link

I tried to use a macro to retrieve the lastest featured post ID so I could exclude it in other queries on the blog page. I cannot get it to work though. Here is my code for the macros_callback. The macros_args just returns an empty array.

` public function macros_callback( $args = array() ) {

        $myargs = array(
            'posts_per_page' => 1,
            'post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'featured-post-tax',
                    'field'    => 'slug',
                    'terms'    => 'featured-post-slug',
                    ),
                ),
            );

        $my_query = new WP_Query( $myargs );
        while ($my_query->have_posts()) : $my_query->the_post();
            $exclude_post_id = get_the_ID();
        endwhile;

        wp_reset_query();

        return $exclude_post_id;

    }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment