SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Syntax for echoing html and PHP

    I need to add some PHP and HTML to a action hook in my WordPress site
    I can not work out how to add the php so that Sub total text is translatable _e('Sub-total', 'wpsc')
    That line throws a syntax error in my editor
    PHP Code:
    add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');

    function add_my_totals() {
        $my_new_totals = '<table  class="wpsc_checkout_table table-4">
        <tr class="total_price">
       <td  class="wpsc_totals">
       <?php _e('Sub-total''wpsc'); ?>:
       </td>
       <td  class="wpsc_totals">
       <?php echo wpsc_cart_total_widgetfalsefalse ,false ); ?>
       </td>
       </tr>
    </table>';

       echo "$my_new_totals";
       }

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Since it is part of a string, you will want to concatenate the function to your text.

    PHP Code:
    add_action('wpsc_before_form_of_shopping_cart''add_my_totals');

    function 
    add_my_totals() {
        
    $my_new_totals '<table  class="wpsc_checkout_table table-4">
        <tr class="total_price">
       <td  class="wpsc_totals">' 
    .
       
    _e('Sub-total''wpsc') .
       
    '</td>
       <td  class="wpsc_totals">' 
    .
       
    wpsc_cart_total_widgetfalsefalse ,false ) .
       
    '</td>
       </tr>
    </table>'
    ;

       echo 
    "$my_new_totals";
       } 
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The alternative would be to exit out of PHO for the HTML instead of addigning it to a variable and then echoing it:

    Code:
    add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');
    
    function add_my_totals() {
        ?><table  class="wpsc_checkout_table table-4">
        <tr class="total_price">
       <td  class="wpsc_totals">
       <?php _e('Sub-total', 'wpsc'); ?>:
       </td>
       <td  class="wpsc_totals">
       <?php echo wpsc_cart_total_widget( false, false ,false ); ?>
       </td>
       </tr>
    </table><?php
       }
    Another alternative would be to use cpradio's code but replace the $my_new_totals = with echo and drop the echo "$my_new_totals";
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both of you! Very helpful. I could use a little clarification. The use of a period when the code is being echoed as a string by being in single quotes, switch between HTML and PHP. Is that a fair description?
    I think that felgall's version is clearer to read fro a novice like me. Any reason one is better than the other?
    I guess cpradio's version does not require me to go in and out of php tags within my functions file, I can just open one for the whole file

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I am really confused. It seemed to me I applied the method properly in this code
    PHP Code:
    add_action('wpsc_before_form_of_shopping_cart''add_my_totals');

    function 
    add_my_totals() {
        
    $my_new_totals '<table  class="wpsc_checkout_table table-10">
     <tr class="total_price my-totals">
          <td class="wpsc_totals my-totals-pad">' 
    .
          
    _e('Total Price including Shipping and Tax:''wpsc') .
          
    '</td>
          <td class="wpsc_totals">
             <span id="checkout_total" class="pricedisplay checkout-total">' 
    wpsc_cart_total(); '</span>
          </td>
       </tr>
       </table>'
    ;
       echo 
    $my_new_totals;
       } 
    But the resulting HTML from the page source is this with the text outside the table completely!
    HTML Code:
    Total Price including Shipping and Tax:
    <table class="wpsc_checkout_table table-10">
        <tbody>
            <tr class="total_price my-totals">
                <td class="wpsc_totals my-totals-pad"></td>
                <td class="wpsc_totals"><span class="pricedisplay checkout-total" id="checkout_total"><span class="pricedisplay">$32.48</span></span></td>
            </tr>
        </tbody>
    </table>
    I did get it to work using felgall's method
    PHP Code:
    add_action('wpsc_before_form_of_shopping_cart', 'add_my_totals');

    function add_my_totals() {
        ?><table  class="wpsc_checkout_table table-10">
     <tr class="total_price my-totals">
          <td class="wpsc_totals my-totals-pad">
         <?php _e("Total Price including Shipping and Tax""wpsc"?>
          </td>
          <td class="wpsc_totals">
             <span id="checkout_total" class="pricedisplay checkout-total"><?php wpsc_cart_total(); ?></span>
          </td>
       </tr>
       </table>
       <?php echo $my_new_totals;
       }

    ?>

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Just to explain why my method did not work and @felgall ; did.

    I missed the fact that _e() was performing an echo to write out the translation, and because of that, it would produce the HTML result you received where the translation was outside of the table.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for clearing that up. I now happily have table of all price options, tax, shipping, coupons etc working

  8. #8
    Non-Member Wisestepp's Avatar
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good that I've actually been using for a while now. But now i got the clarity

  9. #9
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Ugh... You should never have html in a function like that. Your function should deal with pure data. You should then be calling the function in your view (html) script and using the data returned to create the view/html.

    If you hard code html into functions like that, you'll make your site virtually impossible to change.

    *Edit: I'm assuming wordpress doesn't expect you to do that? I've never coded in wordpress before. It's a horrible way to write code, because you end up with a design that's nigh on impossible to change. Don't do it!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •