1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how would I write this function correctly

Discussion in 'PHP' started by caligrafx, Feb 27, 2015.

  1. #1
    I am looking to write this "if user is logged in" function but need some help putting the two together for wordpress. Any help would be greatly appreciated!

    <?php
    if ( is_user_logged_in() ) {
        echo 'Welcome, registered user!';
    } else {
        echo 'Welcome, visitor!';
    }
    ?>
    PHP:
    <?php
    
                                /*
                                *  Show selected file if value exists
                                *  Return value = URL
                                */
    
                                if( get_field('upload_file') ):
                                    ?><a href="<?php the_field('upload_file'); ?>" ><button class="button color large blue">Download</button></a><?php
                                endif;
    
                                ?>
    PHP:
     
    caligrafx, Feb 27, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Uhm, what? You only want the download file button to be visible for logged in users, or...? You need to explain a bit better, man
     
    PoPSiCLe, Feb 27, 2015 IP
  3. caligrafx

    caligrafx Active Member

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    83
    #3
    Sorry, yes I am looking to hind the download link from non registered members. Only show the link once one is signed in. The download link is the function link to the download link. Thanks!
     
    caligrafx, Feb 28, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Then just place the second code you pasted within the the if-statement in the first bit of code (not the <?php bit, just the rest)
     
    PoPSiCLe, Feb 28, 2015 IP
  5. caligrafx

    caligrafx Active Member

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    83
    #5
    Thanks this is what I ended up using that works.

    <?php
    if ( is_user_logged_in() ) {
        if( get_field('upload_file') ):
        ?><a href="<?php the_field('upload_file'); ?>" ><button class="button color large blue">Download</button></a><?php
    endif;
    }
    ?>
    PHP:
     
    caligrafx, Mar 1, 2015 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Some advice: don't open and close PHP willy-nilly like that, makes the parser work harder for nothing... why are you putting a form element like BUTTON inside an anchor and throwing so many PRESENTATIONAL classes at it?

    <?php
    
    if (is_user_logged_in() && get_field('upload_file')) echo '
    		<a href="', the_field('upload_file'), '" class="downloadButton">Download</a>';
    		
    ?>
    Code (markup):
     
    deathshadow, Mar 4, 2015 IP