March 22, 2021

Wordpress with PHP Everywhere

Sometimes you have to handle things in Wordpress that you can't find any plugin for.

In our case I had to visualize a list from a MySQL database and in another case I had to fetch data from another website.

In both cases I solved it with inserted PHP code which is executing and getting the info.

Plug-in
I have been using this plug-in, for several years, but there are no updates, and now also using the Gutenberg block editor, it was time to find another plug-in. 

After some Googling i found this plugin PHP Everywhere which can be used in both the old and the new editor.

It's just tested up to WP 5.5.3 but I'm using it on both 4.x.x and 5.6.2 and it works well.

Error handling
One major thing to consider is the error handling. Due to bad PHP code, and an upgrade by the web-host, some parts of our site stopped working. I solved this by simply adding 

<?php error_reporting(0); ?>

as the first row in my code which will encapsulate the error. In this way a potential error will not affect other parts of the site, and just produce "blank" information.

Test
To test the function just add a "PHP Everywhere" block or in the classic editor a shortcode "[php_everywhere]" and paste the code below.

Code example

<?php error_reporting(0); ?>
<?php
echo "Hello World ! <br>"; // Test
echo "Today is ".date("Y-m-d")."<br>";
$date = date("Y-m-d");
$time = date("H:i:s");

?>

<!DOCTYPE html>

<head>
<title>Today is ? </title>
</head>

<body>
<table>
<tbody>
<tr>
<td>
<li>Today is </li>
</td>
<td>
<?= $date ?>
</td>
</tr>
<tr>
<td>
<li>Time is </li>
</td>
<td>
<?php echo $time ?>
</td>
</tr>
</tbody>
</table>

</body>

</html>


No comments:

Post a Comment

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !