Jump to content

How to add PHP code in CMS page?


zolute

Recommended Posts

How to add PHP code in a CMS page?

 

The reasons I am posting this question is

  1. I am new to prestashop
  2. Google is not helping me and/or my searching skills have degraded.

I want to include certain php codes in a CMS page. Is this possible with prestashop?

I have been using jumi to achieve this in Joomla (just a comparison so that you get the idea of what I want).

 

Prestashop version - 1.4.10.0

 

Please don't disappoint me. :)

Link to comment
Share on other sites

You can't add the php code in the back office -> preferences -> CMS.

you also can't add php code in tpl (template)

 

the only way to add php code is edit controllers / classes - in this case CMS class

 

/classes/CMS.php

  • Like 1
Link to comment
Share on other sites

How to add PHP code in a CMS page?

 

The reasons I am posting this question is

  1. I am new to prestashop
  2. Google is not helping me and/or my searching skills have degraded.

I want to include certain php codes in a CMS page. Is this possible with prestashop?

I have been using jumi to achieve this in Joomla (just a comparison so that you get the idea of what I want).

 

Prestashop version - 1.4.10.0

 

Please don't disappoint me. :)

 

 

It depends on what kind of code do you want to add exactly, you can simply implement it into your existing php page or you can create completely new and separate PHP page and add your desired code there. Then just include PHP page to your website.

Edited by AndyLaw (see edit history)
  • Like 1
Link to comment
Share on other sites

It depends on what kind of code do you want to add exactly, you can simply implement it into your existing php page or you can create completely new and separate PHP page and add your desired code there. Then just include PHP page to your website.

 

php code isn't working in preferences -> CMS

moreover - it isn't available to add php code into smarty templates (cms.tpl)

 

in this case the only way to add php is edit CMS class.

 

and {php}{/php} doesnt work.

( http://www.smarty.net/docsv2/en/language.function.php.tpl )

Link to comment
Share on other sites

It depends on what kind of code do you want to add exactly, you can simply implement it into your existing php page or you can create completely new and separate PHP page and add your desired code there. Then just include PHP page to your website.

 

Let's say I wish to put a custom contact form. How do you suggest I proceed.

Link to comment
Share on other sites

I suppose as vekia mentioned with previous post, you can implement this through /classes/CMS.php only of what you desire.

 

Yet here is tutorial which I found, you may want to try this:

How to add a "Contact Us" link to a CMS block in PrestaShop

 

In PrestaShop the CMS Block module displays by default an Information block in the left column of the store's frontend, as well as some footer links. The Information box shows links to different CMS pages. From the backend of your PrestaShop you can create as many CMS pages as you want (Tools tab>CMS sub-tab), and from the settings of the CMS Block module you can add links to these pages in any of the blocks displayed by the module (e.g. left, right, footer).

The Contact Us page, however, is not a CMS page, so you can't use the module's options to add it as a link in the default Information block, for example. To do this you have to modify the code of the file blockcms.tpl. If your PrestaShop is installed in a folder called prestashop in the root public_html directory of your PrestaShop hosting account, then the path to the file would be public_html/prestashop/modules/blockcms/blockcms.tpl. One way to modify the file is from the Files section of the Pixie control panel.

Inside the blockcms.tpl file you'll see a section labeled <!-- Block CMS module -->. In that section you'll see the following tags:

<ul class="block_content">

 

</ul>

 

Between them there's some code for the different links. Somewhere on a new line after the opening tag (<ul class="block_content">) or before the closing tag (</ul>) insert the following code:

<li><a href="{$link->getPageLink('contact-form.php')}"

title="{l s='Contact Us' mod='blockcms'}">{l s='Contact Us' mod='blockcms'}</a></li>

 

If you put the code right after the above mentioned opening tag, the Contact Us link will be the first in the CMS block. If you put it right before the closing tag, it will be the last link.

To be able to see the result of changes to template files on the frontend of the store, you also have to enable the Force Compile option. You can do this from the Performance sub-tab of the backend's Preferences tab. After you're done with the changes and you've checked the results on the frontend, disable the option.

You should also keep in mind that changes to core files are overwritten during an update of the application.

  • Like 1
Link to comment
Share on other sites

well.. you need to know something about OOP concepts and how prestashop works.

the most important thing is to implement correct php code / functions to the CMSController.php (controllers/ CMSController)

 

for example:

 

public function displayContent()
{
 parent::displayContent();
 self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}

 

add code to handle form:

 

public function displayContent()
{
IF  ($_POST['submit_form']==1){
  // here submit action mail() for example
}
 parent::displayContent();
 self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}

 

in cms.tpl file you can add simple smarty {if} condition, where you can add contact form with own fields.

 

{if YOUR_CONDITION}

<form>

your fields

</form>

{/if}

  • Like 1
Link to comment
Share on other sites

well.. you need to know something about OOP concepts and how prestashop works. the most important thing is to implement correct php code / functions to the CMSController.php (controllers/ CMSController) for example:
 public function displayContent() { parent::displayContent(); self::$smarty->display(_PS_THEME_DIR_.'cms.tpl'); } 

add code to handle form:

 public function displayContent() { IF ($_POST['submit_form']==1){ // here submit action mail() for example } parent::displayContent(); self::$smarty->display(_PS_THEME_DIR_.'cms.tpl'); } 

in cms.tpl file you can add simple smarty {if} condition, where you can add contact form with own fields. {if YOUR_CONDITION} your fields

{/if}

 

 

Okay. I'll give it a try and let you know how it worked out.

 

Thanks for your time vekia and AndyLaw.

 

I <3 opensource

Link to comment
Share on other sites

I know it wasn't discussed in this thread, but you can add php in the tpl files. All you need to do it wrap them in {php}{/php} and enable php in smarty. It is considered in secure though, best practice is to either make an overrride, or in your case a new php file to control a tpl.

Link to comment
Share on other sites

I know it wasn't discussed in this thread, but you can add php in the tpl files. All you need to do it wrap them in {php}{/php} and enable php in smarty. It is considered in secure though, best practice is to either make an overrride, or in your case a new php file to control a tpl.

 

But {php}{/php} tags are deprecated from Smarty

 

documentation:

{php}

IMPORTANT NOTICE

{php} tags are deprecated from Smarty, and should not be used. Put your PHP logic in PHP scripts or plugin functions instead.

 

Note

As of Smarty 3.1 the {php} tags are only available from SmartyBC.

 

 

{php}

IMPORTANT NOTICE

{php} tags are deprecated from Smarty, and should not be used. Put your PHP logic in PHP scripts or plugin functions instead.

 

Note

As of Smarty 3.1 the {php} tags are only available from SmartyBC.

 

Link to comment
Share on other sites

  • 3 years later...
Guest locen

Hi, if i save cms page for the first time, then i re-save (ex: save and preview) i have two pages created.

 

the second time that i reopen page all is ok.

 

Can someone help me?

Thanks

Link to comment
Share on other sites

please create now topic related to your case.

problem you have it is totally different that main case in this thread about php code usage inside cms pages.

in new topic please share information about:

- what is your prestashop version.

- some environment details (from prefrences > informations section in BO)

Link to comment
Share on other sites

  • 4 months later...

well.. you need to know something about OOP concepts and how prestashop works.

the most important thing is to implement correct php code / functions to the CMSController.php (controllers/ CMSController)

 

for example:

 

public function displayContent()
{
  parent::displayContent();
  self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}
add code to handle form:

 

public function displayContent()
{
 IF  ($_POST['submit_form']==1){
   // here submit action mail() for example
 }
  parent::displayContent();
  self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}
in cms.tpl file you can add simple smarty {if} condition, where you can add contact form with own fields.

 

{if YOUR_CONDITION}

<form>

your fields

</form>

{/if}

 

 

I'm adding a questionnaire on cms page to recommend some products depending on the result.  I could use iFrame to add that function, but I'd rather plugin to the page.

Does this method (using PHP on cms)  work for the current version (1.6.1) too?

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
×
×
  • Create New...