Support

Account

Home Forums General Issues Showdown content if current user..

Solved

Showdown content if current user..

  • Hi there! Thank you for this amazing plugin!

    I use the calendar plugin from http://tri.be/ on a worpress website for a private website (it’s like an intranet..).

    On this calendar I put all the planned meetings, but I would like that the current logged in user can see only the events he/she is supposed to join.

    I thought that I can create a custom field, named, for example, “mustjoin” and put there a list of registered usernames as values.
    Then do a check on the page.
    If the current logged in username is in that list, he/she can see the event on the calendar.. otherwise not.

    Something like :

     if ( $current_user->user_login =='get_field(mustjoin)') { ?>
    ...html...
    <?php } else { ?>
    ...html 2...<?php } ?>

    It doesn’t work. 🙂 Well.. no errors at least.. but no changes. Everybody can see the event.

    This is the part of the code I need to show IF the current user username is in the value list ‘mustjoin’

    <?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
    <?php tribe_get_template_part('month/single', 'event') ?>
    <?php endwhile; ?>

    Really thank you for your help.
    Gio.

  • 
    function searchForName($name, $array) {
       foreach ($array as $key => $val) {
           if ($val['nickname'] === $name) {
               return true;
           }
       }
       return null;
    }
    $current_user = wp_get_current_user();
    if(searchForName($current_user->user_login,get_field('YourACFUserField'))):
    	//CODE GOES HERE
    endif;
    

    something like that

  • Hi VoiD2008, thank you for your reply.

    Unfortunately it doesn’t work, and my php knowledge is not good enough to fix it.
    I should insert a control to check if the array exists or not I think..

    If I use this code, that works only with one username in that field,

    <?php  
    global $current_user;
    get_currentuserinfo();
    
    if ($current_user->user_login == 'get_field(mustjoin)') { ?>
    	<?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
    <?php tribe_get_template_part('month/single', 'event') ?>
    <?php endwhile; ?>
    		<?php } ?>

    Everything works.

    Of course, with two usernames in the field, it doesn’t. But I don’t receive an error.. it just works as it should, because it checks for an username made by the 1st+2nd usernames together.
    With a
    var_dump( get_field('mustjoin') ); I receive this :
    string(17) "User1 User2 User3"

    Thank you for your help.

  • I guess thats because you are using simple text field for your userlist. But my code was written for UserSelect ACF field
    That returns an array of user objects

  • But we can upgrade your code =))

    <?php  
    global $current_user;
    get_currentuserinfo();
    
    if (in_array(explode(' ',get_field('mustjoin')),$current_user->user_login)) { ?>
    	<?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
    <?php tribe_get_template_part('month/single', 'event') ?>
    <?php endwhile; ?>
    		<?php } ?>
  • Hi there! Thank for your suggestion, but it doesn’t work yet.

    If I use the code you wrote above, I receive an error, which tells me that the second parameter should be an array for the script..

    Because it is a string, I have ended up using this code :

    <?php $current_user = wp_get_current_user(); ?>
    <?php global $user_login;
          get_currentuserinfo(); ?>
    <?php $lookforuser = get_field('mustjoin') ?>
    <?php if (strpos($lookforuser, $user_login) !== false) { ?>
        <?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
        <?php tribe_get_template_part('month/single', 'event') ?>
        <?php endwhile; ?>
    <?php } ?>

    And it works at 99.5%!
    The problem is when I open the calendar page, I can’t see anything.. as soon as I manually refresh the page, all the events (where I am supposed to join) appear.
    It seems it needs to re-collect the infos..
    How can I fix it? (I tried with different computers/browsers, no changes..) Thank you.

  • can you post a link to your project?

  • Unfortunately it is for an intranet, and because is still underwork for the content I can’t..

    But if you want more information, I can run all the test you may need, and post the results here.

    Thank you

  • thats strange that you not see your events without refreshing
    try to dump your var

    global $user_login; var_dump($day['events']);

    or maybe you have some javascripts that works with you data? and you placed them in wrong place

  • If I use that dump, I receive an insane long reply from the page..
    Especially for the days where there is a meeting planned.
    Without refreshing of course.

    As soon as I refresh the page, I have the same dump + the event.
    that’s weird.. :/

    do you want me to post it ?

    Thank you.

  • No I don’t need that dump. I just was interested in information about the var is not NULL when you calling page first time =)
    Now try to dump your user field
    <?php $lookforuser = get_field('mustjoin'); var_dump($lookforuser);var_dump(strpos($lookforuser, $user_login));?>

  • and for checking if the error in next template
    add this
    <?php echo 'event exists<br/>';tribe_get_template_part('month/single', 'event') ?>

  • Ok then, I have added this now :

    <?php $lookforuser = get_field('mustjoin'); var_dump($lookforuser);var_dump(strpos($lookforuser, $user_login));?>

    And when I navigate to the page, I have this in ALL the days :
    bool(false) bool(false)

    As soon as I refresh :

    string(17) "User1 User2 User3" int(0)
    Even on the days where there are not meetings.

    Now I will apply your second post and will post the differences.

  • my second is not neccesary the bug is with get_field(‘mustjoin’);
    it returns nothing first time… let me think =)))

  • if I use also the second snippet, I have for ALL days this

    string(17) "User1 User2 User3" int(0) event exists + the events names.

    without refreshing.

    With refresh, same stuff + 2 times the events names on the day that the event is planned.

  • post plz all code from this template file… and it’s filename

  • Filename single-day.php

    <?php
    /**
     * Month View Single Day
     * This file contains one day in the month grid
     *
     * Override this template in your own theme by creating a file at [your-theme]/tribe-events/month/single-day.php
     *
     * @package TribeEventsCalendar
     *
     */
    
    if ( !defined('ABSPATH') ) { die('-1'); } ?>
    
    <?php
    $day = tribe_events_get_current_month_day();
    ?>
    
    <?php if ( $day['date'] != 'previous' && $day['date'] != 'next' ) : ?>
    
    	<!-- Day Header demon controller 2 -->
    	<div id="tribe-events-daynum-<?php echo $day['daynum'] ?>">
    
    		<?php if ( $day['total_events'] > 0 && tribe_events_is_view_enabled( 'day' ) ) { ?>
    			<a href="<?php echo tribe_get_day_link( $day['date'] ) ?>"><?php echo $day['daynum'] ?></a>
    		<?php } else { ?>
    			<?php echo $day['daynum'] ?>
    		<?php } ?>
    
    	</div>
    
    	<!-- EDITED - show this part only if the user must join Events List -->
    
    <?php $current_user = wp_get_current_user(); ?>
    <?php global $user_login;
          get_currentuserinfo(); ?>
    <?php $lookforuser = get_field('mustjoin'); var_dump($lookforuser);var_dump(strpos($lookforuser, $user_login)); ?>
    <?php echo 'event exists<br/>';tribe_get_template_part('month/single', 'event') ?>
    <?php if (strpos($lookforuser, $user_login) !== false) { ?>
        <?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
        <?php tribe_get_template_part('month/single', 'event') ?>
        <?php endwhile; ?>
    <?php } ?>
    
    	<!-- View More -->
    	<?php if ( $day['view_more'] && tribe_events_is_view_enabled( 'day' ) ) : ?>
    		<div class="tribe-events-viewmore">
    			<?php
    				if($day['total_events'] > 1 || $day['total_events'] == 0) {
    					$events_label = __( 'Events ', 'tribe-events-calendar' );
    				} else {
    					$events_label = __( 'Event ', 'tribe-events-calendar' );
    				}
    			?>
    			<a href="<?php echo $day['view_more'] ?>">View All <?php echo $day['total_events'] . " " . $events_label ?> &raquo;</a>
    		</div>
    	<?php endif ?>
    
    <?php endif; ?>
  • thats weird =)
    where is your field mustjoin?
    I mean in what post type?

  • Ahemm.. what do you mean exactly ?

    I have the ACF ‘mustjoin’ in the page events for the plugin (you can also download the calendar, it’s free), then I’ve put the snippet between
    <!– EDITED etc –> and <–view more–>

    Sorry I have understood wrongly your reply. 😐

  • so I guess you have to check if user must join in events loop
    not in calendar

    remove all my var_dump functions and change your code

    <?php $current_user = wp_get_current_user(); ?>
    <?php global $user_login;
          get_currentuserinfo(); ?>
    <?php $lookforuser = get_field('mustjoin'); ?>
    <?php if (strpos($lookforuser, $user_login) !== false) { ?>
        <?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
        <?php tribe_get_template_part('month/single', 'event') ?>
        <?php endwhile; ?>
    <?php } ?>

    to

    <?php $current_user = wp_get_current_user(); ?>
    <?php global $user_login;
          get_currentuserinfo(); ?>
    <?php while ($day['events']->have_posts()) : $day['events']->the_post(); ?>
       <?php $lookforuser = get_field('mustjoin'); ?>
       <?php if (strpos($lookforuser, $user_login) !== false) 
             tribe_get_template_part('month/single', 'event'); ?>
    <?php endwhile; ?>
    
  • If you ever come to London, I own you a nice lunch. 🙂

    It works!

    🙂

  • Nice =) Good luck =)

  • btw the line <?php $current_user = wp_get_current_user(); ?> is unnecessary

Viewing 23 posts - 1 through 23 (of 23 total)

The topic ‘Showdown content if current user..’ is closed to new replies.