Jump to content

PHP include in field content


ryanC
 Share

Recommended Posts

Hi, I have php includes in my template file that are working fine....however I need to have some additional includes within a text area field, where I enter my HTML content.

For example, this code at the top of my template file:

<?php $myPath = $config->urls->templates;?>

Allows me to have something like this on every page:

<?php include("includes/footer.inc.php"); ?>

So far so good, but I have a text area field named page_content, where I would like to manually enter additional includes. 

If I enter this in that text area field, I get a photo:

<img src="images/photo.jpg" />

But if I enter this in that same field, nothing happens at all:

<?php include("includes/phone.inc.php"); ?>

Any thoughts? I have reviewed this thread https://processwire.com/api/ref/files/include/ but I don't think it gets at what I'm looking for.

Thanks!

Link to comment
Share on other sites

CKEditor fields are regular HTML strings, they're not evaluated as PHP. Even then, whatever you write in the field is encoded, so < and > for example will turn into &lt; and &gt; HTML escaped variants.

Your problem can be solved easily with Hanna Code

 

  • Like 5
Link to comment
Share on other sites

Thanks abdus, does this apply even if the field isn't a regular textarea? I should have added that I am using the AceExtended module. I have a lot of HTML content in there already, which is working fine, like :

<section>
<div class="xyz">
<img src="images/photo.jpg" />
</div>
</section>

I wonder if using a javascript text would be a work around. I did look into that Hannah page, I'd like to use it but it's a little beyond my skill level.

Link to comment
Share on other sites

Here's a sample Hanna code that you can use in text fields as [[include file=test]] where `test` is a php file at templates/includes/test.php.

<?php namespace ProcessWire; 
// test.php

echo 'hello';

?>
<p>regular html</p>

Hanna Code definition is just these lines:

<?php

$file = "{$config->paths->templates}includes/{$file}.php";
if (file_exists($file)) include $file;

Or use this to quickly import into your setup

!HannaCode:include:eyJuYW1lIjoiaW5jbHVkZSIsInR5cGUiOiIyIiwiY29kZSI6Ijw/cGhwXG5cbiRmaWxlID0gXCJ7JGNvbmZpZy0+cGF0aHMtPnRlbXBsYXRlc31pbmNsdWRlc1wveyRpbmNsdWRlfS5waHBcIjtcbmlmICghZmlsZV9leGlzdHMoJGZpbGUpKSByZXR1cm47XG5cbmluY2x1ZGUgJGZpbGU7In0=/!HannaCode

 

  • Like 2
Link to comment
Share on other sites

Hi, I am trying this Hannah example out, I think I am missing a step.

I created a new php Hannah code called test that contains this:

<?php

$file = "{$config->paths->templates}includes/{$file}.php";
if (file_exists($file)) include $file;

made a new include file at templates/includes with:

<?php namespace ProcessWire; 
// test.php

echo 'hello';

?>
<p>regular html</p>

and in my page content field I have 

[[include file=test]] 

but the page output just gives me "[[include file=test]]"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...