Matto Matto - 3 years ago 43
PHP Question

Wrap in a shortcode a function and then return and echo the result in my editor on my wordpress website

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?

Answer Source

Try this instead:

function mymembershortcode() {
    $members = [];
    $query = new WP_Query( array(
        'cat' => 2,
        'posts_per_page' => 20,
    )); 

    while ($query->have_posts()) : $query->the_post();
        $membername = get_post_meta( get_the_ID(), 'name', true );
        $memberemail = get_post_meta( get_the_ID(), 'email', true );
        if ( ! empty( $membername ) ) {
            $members[] = '<option value="' . $memberemail . '">' . get_post_meta(get_the_ID(), 'name', true) .'</option>';
        }

    endwhile;
    wp_reset_postdata(); 
    return implode($members);

}

add_shortcode('mymemberselector', 'mymembershortcode');

Here's some info about why you should NEVER use query_posts() https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download