1

I'm building an intranet where I want users to be able to email other groups of users. I've successfully created a function that returns a list of user emails depending on their meta data (which are passed as arguments).

But I need help creating shortcodes. For example, a shortcode that outputs a mailing-list of all the users that have blue as a favorite color (data from a user registration form).

I've tried the simple add_shortcode('$tag , $func') but since I need to pass arguments that doesn't work, that will just list ALL the user emails and not depending on their meta data.

Anyone that can help me?

PS. I've just started to code so please be nice to me :)

function get_user_by_meta_data ($meta_key, $meta_value) {

    $args = array(
        'meta_key'  => $meta_key,
        'meta_value'  => $meta_value
    );

    // The Query
    $user_query = new WP_User_Query( $args );

    // The Results
    $users = $user_query->get_results();
    $result = array();

    // User Loop
    foreach ( $users as $user ) {
        $result[] = $user->user_email;
    }

    return "<span>" . implode( ', ', $result ) . "</span>";

}

1 Answer 1

1

You can adapt that code so it works with a shortcode. Here's an example:

/**
 * Register the [mailing_list] shortcode.
 *
 * Usage: [mailing_list meta_key='color' meta_value='blue']
 *
 * @author @cabrerahector
 * @param array $atts Shortcode parameters
 * @return string
 */
function wp44162_mailing_list_shortcode( $atts ) {
    $args = shortcode_atts(array( 
        'meta_key' => '',
        'meta_value' => '',
    ), $atts );

    // The Query
    $user_query = new WP_User_Query( $args );

    // The Results
    $users = $user_query->get_results();
    $result = array();

    // We have some users that match the conditions
    if ( ! empty($users) ) {
        // User Loop
        foreach ( $users as $user ) {
            $result[] = $user->user_email;
        }

        //return "<span>" . implode( ', ', $result ) . "</span>";
        return "<a href=\"mailto:" . implode(',', $result) . "\">" . $args['meta_value'] . "</a>";
    }

    // No users that matched the criteria were found
    return 'No users found.';
}
add_shortcode( 'mailing_list', 'wp44162_mailing_list_shortcode');

Update:

If you want to query for multiple colors, then you'll need to store every color selected by the user separatedly (see add_user_meta() for more details). Doing so will allow you to do this:

$args = shortcode_atts(array( 
    'meta_key' => '',
    'meta_value' => '',
), $atts );

$query = array(
    array(
        'key'     => $args['meta_key'],
        'value'   => explode(',', $args['meta_value']), // array of colors
        'compare' => 'IN'
    )
);

// The Query
$user_query = new WP_User_Query( $query );
Sign up to request clarification or add additional context in comments.

15 Comments

Ok so I need your help again @cabrerahector :) I changed the code as you suggested. Now I want to return a link that uses the meta_value as the link text. This is what I've come up with: return "<a href=\"mailto:".implode(',', $result)."\">".$meta_value."</a>"; But the $meta_value variable doesn't work, it just outputs meta_value. Any suggestions?
Hey @lindag., don't mention it. Glad I could help! So, you want to return a list of clickable email addresses?
I want to be able to insert a shortcode on a page like the one you had as an example: [mailing_list meta_key='color' meta_value='blue'] and I want the output to be a link that holds a list of email adresses and I want the link text to be the meta_value ("Blue" in this example). So the only thing that shows on the front end is a mailto-link that says Blue which contains all the emails for the users that have Blue as the meta_value.
You were close. $meta_value doesn't exist inside the function but you can access its value with $args['meta_value'] (remember that values passed to the shortcode via parameters are stored in the $args array). See my updated answer, I also made a minor improvement there.
You are my hero!! So thankful for this as the newbie I am :p
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.