• Resolved Shaazaam

    (@shaazaam)


    So I have been reading that for proper shortcode appearance (as in having shortcode appear before anything else on a page/post) you should put HTML elements in a string variable and return that variable. My issue complicates that a little. I have some PHP driven elements, mostly some conditional statements and variables.

    My PHP knowledge is not the best out there, but I am under the impression that I can’t store that PHP as a string and simply return it and expect it to work.

    Can anyone maybe shed some light on this issue?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Yes, you can. There’s various ways of doing it, so there’s going to be at least one that will do what you want it to. Without more information of what you’re actually doing, and why you think it won’t work for you, that’s all that we can say at this point.

    Thread Starter Shaazaam

    (@shaazaam)

    <div class='deerPedigree'><!-- This is the displayed content-->
    	  <table width='100%' border='0' cellpadding='0' cellspacing='0'>
                <tr>
                  <td width='99%' align='center'>
                    <table width='100%' border='0' cellspacing='0' cellpadding='5'>
    	          <tr>
    	            <td width='50%' align='center'>
    	              <a>'>
    	                <img src='<? echo plugins_url('images/large_buttons/buck_pedigrees_off.png', __FILE__); ?>' alt='Buck Pedigrees' name='buckpedigrees' width='350' height='80' border='0' id='buckpedigrees' />
    		      </a>
    		     </td>
    		     <td width='50%' align='center'>
    		      <a>'>
    			<img src='<? echo plugins_url('images/large_buttons/doe_pedigrees_off.png', __FILE__); ?>' alt='Doe Pedigrees' name='doepedigrees' width='350' height='80' border='0' id='doepedigrees' />
    			</a>
    		       </td>
    		      </tr>
    		     </table>
    		   </td>
                      </tr>
    	          <tr>
    		   <td align='right'>
    		     <table border='0' cellspacing='0' cellpadding='0' class='deerName'>
    		      <tr>
    			<td align='left' class='pedigreegreyboxbax'>
                             <div class='greyBoxEnd'>
                              <img src='<? echo plugins_url('images/grey_nav_left.png', __FILE__); ?>' width='18' height='34' />
                              <!--end .geyBoxEnd'--></div>
                              <div class='greyBoxMain'>
    			    <h1>Pedigree Sheet | <span class='pedigreename'><? echo $oDeer->DeerName; ?></span></h1>
                               <!--end .greyBoxMain--></div>
    			  </td>
    			 </tr>
    		       </table>
    		      </td>
    		     </tr>
    		     <tr>
    		     <td valign='top' colspan='2'>
    		      <? if (file_exists($pedigree_directory . 'pedigrees/photos/' . $oDeer->Deer_ID . '-1.png')) {
    			  $deerImg = (plugins_url('pedigrees/photos/', __FILE__) . $oDeer->Deer_ID . '-1.png');
    			   echo "<img class='deerListThumb' src='". $deerImg ."' width='300' height='300' alt='". $oDeer->DeerName ."' title='". $oDeer->DeerName ."'/>";
    		           } else {
    			     echo "<img class='deerListThumb' src=". plugins_url('pedigrees/photos/placeholder.png', __FILE__) ." width='300' height='300' alt='". $oDeer->DeerName ."' title'". $oDeer->DeerName ."'/>";
    			    }
    			    echo "<div style='float: right;'>";
    			     echo $oDeer->getAncestorTreeHTML();
    			    echo "</div>";
    			    echo "<div style='clear: both;'></div>";
    			     echo "<ul class='videos'>";
    			      if (file_exists($pedigree_directory . 'pedigrees/videos/' . $oDeer->Deer_ID . '-1.mov')) {
    				echo "
    <li>";
    			        echo "<a>Deer_ID ."&n=1'>View Video</a>";
    				echo "</li>
    ";
    				}
    			      if (file_exists($pedigree_directory . 'pedigrees/videos/' . $oDeer->Deer_ID . '-2.mov')) {
    				echo "
    <li>";
    				echo "<a>Deer_ID ."&n=2'>View Video</a>";
    				echo "</li>
    ";
    				}
    			     echo "";
    			     if (count($aDescriptionItems)) {
    			       echo "<ul class='notes'>";
    			    foreach($aDescriptionItems as $sListItem) {
    			     echo "
    <li>";
    			     echo $sListItem;
    			     echo "</li>
    ";
    			     }
    			   echo "";
    			  } ?>
    		        </td>
    		     </tr>
    		</table>
    	</div> <? //End of displayed content
    }//End of shortcode

    There are a few things above that, but it’s just some queries and stuff, nothing that will be displayed. I suppose a better practice would probably be not using echo at all in any of my conditional statements.

    I suppose a better practice would probably be not using echo at all in any of my conditional statements

    EXACTLY.

    Better yet is to not use echo at all in the functions you have composing your shortcode content. Just concatinate the whole thing with . and .= operators.

    So a typical use would be like:`

    <?php
     function example() {
     ret = "<div class='deerPedigree'><!-- This is the displayed content-->
    	  <table width='100%' border='0' cellpadding='0' cellspacing='0'>
                <tr>
                  <td width='99%' align='center'>
                    <table width='100%' border='0' cellspacing='0' cellpadding='5'>
    	          <tr>
    	            <td width='50%' align='center'>
    	              <a>'>
    	                <img src='";
     ret .= plugins_url('images/large_buttons/buck_pedigrees_off.png', __FILE__);
     ret .= "' alt='Buck Pedigrees' name='buckpedigrees' width='350' height='80' border='0' id='buckpedigrees' />
    		      </a>
    		     </td>";
    
    //some code deleted here
    
     ret .=          "</tr>";
    
    //some code deleted here
    
     ret .=		 "<td valign='top' colspan='2'>";
    	if (file_exists($pedigree_directory . 'pedigrees/photos/' . $oDeer->Deer_ID . '-1.png')) {
    		$deerImg = (plugins_url('pedigrees/photos/', __FILE__) . $oDeer->Deer_ID . '-1.png');
    		ret .= "<img class='deerListThumb' src='". $deerImg ."' width='300' height='300' alt='". $oDeer->DeerName ."' title='". $oDeer->DeerName ."'/>";
    	} else {
    		ret .= "<img class='deerListThumb' src=". plugins_url('pedigrees/photos/placeholder.png', __FILE__) ." width='300' height='300' alt='". $oDeer->DeerName ."' title'". $oDeer->DeerName ."'/>";
    	}
     ret .=	"<div style='float: right;'>"
    	. $oDeer->getAncestorTreeHTML()
    	. "</div>";
    
    //some code deleted here
    
     ret .= '</table>
    	</div>'; //End of displayed content
    
     return ret;
    
    }//End of shortcode

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been damaged by the forum’s parser.]

    Thread Starter Shaazaam

    (@shaazaam)

    I edited my plugin to just dip in and out of PHP without echos. I also found just by doing this:

    ob_start();
    //WHATEVER DISPLAYED CONTENT HERE
    return ob_get_clean();

    Will solve the whole formatting shortcode output above any other content issue. Though I suppose the above solution you provided RossMitchell would would just as well.

    Thanks you both for the help.

    Many thanks, I didn’t know about ob_start etc

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Return Instead of Echo’ is closed to new replies.