0

So I have this code (which is irrelevant I guess):

<!-- Ad Code - DO NOT MODIFY -->

<?php

function isUsignMobileDeviceTrue() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, show ads
if(isUsignMobileDeviceTrue()): ?>

<?php echo file_get_contents("https://www.example.com/ad1.php");
echo 'https://www.example.com';
?>

<?php endif;?>

<!-- //Ad Code End-->

Basically I want the above code to be shown as text. Same as the above... So that a user could copy and paste.

I'm using this inside the .php file.

I have tried <code> my above code </code> in Wordpress, but it still tries to execute the code. I guess this works in Post only.

Is there any simple code line to wrap around so that php/html/javascript code would show as plain text, instead of trying to execute it?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
r0naldinio
  • 171
  • 6
  • You can use plugin for WordPress:- [full tutorial](https://www.greengeeks.in/tutorials/insert-php-code-wordpress-posts-pages/) – Hedayat H Sep 03 '21 at 11:45
  • Hedayat H, this is not what I wanted. I know this plugin and how it works. Thanks for your time anyway. – r0naldinio Sep 03 '21 at 11:48
  • Usually when code is not executed, people wish it was! https://stackoverflow.com/q/5121495/2943403 – mickmackusa Sep 03 '21 at 12:02

2 Answers2

1

Without a plugin you can do this - I just changed all < to &lt; and wrapped in <pre></pre>

<pre>&lt;!-- Ad Code - DO NOT MODIFY -->

&lt;?php

function isUsignMobileDeviceTrue() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, show ads
if(isUsignMobileDeviceTrue()): ?>

&lt;?php echo file_get_contents("https://www.example.com/ad1.php");
echo 'https://www.example.com';
?>

&lt;?php endif;?>

&lt;!-- //Ad Code End--></pre>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

You have to escape the HTML tags because < and > and some other chars have an effect in the HTML rendering. But you can escape it yourself or use a PHP function to do that: htmlspecialchars().

You can save your code to display in a variable with the Nowdoc syntax to make it easy and then print it by escaping HTML chars:

<?php

$code = <<<'END_OF_CODE'
<!-- Ad Code - DO NOT MODIFY -->

<?php

function isUsignMobileDeviceTrue() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, show ads
if(isUsignMobileDeviceTrue()): ?>

<?php echo file_get_contents("https://www.example.com/ad1.php");
echo 'https://www.example.com';
?>

<?php endif;?>

<!-- //Ad Code End-->
END_OF_CODE;

echo '<pre></code>' . htmlspecialchars($code) . "</code></pre>\n";

The syntax coloring on Stackoverflow is not handling Nowdoc format. But you could test it here: http://sandbox.onlinephpfunctions.com/code/078eba2279612adabaaa59560cf3889459e4dfe8

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
Patrick Janser
  • 3,318
  • 1
  • 16
  • 18