Show PHP/HTML as text on page in php template - php

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];
?>
<img src="<?= $thumb; ?>" alt="<?= $alt; ?>"/>
<div class="text">
<h1><?= get_sub_field('heading')?></h1>
<p><?= get_sub_field('paragraph')?></p>
</div><!--end text-->
<?php if( have_rows('column_link')): ?>
<?php while ( have_rows('column_link')) : the_row() ?>
<?= get_sub_field('col_link_text'); ?><img src="<?= get_template_directory_uri(); ?>/img/button-arrow.svg" alt="click here" />
<?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>

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>

You just have to put all that between double quotes: "...", and escape the double quotes inside:
<div class=\"icon-panel-three block\">
<div class=\"main-wrap\">
.... etc

Another thing you can do is this: put it all in a .txt file and show the contents with php <?php
$homepage = file_get_contents('mycode.txt');
echo $homepage;
?>

with php you can try the php function "highlight_string();" like this:
<?php
highlight_string('<?php phpinfo(); ?>');
?>
It will output the ('input') as a string with colorcoding.
check out its php documentation: http://php.net/manual/en/function.highlight-string.php

Related

PHP wordpress template - add default image if none selected

Apologies, I am relatively new to PHP and am learning as I go, but am stuck on this... I have this template for a page-title section with a background image. I can add the background image, but if there is no background-img:url elected on the page, is there a way to write in a default background-img:url into this template?
<section class="page-title" <?php if($bg):?>style="background-image:url('<?php echo esc_url($bg);?>');"<?php endif;?>>
<div class="auto-container">
<h1><?php if($title) echo balanceTags($title); else wp_title('');?></h1>
<div class="bread-crumb">
<?php echo convo_get_the_breadcrumb();?>
</div>
</div>
Add this before your code:
if(empty($bg)){
$bg = '/path/to/your/default/image.jpg';
}
<section class="page-title" <?php if($bg):?>style="background-image:url('<?php echo esc_url($bg);?>')" <?php else: ?>
style="background-image:url('your URL')"
<?php endif; ?>>
Using #psdev's code, I was able to add in a default background-image if $bg was empty. Thanks!!
<section class="page-title" <?php if(empty($bg)){$bg = 'http://aqmenalt.mhsites.co.uk/wp-content/uploads/2017/06/Banner-10.png';} if($bg):?>style="background-image:url('<?php echo esc_url($bg);?>');"<?php endif;?>>
<div class="auto-container">
<h1><?php if($title) echo balanceTags($title); else wp_title('');?></h1>
<div class="bread-crumb">
<?php echo convo_get_the_breadcrumb();?>
</div>
</div>

PHP looping custom fields with bootstrap

So I have this code I want to repeat. Uses wordpress acf to generate the image:
`<div class="row">
<?php if( get_field('image_1') ): ?>
<img class="this-image" src="<?php the_field('image_1'); ?>" />
<?php endif; ?>
</div>`
I'm just wondering how I can loop this in with which I would also need to increment the numbers (ie. image_1, image_2, image_3).
I'm having trouble figuring out the logic of PHP, if there already is a similar post, a kind link would be much appreciated, thanks!
Use below code - it is using for loop to iterate through all images from image_1 to image_X (where X can be defined by you). similar code can be modified by add an array to store all the image names, just in case if there is no patterns for the image names.
<div class="row">
<?php $numImages = 10; #Define how many images you have
for ($i=1; $i<= $numImages; $i++){ #starting from image_1 ?>
<?php if( get_field('image_' . $i)): ?>
<img class="this-image" src="<?php the_field('image_' . $i); ?>" />
<?php endif; ?>
<?php } ?>
</div>
You can try something like this
`<div class="row">
<?php
for (i=1; i<=3; i++) {
if( get_field('image_'.i) ): ?>
<img class="this-image" src="<?php the_field('image_'.i); ?>" />
<?php endif; } ?>
</div>`

Live jQuery Thumbnails with PHP

I am trying to implement the jQuery live thumbnails with my PHP page. On the page I am retrieving event information along with a comma separated list of thumbnail values. The plugin works a bit differently that that and I am having difficulty adapting my script to work. The plugin is located here http://tutorialzine.com/2012/12/album-photo-previews-jquery/. In their example they use an array while I have a comma separated list of thumbnails. The code I have so far is here...
<?php foreach ($events as $event) { ?>
<div class="left13 section_home" style="margin-bottom:25px;">
<h2><?php echo $event['event_title']; ?></h2>
<div align="center">
<?php foreach ($event['thumbnails'] as $thumbnail) { ?>
<a href="#" data-images="<?php echo str_replace(',', '|', $event['thumbnails']); ?>" class="album">
<img src="<?php echo ($event['thumbnails'] != '') ? base_url() . 'media/photos/thumbnail/' . $event['thumbnails'] : base_url() . 'images/no_photo_thumbnail.png'; ?>" alt="" title="" /><span class="preloader"></span></a>
<p><?php echo ($event['event_description'] != '') ? substr($event['event_description'], 0, 200) : 'No description yet...'; ?></p>
<span class="swirl_left"><span class="swirl_right">View This Event</span></span>
<?php } ?>
</div>
</div>
<?php } ?>
How would I be able to make this script work by using a comma separated list of thumbnail values instead of the array used in the tutorial? Thanks!
I figured it out and the answer is quite simple. First, my data coming from the database included a comma separated list of thumbnails. I just used the proper functions to insert that data into the script. The only problem I have now is that I need to add the path to the beginning of each value in the comma separated list. Is there a PHP function to do that? Below is my almost finished code.
<div class="content">
<?php foreach ($events as $event) {
// Create an array out of the comma separated list of thumbnails
$thumbnails = explode(',', $event['thumbnails']); ?>
<div class="left13 section_home" style="margin-bottom:25px;">
<h2><?php echo $event['event_title']; ?></h2>
<div align="center">
<img src="<?php echo base_url() . 'media/photos/thumbnail/' . $thumbnails[0]; ?>" alt="" title="" /><span class="preloader"></span>
<p><?php echo ($event['event_description'] != '') ? substr($event['event_description'], 0, 200) : 'No description yet...'; ?></p>
<span class="swirl_left"><span class="swirl_right">View This Event</span></span>
</div>
</div>
<?php } ?>
</div>
To finish this up I need to add the following to the beginning of each item in $event['thumbnails']...
<?php echo base_url(); ?>media/photos/thumbnail/
Then it should work perfectly.

PHP multiline string with PHP

I need to echo a lot of PHP and HTML.
I already tried the obvious, but it's not working:
<?php echo '
<?php if ( has_post_thumbnail() ) { ?>
<div class="gridly-image"><?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) ));?>
</div>
<?php } ?>
<div class="date">
<span class="day">
<?php the_time('d') ?></span>
<div class="holder">
<span class="month">
<?php the_time('M') ?></span>
<span class="year">
<?php the_time('Y') ?></span>
</div>
</div>
<?php } ?>';
?>
How can I do it?
You don't need to output php tags:
<?php
if ( has_post_thumbnail() )
{
echo '<div class="gridly-image">'. the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )) .'</div>';
}
echo '<div class="date">
<span class="day">'. the_time('d') .'</span>
<div class="holder">
<span class="month">'. the_time('M') .'</span>
<span class="year">'. the_time('Y') .'</span>
</div>
</div>';
?>
You cannot run PHP code within a string like that. It just doesn't work. As well, when you're "out" of PHP code (?>), any text outside of the PHP blocks is considered output anyway, so there's no need for the echo statement.
If you do need to do multiline output from with a chunk of PHP code, consider using a HEREDOC:
<?php
$var = 'Howdy';
echo <<<EOL
This is output
And this is a new line
blah blah blah and this following $var will actually say Howdy as well
and now the output ends
EOL;
Use Heredocs to output muli-line strings containing variables. The syntax is...
$string = <<<HEREDOC
string stuff here
HEREDOC;
The "HEREDOC" part is like the quotes, and can be anything you want. The end tag must be the only thing on it's line i.e. no whitespace before or after, and must end in a colon. For more info check out the manual.
Use the colon notation
Another option would be to use the if with a colon (:) and an endif instead of the brackets:
<?php if ( has_post_thumbnail() ): ?>
<div class="gridly-image">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )); ?>
</a>
</div>
<?php endif; ?>
<div class="date">
<span class="day"><?php the_time('d'); ?></span>
<div class="holder">
<span class="month"><?php the_time('M'); ?></span>
<span class="year"><?php the_time('Y'); ?></span>
</div>
</div>
The internal set of single quotes in your code is killing the string. Whenever you hit a single quote it ends the string and continues processing. You'll want something like:
$thisstring = 'this string is long \' in needs escaped single quotes or nothing will run';
To do that, you must remove all ' charachters in your string or use an escape character. Like:
<?php
echo '<?php
echo \'hello world\';
?>';
?>
Use the show_source(); function of PHP. Check for more details in show_source. This is a better method I guess.

Ad block won't place correctly

Basically, I need for the image at the bottom of the page with no text wrapped around it to be in the place of test post 4 (it's going to be a Google Adsense block but I am using images as a placeholder for now). I'm not sure how to do this, right now the code is the following:
<div class="google_ad_post">
<?php if ($count == 3) { ?>
<br /><img src="****" alt="post ad">
<?php } ?>
<?php $count++; ?>
</div>
Yet, the image is still at the bottom of the page. How can I fix this?
Here is the image:
I can't post pictures just yet so the URL to the image is http://i.imgur.com/7rw5B.jpg
I have to see your code to help you better with the Scripting Part, but something logical like this should work:
<?php
$count = 0;
foreach( $posts as $post ) : ?>
<?php if ($count == 3) { ?>
<div class="google_ad_post">
<img src="****" alt="post ad">
</div>
<?php } else { ?>
<div class="post" id="post-<?php the_ID(); ?>><div class="content"><?php the_content(); ?></div>
<?php } ?>
<?php $count++; ?>
<?php endforeach; ?>

Categories