I'm using a plugin that will display content based on user location, and I'm trying to add it to a facebook widget code. The plugin is using a shortcode to wrap content that will be displayed, and here is what I have so far. I know I'm doing something wrong, and would really appreciate it if you can point me to the right direction.

if (($user_id) and ($app_id) and ($secret_code)){
    $user_data = get_option('wt_recent_fb_posts_user');

    echo do_shortcode ('[wpgc_is_city city="Morrisville"]');

    if ((!$user_data) OR ($user_data[0]->id !== $user_id)){
        $user_data = $this -> fetch_userdata($user_id);             

    $fb_posts = get_transient('wt_recent_fb_posts');                
    if (!$fb_posts){ //no messages, fetch from facebook             
        $fb_posts = $this->fetch_fbposts($user_id, $post_count, $app_id, $secret_code);             
    } else {
        $fb_posts = get_transient( 'wt_recent_fb_posts' );                  

        echo "<div class='user'><h3><a href='".$user_data[0]->url."' target='_blank'>".$user_data[0]->name."</a></h3></div>";
    } else {
        echo "<div class='user'>Facebook</div>";

    echo "<div class='fb-posts-list'><ul class='slides'>";

    foreach ($fb_posts as $fb_post) {
        echo "<li>";
        echo "<div class='message'>".$fb_post->message."</div>";
        $created_time = $fb_post->created_time;
        $time_ago = sprintf(__('%s ago', 'wellthemes'), human_time_diff(strtotime($created_time))); 
        echo "<div class='time'>".$time_ago."</div>";                                   
        echo "</li>";
    echo "</ul></div><div class='fb-posts-nav'></div>"; 
    echo do_shortcode('[/wpgc_is_city]');
  • 2
    I'm sorry but you haven't said what the problem is? What are you asking? What is it supposed to do, and what does it do instead? – Tom J Nowell Aug 20 '13 at 14:30
  • Sorry I forgot.. You will see that I added echo do_shortcode ('[wpgc_is_city city="Morrisville"]'); and echo do_shortcode('[/wpgc_is_city]'); but the content is still showing even if I'm not from that city – Mac Mendoza Aug 20 '13 at 14:45
  • Oooh that's bad, your shortcode string should be a shortcode, not a fragment of a shortcode, do_shortcode is not psychic. Imagine, some other plugin could put a '[/yourshortcodehere]' at the very end of the page, and ruin everything if you could do that! – Tom J Nowell Aug 20 '13 at 16:41

From Codex Shortcode Examples

// In case there is opening and closing shortcode.
echo do_shortcode('[iscorrect]'.$text_to_be_wrapped_in_shortcode.'[/iscorrect]');

Basically, you can't use do_shortcode() two separate times for what is one shortcode consisting of a pair of tags.

In your code you need to save your output to variables instead of using echo. Then at the end wrap it in the shortcode like the example.


Thanks for all the responses.. I actually drop the [shortcode] stuff and just use the function of the shortcode it self.

something like this..

$location = shortcode_city($atts, $content='');
if ($user_loc1 == $location){
// Code here

Thanks again guys!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.