Browse: Home / Snippets /

Certificate custom user field and shortcode

Contents


Snippet #

Problem:
We have states that have unique certificate requirements. For instance, they have an image of their state logo that they require on the certificate. Different states have different requirements. It would be awesome if Learndash provided PDF image selection logic by user location (State). So, when the certificate is generated Learndash selects the correct PDF for the students state.

Solution:
This can easily be added with logic outside of LeandDash using the Advanced Custom Fields plugin. In this solution I add a single selector field with ACF to be shown on the WP user profile. The user of admn can set this selector to the user’s state of origin.

https://monosnap.com/direct/Ub7j3C2mIXtbBaOOlCf8iC0MQVfR73
Here is the ACF editor for the Field Group and showing the custom field User State. Note the ‘Choices’ section where I’ve setup the internal key as the two character state abbreviations like ‘al’, ‘fl’, ‘tx’, etc.

https://monosnap.com/direct/7d1uykv9FWEmOZA9RM767kQMaPMEVU
When I view a WP profile I now see the new field group and custom field ‘User State’ and have made a selection then saved the profile.

So now comes the fun part. How to I use this custom field on a certificate? And better how to I translate user meta stored state abbreviation and use it for an image? The best solution will to use a shortcode. While ACF does provide support for custom shortcodes, we don’t really have a way to show an image based on the user meta setting.

This takes a few lines of code (sorry). The most direct way is to register our own shortcode [user_state] and to be a little extra, we will add support for some parameters to show:

[user_state] : By default will show the stored user value: ‘al’, ‘fl’, ‘tx’ for the state selector.
[user_state field=”label”] : Will return the state full name: Alabama, Florida, Texas for the state selector.
[user_state field=”image” width=”300″] : Will return an HTML element with a link to the image. We also have a second parameter to control the width.

Custom shortcode PHP code, add this to your theme’s functions.php file

add_shortcode( 'user_state', function( $atts = array(), $content = '' ) {
    if ( is_user_logged_in() ) {
 
        $atts = shortcode_atts(
            array(
                'field' => '',
                'width' => '100',
            ),
            $atts
        );
 
        $user_id = get_current_user_id();
 
        // Get the 2 character state value like 'al', 'fl', 'tx', etc.
        $user_state = get_field( 'user_state', 'user_' . $user_id );
        $user_state_label = '';
 
        if ( 'false' !== $user_state ) {
            /**
             * Validate the $user_state value by checking the choices for the field. This is
             * done in case the overall choices changes and we have to revert the user value
             * to some default value.
             */
            $user_state_field = get_field_object( 'user_state', 'user_' . $user_id );
            if ( ! isset( $user_state_field['choices'][ $user_state ] ) ) {
                $user_state = 'false';
            } else {
                $user_state_label = $user_state_field['choices'][ $user_state ];
            }
        }
 
        /**
         * Now work on the returned values.
         */
 
        switch ( $atts['field'] ) {
            case 'image':
                /**
                 * Assumed we have a directory off the site root containing all the US state images where the
                 * the image filenames are all in a pattern using the 2 character state code like:
                 * al - Alabama - https://www.site.com/images-state-logos/al.png
                 * fl - Florida - https://www.site.com/images-state-logos/fl.png
                 * tx - Texas   - https://www.site.com/images-state-logos/tx.png
                 *
                 * But these image coould be external or anywhere else within the site.
                 */
 
                if ( ( 'false' !== $user_state ) && ( file_exists( ABSPATH . '/images-state-logos/' . $user_state . '.png' ) ) ) {
 
                    // If the $user_state is not false and the state image exists then show it.
                    $content .= '<img width="' . $atts['width'] . '" src="/images-state-logos/' . $user_state . '.png" alt="' . $user_state_label . '" />';
                } else {
 
                    // If the user state is false or the state image does not exist we show a default logo.
                    $content .= '<img width="' . $atts['width'] . '" src="/images-state-logos/default.png" alt="' . $user_state_label . '" />';
                }
                break;
 
            case 'label':
                // We want to return the state full name.
                $content .= $user_state_label;
                break;
 
            case '':
            default:
                // By default we want to return the state aabbreviation.
                if ( 'false' !== $user_state ) {
                    $content .= $user_state;
                }
                break;
        }
    }
 
    return $content;
 
}, 30, 2 );

Once the PHP code is added to your theme functions.php file you can then use the shortcodes on a certificate (or really anywhere)

User State Value : [user_state]
User State Label : [user_state field=”label”]
User State Image : [user_state field=”image” width=”300″]