phphtmlescapingpre

Show PHP/HTML as text on page in php template


How can I spit out the following code as text on a page inside of 'pre' and 'code' tags without my php template actually trying to read the php code. With js and css the 'code' and 'pre' tags stop the actual code from running. How do I do this with the following combo of HTML/PHP.

I have tried using htmlescaping but this only spits out the php. I can't figure out what the right combination is here.

<pre>
                <code>
                <?php elseif(get_row_layout() == 'three_col_panel'): ?>

                <!-- ==================================== -->
                <!-- === 3 COLUMN ICON TEXT GRID ======== -->
                <!-- ==================================== -->

                <div class="icon-panel-three block">
                    <div class="main-wrap">
                        <div class="container">
                            <div class="row">
                                <?php if( have_rows('pillar')): ?>
                                    <?php while ( have_rows('pillar')) : the_row() ?>
                                        <div class="col-md-4 icon-column">
                                            <div class="inner-content">
                                                <?php $image = get_sub_field('image'); 
                                                            $url = $image['url'];
                                                            $alt = $image['alt'];
                                                            $size = 'medium';
                                                            $thumb = $image['sizes'][$size];
                                                    ?>
                                                <a href="<?= get_sub_field('image_link'); ?>"><img src="<?= $thumb; ?>" alt="<?= $alt; ?>"/></a>
                                                <div class="text">
                                                    <h1><a href="<?= get_sub_field('image_link'); ?>"><?= get_sub_field('heading')?></a></h1>
                                                    <p><?= get_sub_field('paragraph')?></p>
                                                </div><!--end text-->
                                                <?php if( have_rows('column_link')): ?>
                                                    <?php while ( have_rows('column_link')) : the_row() ?>
                                                        <a href="<?= get_sub_field('col_link_url'); ?>" class="body-button"><?= get_sub_field('col_link_text'); ?><img src="<?= get_template_directory_uri(); ?>/img/button-arrow.svg" alt="click here" /></a>
                                                    <?php endwhile; ?>
                                                <?php endif; ?>
                                            </div><!--end inner-content-->
                                        </div><!--end col-->
                                    <?php endwhile; ?>
                                <?php endif; ?>
                            </div><!--end row-->
                        </div><!--end container-->
                    </div><!--end main-wrap-->
                </div><!--end tile-panel-->

            </code>
            </pre>

Solution

  • Precondition:

    Text to output must be in a variable string or in a separate file.

    I suggest you the file solution: it is more simple, the code is more clear, and you can use one file to display more that one code.

    If you prefer the string-way, you have to wrap it by single quotes and escape internal single quotes:

    $string = '<?php elseif(get_row_layout() == \'three_col_panel\'): ?>
    
                    <!-- ==================================== -->
               (...)';
    

    You can't use double quotes, because they allow php variables evaluation.

    If you prefer, you can use also Nowdoc string syntax: in this case, no escaping is needed.

    Best way:

    Use highlight_string( $string ) if your text is a string, or highlight_file( $filepath ) if you want output a complete file.

    You don't need to wrap it by <code></code>: above commands wrap-it for you.

    As gift, you will have colored syntax!

    Alternative way:

    For string:

    <code><?php echo htmlentities( $string ); ?></code>
    

    For files:

    <code><?php echo htmlentities( file_get_contents( $filepath ) ); ?></code>