Announcement

Collapse
No announcement yet.

PHP/Coding Help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

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

  • #2
    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, 22:10.

    Comment


    • #3
      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

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

      Comment


      • #4
        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>

        Comment


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

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

          Comment


          • #6
            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?

            Comment


            • #7
              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

              Comment


              • #8
                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

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

                Comment

                Working...
                X