ryanC

PHP include in field content

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!

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

It works as long as Hanna Code is added to the field's textformatters.

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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]]"

Share this post


Link to post
Share on other sites

Add hanna code to field's textformatters.

Setup > Fields > <fieldname> > Details > Textformatters

  • Like 1

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      So maybe has already figured this out, but I am stumped. I have a field (test_field) that is set to pdfs, and I was trying to get a count of the number of pdfs in that field so I could add it to a status bubble on the front end. I tried:
      function testPDF() { $a = $page->test_field->count(); echo "<span class=\"bubble\">" . $a . "</span>"; } but it is returning "NULL" . I currently have 3 added to the field. I also tried putting this function in _func.php, though I need to use wire('pages'), but I only need to get the count for that specific page, so I am sort of at a loss of how to proceed.
    • By sirhc
      Hi all,
      Im trying to install this core module and got the error of not having the ImageMagick library installed.
      But if im checking for the version of my ImageMagick installed on my server via SSH im getting this response;
      identify -version
      Version: ImageMagick 6.9.4-9 Q16 i386 2016-06-21 http://www.imagemagick.org
      Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
      License: http://www.imagemagick.org/script/license.php
      Features: Cipher DPC 
      Delegates (built-in): bzlib freetype gslib jng jpeg png ps tiff xml zlib
      Does this even mean the library is installed? And if yes, why cant i install my module?
      Pleaassee help me
      Greetz.
    • By rocket
      Noob to Processwire. Trying to convert some older sites to Pwire. I am having trouble including a legacy form class into my templates:
      If I embed the php directly in the template, it works. But if I use include statements, the form object returns null.
      This works:
          use formbuild\Form;
          session_start();
          $form = new Form(init1, 'init2', ...');
          $form->param1 = ...;
          $form->param2 = ...;
          $form->param3 = ...;
       
          $form->render();
      -----------------------------------------------------------------------------------------
      This fails:
          include_once ($config->paths->templates."forms/form1.php");
          $form->render();
      ____________________________________________________________________
      My referencing is OK, no error message. The only code that cannot reside in the include file are the following lines:
          use formbuild\Form;
          $form = new Form(init1, 'init2', ...');
      My PhP is rusty, maybe that is the problem, but it could be something about Pwire, namespace maybe. I have tested it outside Pwire and no problems.
      Please advise. Thanks.
       
       
    • By louisstephens
      I have a foreach loop on a dashboard page where I sorting the pages titles to different columns based on a date field. Unfortunately, I am doing it with multiple different if statements for the sorting (with multiple foreach loops), as I needed something quickly for testing. I thought it would be nice to implement wireSMTP to send an email out to alert the users that created the pages, which I got working in no time. However, now I have hit a roadblock. The dashboard page has a javascript function that refreshes the page every few hours to get the changes in the date, which then triggers the $mail to fire the email out. 
      I guess my question is there a way to limit the emails to be sent out only once the date/time has changed once instead of it firing every time the page refreshes?
    • By louisstephens
      I was working on a simple "to-do" style template that has a form on a page. Once the form is submitted, I use the API to  create a new subpage under a pre-existing page, but I notice that it does resubmit the form data (as to be expected) if a user were to refresh the page. Is there a way to prevent this behavior so duplicate content will not be posted by using something like exit(), or is this not proper due to usability?  I guess I am curious as to how other developers handle the same hurdle.