0

I am using WordPress and the Gravity Forms plugin. I am trying to print/run a report of users based on meta key, meta value and role selections, using a simple Gravity Form to choose those selections.

Gravity Forms uses merge tags to display certain information (ie. {report_type:3}), so I tried just putting the merge tags as parameters in a shortcode I created, but it didn't work. See below:

/**
 * Shortcode for Listing Users from Meta Data
 * Usage: [list_users key="{report_type:3}" value="{which_report:1}" userrole="{which_state:2}"]
 */
function users_from_meta($atts) {
    $atts = shortcode_atts( array(
        'key'   => '',
        'value' => '',
        'userrole'  => '',
    ), $atts );
    $user_query = new WP_User_Query( array( 
        'meta_key' => $atts['key'],
        'meta_value' => $atts['value'],
        'role__in'    => wp_parse_list( $atts['userrole'] ),
    ) );
    $users = $user_query->get_results();
    if (!empty($users)) {
        $results = '<table class=list_user_table>
                <tr>
                    <td class=list_user_title1>Shop #</td>
                    <td class=list_user_title2>City</td>
                    <td class=list_user_title3>State</td>
                    <td class=list_user_title4>Franchisee</td>
                    <td class=list_user_title5>Consultant</td>
                </tr>';
        foreach ($users as $user){
            $results .= '<tr>
                    <td class=list_user_td_left><a href=https://clearstone.gogrw.com/user/shop' . $user->Store_Number . '/>' . $user->Store_Number . '</a></td>
                    <td class=list_user_td>' . $user->City . '</td>
                    <td class=list_user_td>' . $user->State . '</td>
                    <td class=list_user_td>' . $user->display_name . '</td>
                    <td class=list_user_td>' . $user->Group . '</td>
                </tr>';
        }
        $results .= '</table>';
    } else {
        $results =  'No users found';
    }
    wp_reset_postdata();
    return $results;
}
add_shortcode( 'list_users', 'users_from_meta' );

I also tried to call the information with the merge tags through the shortcode without parameters, which also did not work:

/**
 * Shortcode for Listing Users from Meta Data on PDF REPORT
 * Usage: [list_users_pdf]
 */
function pdf_users_from_meta() {
    $user_query = new WP_User_Query( array( 
        'meta_key' => '{report_type:3}',
        'meta_value' => '{which_report:1}',
        'role__in'    => wp_parse_list( '{which_state:2:value}' ),
    ) );
    $users = $user_query->get_results();
    if (!empty($users)) {
        $results = '<table class=list_user_table>
        <tr>
            <td class=list_user_title1>Shop #</td>
            <td class=list_user_title2>City</td>
            <td class=list_user_title3>State</td>
            <td class=list_user_title4>Franchisee</td>
            <td class=list_user_title5>Consultant</td>
        </tr>';
        foreach ($users as $user){
            $results .= '<tr>
            <td class=list_user_td_left>' . $user->Store_Number . '</a></td>
            <td class=list_user_td>' . $user->City . '</td>
            <td class=list_user_td>' . $user->State . '</td>
            <td class=list_user_td>' . $user->display_name . '</td>
            <td class=list_user_td>' . $user->Group . '</td>
        </tr>';
        }
        $results .= '</table>';
    } else {
        $results =  'No users found';
    }
    wp_reset_postdata();
    return $results;
}
add_shortcode( 'list_users_pdf', 'pdf_users_from_meta' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.