Jump to content
zolute

How to add PHP code in CMS page?

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. :)

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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.ne...unction.php.tpl )

 

Thanks vekia. What should I edit in the CMS class. Is there a tutorial/doc/KB article?

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

 

Share this post


Link to post
Share on other sites
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

Share this post


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

Share this post


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

 

 

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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More