Results 1 to 8 of 8

Thread: PHP/Coding Help

  1. #1
    Boob Inspector M4T VW's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    9,584
    Thanks
    516
    Thanked 640 Times in 472 Posts
    Rep Power
    9

    Default PHP/Coding Help

    Trying to write a small wordpress plugin to take the username from xenforo session cookie and display it on the wrodpress site using a shortcode. I'm very close but my lack of PHP has hit me. This is my current code:

    PHP Code:
    include_once( plugin_dir_path( __FILE__ ) . '/includes/xf_connector.php' );
    $xf_userId = $visitor['user_id'];
    $xf_username = $visitor['username']; 

    function xenfield_shortcode() {
      ob_start(); ?> 
      <div class="xenfield">
        <p>echo $xf_username;<p>
      </div>
      <?php    return ob_get_clean();
    }
    add_shortcode'xenfield''xenfield_shortcode' );
    That currently returns just "echo $xf_username;" in plain text and not the value. I cannot seem to get it to display the value for some reason.

    The following code works on a blank page so i know if sort of works.

    PHP Code:
    <p>Hello <?php $xf_userId $visitor['user_id'];
    $xf_username $visitor['username'];
    echo 
    "$xf_username?> welcome to this web page.</p>
    Any help?

    Thanks
    Mat

    Quote Originally Posted by Aria View Post
    On reflection though, as I am taking a bath, listening to "Rain of blessing-vajra chant" while typing on the iPad

  2. #2
    🙈 🙉 🙊 heavywater's Avatar
    Join Date
    Feb 2010
    Posts
    7,152
    Thanks
    770
    Thanked 2,698 Times in 1,542 Posts
    Rep Power
    9

    Default

    Just wrap the echo in php tags, it's currently outside your blocks so gets treated as HTML.

    EDIT- assuming that's not the whole code obviously as you'd need opening/closing tags on the rest.
    Last edited by heavywater; 20-02-17 at 22:10.

  3. The Following User Says Thank You to heavywater For This Useful Post:


  4. #3
    Boob Inspector M4T VW's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    9,584
    Thanks
    516
    Thanked 640 Times in 472 Posts
    Rep Power
    9

    Default

    Like this?

    PHP Code:
    include_once( plugin_dir_path( __FILE__ ) . '/includes/xf_connector.php' );

    function xenfield_shortcode() {
      ob_start(); ?> 
      <div class="xenfield">
    <?php $xf_userId $visitor['user_id'];
    $xf_username $visitor['username'];
    echo 
    "$xf_username?>
      </div>
      <?php    return ob_get_clean();
    }
    add_shortcode'xenfield''xenfield_shortcode' );
    This actually returns nothing

    Quote Originally Posted by Aria View Post
    On reflection though, as I am taking a bath, listening to "Rain of blessing-vajra chant" while typing on the iPad

  5. #4
    🙈 🙉 🙊 heavywater's Avatar
    Join Date
    Feb 2010
    Posts
    7,152
    Thanks
    770
    Thanked 2,698 Times in 1,542 Posts
    Rep Power
    9

    Default

    Sorry, not very clear. You'd just need to wrap the php in tags. You could echo the html as well but in this case it's not really necessary.

    So for your above extract your HTML section should just be:

    PHP Code:
    <div class="xenfield">
      <p><?php echo $xf_username?><p>
    </div>

  6. The Following User Says Thank You to heavywater For This Useful Post:


  7. #5
    Boob Inspector M4T VW's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    9,584
    Thanks
    516
    Thanked 640 Times in 472 Posts
    Rep Power
    9

    Default

    Sadly not, That also displays nothing. Looking at the HTML it shows empty <p> </p> tags

    Quote Originally Posted by Aria View Post
    On reflection though, as I am taking a bath, listening to "Rain of blessing-vajra chant" while typing on the iPad

  8. #6
    Rocketeer Toonshorty's Avatar
    Join Date
    Mar 2010
    Location
    Newcastle upon Tyne
    Posts
    7,041
    Thanks
    152
    Thanked 929 Times in 689 Posts
    Rep Power
    3

    Default

    Try hard-coding the variable, that would at least confirm whether the problem is with the output. It could just be that $visitor['username'] is actually just an empty string?

  9. #7
    Military Grade Tester DoubleTop's Avatar
    Join Date
    Nov 2009
    Location
    In a lamp
    Posts
    12,818
    Thanks
    135
    Thanked 2,241 Times in 1,421 Posts
    Rep Power
    10

    Default

    PHP Code:

    include_once( plugin_dir_path( __FILE__ ) . '/includes/xf_connector.php' ); 

    function xenfield_shortcode() {
      global $visitor;
      ob_start(); 
    ?>  
      <div class="xenfield"> 
    <?php 
        $xf_userId 
    $visitor['user_id']; 
        
    $xf_username $visitor['username']; 
        echo 
    $xf_username;
    ?> 
      </div> 
    <?php
      
    return ob_get_clean(); 

    add_shortcode'xenfield''xenfield_shortcode' );
    You might not need the global, and you can shorten the declarations a bit if you wanted to

    PHP Code:

    include_once( plugin_dir_path( __FILE__ ) . '/includes/xf_connector.php' ); 

    function xenfield_shortcode() {
      ob_start(); 
    ?>  
      <div class="xenfield"> 
    <?php echo $visitor['username']; ?>
      </div> 
    <?php
      
    return ob_get_clean(); 

    add_shortcode'xenfield''xenfield_shortcode' );
    You have a bit of a combo of errors, things enclosed in "" are a string, yet you seem to want to output the variable content, along with a missing ; which will cause a syntax error and be logged somewhere depending on the server setup.

    A little bit of guesswork though without having the entire xf_connector code or any idea how that is formatted.

    DT.
    Please see thread, here for how post reports are dealt with.
    Forum Guidelines here

  10. #8
    Boob Inspector M4T VW's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    9,584
    Thanks
    516
    Thanked 640 Times in 472 Posts
    Rep Power
    9

    Default

    Thanks guys, managed to get it working and will post the code tonight. Someone wrote it for me and it looks completely different to how i expected it

    Quote Originally Posted by Aria View Post
    On reflection though, as I am taking a bath, listening to "Rain of blessing-vajra chant" while typing on the iPad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Watch Game of Thrones Online Game of Thrones S07E01 Streaming GoT Season 7 Streaming Game of Thrones Season 7 Stream game of thrones season 7 episode 1 stream got season 7 streaming game of thrones episodes download game of thrones season 7 spoilers game of thrones season 7 predictions viral viral news viral stories viral news today usa news usa news today dog meat ban yulin festival Watch Game of Thrones Online Game of Thrones S07E01 Streaming GoT Season 7 Streaming Game of Thrones Season 7 Stream game of thrones season 7 episode 1 stream got season 7 streaming game of thrones episodes download