Here's my problem:
Two weeks ago I created a nice function to populate my options in a select element for a wordpress website. My purpose was to add all the names of my members with their emails as values from some posts that I create under the category "Member", so, whenever I edit, add or delete any member in my website, that section will be automatically updated.
Something like:
<select>
<option value="myemail@test.com">First user</option>
<option value="myemail2@test.com">Second user</option>
<option value="myemail3@test.com">Third user</option>
</select>
To achieve my goal I modify the loop in this way:
$members = [];
$temp_query = $wp_query;
query_posts("cat=2&showposts=20");
while (have_posts()) : the_post();
$membername = get_post_meta( $post->ID, 'name', true );
$memberemail = get_post_meta( $post->ID, 'email', true );
if ( ! empty( $membername ) ) {
$members[] = '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
}
endwhile;
and then I call the options in my selector like that:
<select class="drops" name="smallsan">
<?php echo implode($members); ?>
</select>
So far so good except for the fact that I wrap this code in my template.
My problem is that I need to run a plugin in order to hide the page from people who are not subscribed so I have to add the html in the loop in the editor and I can't execute php there.
So I tried to wrap the code in my function.php to associate it with a shortcode like that:
function mymembershortcode() {
$members = [];
$temp_query = $wp_query;
query_posts("cat=2&showposts=20");
while (have_posts()) : the_post();
$membername = get_post_meta( $post->ID, 'name', true );
$memberemail = get_post_meta( $post->ID, 'email', true );
if ( ! empty( $membername ) ) {
$members[] = '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
}
endwhile;
return implode($members);
}
add_shortcode('mymemberselector', 'mymembershortcode');
To run the shortcode:
<select class="drops" name="smallsan">
[mymemberselector]
</select>
essentially whenever I need but, it doesn't work, and I suspect it is because I can't return and echo at the same time.
Does anyone know how can I solve my problem?