Php inside wordpress pages stopped working after update - php

I have a wordpress website and it runs with some php code inside the pages. After I updated it stopped working and when I try to edit the code again, it changes the <?php to <!--?php not allowing it to run anymore.
I tested with a simple code like:
<?php
echo "Hello World"
?>
And it's not working after the update.
Is there any easy solution to this? Thanks.

I solved the issue using 2 plugins to insert php and javascript code into the page. Thanks a lot #idlab and #arifullslam for the help!

Related

PHP variable in href in PHP

I'm struggling with the syntax of this:
echo '<strong>Datasheet</strong>';
The central PHP element gets a URL from a WooCommerce custom field (I know this works ok on its own). That needs to be turned into a viable link opening a new page, and the whole sits in a PHP wrapper on the page.
Where am I going wrong?
Try this hopefully, it will work.
echo '<strong>Datasheet</strong>';
Try adding more to / changing the href
something like this might work
echo '<strong>Datasheet</strong>';
You'll have to adjust a little bit, but should do the trick

Calling PHP from Smarty template

I am trying to integrate an ad package into my Smarty based site. I have been given the following PHP code that I need to add to my footer file. But due to it being Smarty I need to add the code instead to my php file so that it can be called/included in my Smarty template file. This was the code that I was given and that I need to include:
<?php if(function_exists('ad_footer')) ad_footer(); ?>
But in the php file I am not sure how to achieve this - I have got this example and wanted to know if it looked correct??
if(function_exists('ad_footer')) {
ob_start();
ad_footer();
$smarty->assign('ad_footer', ob_get_clean());
}
Does the above look the right way to do it?
Thanks in advance
I went ahead and created a test page to see if the above worked and it did so will stick with this code.

Execute the Code in a PHP Page And Echo the HTML Output

I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()

php the_content executes incorrectly if placed after script in html body

I am using wordpress and using the following to drag the pages content into the html body.
<?php the_content(); ?>
It works fine when placed anywhere above the script, but if I place it anywhere on the page after the script, it outputs as though it's an echo function (I'm guessing that what it is) of the same script.
Can anyone tell me what might be going on and if there is a way to stop this from happening. I tried placing the script in a separate page and using the include function to pull it in, but that didn't work either.
<?php
$pages = array(
0 => 'longtailtext/intro1-1.php',
1 => 'longtailtext/intro1-2.php'
);
srand(floor(time()/60/60/1));
$key =rand(0,count($pages)-1);
include($pages[$key]) ;
?>
Placing the content tag anywhere after the script simply outputs the following:
"longtailtext/paragraph1-1.php"
Thank you for any help you can give me on this :)
Could be a rogue plugin causing the error. Try this: http://hungred.com/how-to/avoid-trouble-filter-thecontent-wordpress/ from here.
Maybe you should be using http://codex.wordpress.org/Function_Reference/get_the_content

Why is my PHP in articles being interpreted literally as a string instead of as PHP?

I am trying to find a way to write pure PHP in my articles with Joomla (currently using 1.5). The closest I got so far was to use a plugin called Sourcerer (see link)
The issue is that the PHP code is actually being rendered as HTML. When I write the below code via the Sourcerer editor:
<?php echo "Hello world"; ?>
This is what is being written to the article:
<span><</span>?php echo "Hello world"; ?<span>></span>
I am afraid that at some point this is going to break my PHP if I come up with more complex code. Also this makes the code hardly readable. Finally the issue with Sourcerer is that you can only insert new code, you can't edit what you previously added.
Does anybody know a way to write pure PHP into articles?
I would like to insist on the fact that it has to be within articles. I already found a way to do it in modules or components, for instance with Jumi.
I have used Sourcerer a couple of times and it does allow one to insert PHP snippets into articles without issues. If your php is being rendered as html, it sounds like the php isn't getting parsed as php - first thing is to make sure that the sourcerer plugin is indeed enabled so it can allow your php scripts to get parsed. (check plugin enabled status here: extensions->plugin manager->sourcerer).
Also, use the sourcerer ("Insert Code") button at the bottom of your WYSIWYG editor when adding your PHP scripts - it keeps the formatting/syntax highlighting and ensures that it is escaped properly with the {source} {/source} tags.
An update, I have the same issue with joomla 3.x, sourcerer, I have tried tinymice, JCKEdidor and JCEEditor. I have now given up with this method.
To insert php code into an article I have used a php include file and all the html is echoed inside this php include file. The php file is then referenced from within the Article Editor.
Those more proficient than me with joomla will no doubt do this correctly using a module or the like.
You can try another plugin called Direct PHP. It's nice and simple to use. Just download and install it and make sure it is enable. After this start writing your php code in joomla article.
Example:
<?php
echo "Hello World!";
?>
it supports all most every PHP functions and you can filter the functions what you don't need it from its settings.
Here is the link for the download.
Download & import plugin sourcerer https://extensions.joomla.org/extension/sourcerer/
{source} [[?php echo "Hello world"; ?]] {/source}
use this. It fine for me.

Categories