Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 2 of 6
How do I use PHP in CMS pages
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Say, you want to run custom PHP code that can accept arguments.

1. declare your module: app/etc/modules/Mayerwin.xml

<?xml version="1.0"?>
<config>
  <
modules>
    <
Mayerwin_Custom>
      <
active>true</active>
      <
codePool>local</codePool>
    </
Mayerwin_Custom>
  </
modules>
</
config>
2. create module config: app/code/local/Mayerwin/Custom/etc/config.xml
<?xml version="1.0"?>
<config>
  <global>
    <
blocks>
      <
mayerwin_custom>
        <class>
Mayerwin_Custom_Block</class>
      </
mayerwin_custom>
    </
blocks>
  </global>
</
config>
3. Create your custom PHP code: app/code/local/Mayerwin/Custom/Block/Test.php
<?php

class Mayerwin_Custom_Block_Test extends Mage_Core_Block_Abstract
{
  
protected function _toHtml()
  
{
     
// put here your custom PHP code with output in $html;
     // use arguments like $this->getMyParam1() , $this->getAnotherParam()

     
return $html;
  
}
}
4. Use your custom PHP logic in CMS page/block:
{{block type="mayerwin_custom/test" my_param1="value 1" another_param="value 2"}}
 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

Thank you very much! That’s great! I couldn’t guess how to do that. Maybe it should be added to the Wiki or KB so that it could benefit to many people in other circumstances.
I love the way things are secured and organized in Magento (although at the beginning you’re not used to it).

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

Well, I have tried your solution, with $html = “Hello world” inside Test.php, but nothing shows up. Is there a mistake somewhere? Again, there is no doc regarding this so I can’t check by myself :-(.
As for my previous solutions, there are problems with variables that become undefined if they are not in the same <?php ?>. So I’m even more interested in your solution.
I also checked this : http://www.magentocommerce.com/wiki/how_to_create_an_admin_form_module but it’s not very appropriate (for admin modules apparently).

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

In original post I’ve made a mistake, in first file (module declaration) please try <codePool> instead of <codepool>

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

Ok, it now works perfectly! Thank you Moshe for your speed and acurracy, I appreciate a lot. There is no doubt this will be also helpful for others who want to embed some custom PHP.

This new way of building a website is not easy but I think I start understanding the positive aspects in the long term. And it is probable that as time goes on, more non-developers-friendly CMS features will become available in the back-end (just like those offered by Joomla). But even right now, thanks to your help, I have already managed to migrate my whole PHP website (mainly CMS pages, the store was not completed before) to Magento and I will take the most of the multi-website engine.
By the way, have you any idea concerning the possibility to display a CMS page or Static Block to several stores (cf. this feature request : http://www.magentocommerce.com/boards/viewthread/4179/) instead of all stores or only one of them?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@mayerwin: store views multiselect for cms pages and blocks has been implemented and should be available in next release.

 
Magento Community Magento Community
Magento Community
Magento Community
 
betabomber
Member
 
Avatar
Total Posts:  37
Joined:  2008-02-22
 

I’ve been following this, and I would highly suggest adding PHP support for CMS pages. Perhaps as like a little check box or ratio boxes that would enable or disable PHP, HTML… etc.

My company works on motorcycles and sells parts. We would like to be able to provide our customers with a tool to check up the status of their bike when it is in the shop.

I was thinking about having a CMS page that would redirect the customers to another page relative to their account name.

I’m not against writing the custom module, but would there be a way to do this without creating a custom module?

I feel like I should be able to do something like:

<?php 
http_redirect
($_SERVER['HTTP_HOST'$_SERVER['PHP_SELF'$_GET('username')

Or something to that effect.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  182
Joined:  2008-01-15
France
 

Sure, you may also create a new template file for your page (which is a phtml file, found in app\design\frontend\default\default\template\page), and insert your PHP code at the very beginning of this file.
To be able to select this template in the back-end, you should then declare it in app\code\core\Mage\Cms\etc\config.xml just by duplicating the lines like this ones :

<three_columns>
                    <
label>3 columns</label>
                    <
template>page/3columns.phtml</template>
                </
three_columns>
Good luck!

@Moshe: Thank you very much for this information! I’m long to discover the next release then.

 
Magento Community Magento Community
Magento Community
Magento Community
 
sdb
Sr. Member
 
Avatar
Total Posts:  223
Joined:  2007-11-13
coastal California, USA
 

i just wanted to say thanks for this thread
while i currently have no need for php in my cms, its great to know that its possible, and i might design for it in the future knowing that

 
Magento Community Magento Community
Magento Community
Magento Community
 
TomDebus
Jr. Member
 
Total Posts:  3
Joined:  2008-02-16
Zürich & Freiburg
 

Hi Moshe,

thank for all the great explaining. I´m trying to integrate the Phorum php-based forum into my up an coming Magento Shop. However when following your instructions to get the MayerErwin HelloWorld example running I run into a Mage tracing error:

I do use the TelescopeTheme - may be an issue there, since the inline translation does not work with that template either?

While both mage and phorum seem to work fine, the integration piece seems to be som while of yet…

Check http://edelbraen.de => for the rough shop outline
and http://edelbraen.de/forum => for the Phorum installation

and

http://www.edelbraen.de/index.php/admin/eb_forum?store=default => for the bespoke tracer error

Any hints as to where I could start?

Thanks
-Tom

 
Magento Community Magento Community
Magento Community
Magento Community
 
redpen
Sr. Member
 
Total Posts:  254
Joined:  2008-01-23
 

Moshe: is there any reason this would suddenly stop working?  Was working fine on a local site, we moved today to our own dedicated server and the PHP inside the CMS you wrote at the top of the page no longer works - just doesn’t do anything...?

 
Magento Community Magento Community
Magento Community
Magento Community
 
synthtk
Jr. Member
 
Total Posts:  1
Joined:  2008-05-30
 

Moshe,

Hello and thank you for all of your help on these forums, just reading your answers to other peoples issues has helped me immensely in setting up Magneto.

I have tried the code you posted at the top of this page and it does not seem to be working correctly in 1.0.19870.1. To test I am setting $html = “Hello World”; inside of the Test function right above the return statement and I am not getting any output on the CMS page. I however can confirm it is entering this function because I can echo text from this function and it displays on the page, such as echo “Hello Echo”;.

Also how can these blocks receive query string variables? I am trying to add some sorting functionality to a small data grid display I have.

Any insight?

UPDATE: Never mind, it just started working.... odd. Also as far as receiving q-string variables it seems we can still use the super globals, so that solves my 2nd question. Thanks again!

Thanks
-Chris

 
Magento Community Magento Community
Magento Community
Magento Community
 
bbepristis
Jr. Member
 
Total Posts:  25
Joined:  2008-04-28
Muskegon, Michigan
 

I am trying to do this and so far it seems like the right path for what I need to do I need to allow my custom to upload an image to go onto a t-shirt this is done on the catalog-product-view page when the user is ready to add the item to the cart they will upload an image and then the item will be added to the cart I have followed Moshe’s directions and the form displays fine but when I submit the form it will not post back to the module it posts back to the catalog-product-view page and I cannot seem to figure this out I have tried $_SERVER[’PHPSELF’] (posts to product view) I have hard coded the module and got client denied by server configuration: /var/www/internal_oscommerce/magento_apparel/app/code/loca
l/Mayerwin/Custom/Block/Test.php, referer: http://domain.com/grid/magento_apparel/index.php/default/cotton-t-shirt/Blank-t-shirt.html

if I creat a new php file in the root of the site to handle the upload after submit i get “not found The requested URL /var/www/internal_oscommerce/magento_apparel/index.php was not found on this server.”

I don’t know what else to do. can someone please help me…

Here is my code and Thank you in advance for your help.

<?php

class Mayerwin_Custom_Block_Test extends Mage_Core_Block_Abstract
{
  
protected function _toHtml()
  
{
$html 
"";

if(isset(
$_POST["action"]) == "Upload Image")
{
unset($imagename);

if(!isset(
$_FILES) && isset($HTTP_POST_FILES))
$_FILES $HTTP_POST_FILES;

if(!isset(
$_FILES['image_file']))
$html .= "An image was not found.";


$imagename basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$html .= "The name of the image was not found.";

if(empty(
$error))
{
$newimage 
"/magento_apparel/media/img/" $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name']$newimage);
if(empty(
$result))
$html .= "There was an error moving the uploaded file.";
}

}else{

$html 
.="<div class='box base-mini mini-product-tags'><div class='head'><h4>Upload Custom Design</h4></div><div class='content'><p>Upload Custom Image<form method='POST' enctype='multipart/form-data' name='i
mage_upload_form' action='/grid/magento_apparel/Test.php'><p><input type='file' name='image_file' size='20'></p><p><input type='submit' value='Upload Image' name='action'></p></form>"
;


$html .="</p></form></p></div></div>";
}
   
// put here your custom PHP code with output in $html;
     // use arguments like $this->getMyParam1() , $this->getAnotherParam()

     
return $html;
  
}
}
?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
nicholas.alipaz
Jr. Member
 
Total Posts:  13
Joined:  2008-07-08
Los Angeles, CA
 

I am trying to get this to work and it seems that nothing shows up.  Here is what I have:

This is my file: /www/app/code/local/Mayerwin/Custom/Block/Test.php

<?php

class Mayerwin_Custom_Block_Test extends Mage_Core_Block_Abstract
{
  
protected function _toHtml()
  
{
     $html 
"Hello world";
     echo 
$html;
     return 
$html;
  
}
}
^ I see a missing closing php tag, but I am not sure that is the problem since adding it didn’t fix it.

This is in the backend in my CMS Page:

{{block type="mayerwin_custom/test" my_param1="value 1" another_param="value 2"}}

All other files are exactly as posted by moshe.  Is there something I have done wrong?  Am I misunderstanding?  I am a little confused on the parameters, I removed them and still didn’t get any results.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 
Moshe - 15 March 2008 03:26 PM

Say, you want to run custom PHP code that can accept arguments.

1. declare your module: app/etc/modules/Mayerwin.xml

//code
3. Create your custom PHP code: app/code/local/Mayerwin/Custom/Block/Test.php
//code
4. Use your custom PHP logic in CMS page/block:
//code

Thanks, this is VERY useful - however, I can’t get it to work!
rather than doing this within the CMS content, using something like:

{{block type="myCompany_Events/Events" category_id="5" template="catalog/product/eventview.phtml" }}

I am using the “Layout update XML” so I have this entry:

<reference name="right">
<
block type="myCompany_Leftbar/Leftbar" category="3" template="leftbar/left.phtml" />
</
reference>

Can I not pass a variable using this method??

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 2 of 6