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

How do I use PHP in CMS pages
 
collinssolutions
Sr. Member
 
Total Posts:  79
Joined:  2008-07-09
USA Arkansas
 

how do you extend blocks, i really need a page with some php in it

I have tried the little tutorial in this thread but it dosent work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
collinssolutions
Sr. Member
 
Total Posts:  79
Joined:  2008-07-09
USA Arkansas
 

Ok here is my code that dosent work

etc/modules/eBayAPI.php

<?xml version="1.0" encoding="utf-8"?>
<config>
  <
modules>
    <
\eBayAPI_Custom>
      <
active>true</active>
      <
codePool>local</codePool>
    </
eBayAPI_Custom>
  </
modules>
</
config>

app/code/local/eBayAPI/Custom/etc/config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
  <global>
    <
blocks>
      <
eBayAPI_custom>
        <class>
eBayAPI_Custom_Block</class>
      </
eBayAPI_custom>
    </
blocks>
  </global>
</
config>

app/code/local/eBayAPI/Custom/Block/eBay.php

<?php

class eBayAPI_Custom_Block_eBay 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()
     
$html 'Hello World';

     return 
$html;
  
}
}
?>

I go to my CMS and make a new page and place this in the body

{{block type="eBayAPI_custom/eBay" my_param1="value 1" another_param="value 2"}}

Nothing Happens, Any Ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
collinssolutions
Sr. Member
 
Total Posts:  79
Joined:  2008-07-09
USA Arkansas
 

Oh i had to delete my above files. it crashed magento.

 
Magento Community Magento Community
Magento Community
Magento Community
 
pastanislas
Member
 
Total Posts:  46
Joined:  2008-07-29
 
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

<?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"}}

Thank you so much ! Works perfect for me on 1.1.1 !!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
rEda
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2008-08-08
 

Hi,

i try this code on Magento 1.1.2, but it doesn’t work
Please help me blank stare

Thank you

 
Magento Community Magento Community
Magento Community
Magento Community
 
radalin
Jr. Member
 
Total Posts:  29
Joined:  2008-09-01
Istanbul, Turkey
 

I have tried this on 1.1.3 and it does not work either?

Something has changed during this time? Some help is really appreciated.

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
radalin
Jr. Member
 
Total Posts:  29
Joined:  2008-09-01
Istanbul, Turkey
 
vgration - 06 August 2008 08:02 AM

Ok here is my code that dosent work

etc/modules/eBayAPI.php

<?xml version="1.0" encoding="utf-8"?>
<config>
  <
modules>
    <
\eBayAPI_Custom>
      <
active>true</active>
      <
codePool>local</codePool>
    </
eBayAPI_Custom>
  </
modules>
</
config>

[...]

Can this be because of you have mistyped

<\eBayAPI_Custom>
<active>true</active>
<codePool>local</codePool>
</eBayAPI_Custom>

instead of

<eBayAPI_Custom>
<active>true</active>
<codePool>local</codePool>
</eBayAPI_Custom>

You have added a backslash there. Maybe this is the cause.

 
Magento Community Magento Community
Magento Community
Magento Community
 
radalin
Jr. Member
 
Total Posts:  29
Joined:  2008-09-01
Istanbul, Turkey
 

Finally I have made it work on version 1.1.3. It was because of caching system. I guess lot of people were missing this point. You have disable/refresh caches once you edit your code, so that the newly edited pages comes, else the cached page will come.

hope that helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
artmechanix
Member
 
Total Posts:  39
Joined:  2008-08-07
 

Does anyone happen to know how I can go about trying to put a PHP include statement into the CMS pages?

I tried Moshe’s solution, but it resulted in a parse/syntax error, as it seems meant for PHP of a different sort.

I’m trying to install an older version of Frontpage Slideshow on the home page, but continue to get a blank space where the php include is inserted.  Worst case scenario, I’ll put it on an html/php page separately and call it up with an iFrame, but I’m trying to avoid doing that.

Thanks very much for any tips…

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

I’m also having problems implementing this. Here is the code I am using…

app\etc\modules\Ben_Custom.xml

<?xml version="1.0"?>
<config>
  <
modules>
    <
Ben_Custom>
      <
active>true</active>
      <
codePool>local</codePool>
    </
Ben_Custom>
  </
modules>
</
config

app\code\local\Ben\Custom\etc\config.xml

<?xml version="1.0"?>
<config>
  <global>
    <
blocks>
      <
ben_custom>
        <class>
ben_custom_block</class>
      </
ben_custom>
    </
blocks>
  </global>
</
config>

app\code\local\Ben\Custom\Block\Test.php

<?php

class Ben_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;
  
}

?>

(tried the Test.PHP above with and without the trailing ‘?>’ that wasn’t on Moshe original post but it makes no difference)

Any thoughts? I’m running 1.1.6.

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Also, I tried sticking with the Mayerwin naming convention of the original solution Moshe posted but I get the following error:

Notice: Undefined variable: html in /home/public_html/magento/app/code/local/Mayerwin/Custom/Block/Test.php on line 10

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

Your app\etc\modules\Ben_Custom.xml is missing a “>”. The last one. Take a look wink

BTW, if you changed the naming to the original Mayerwin, be careful to rename all the files and directories accordingly wink

The original Ben_Custom should work just fine.

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

DOH! Sicarius, thanks for that. Managed to get it working now! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
benfrain.com
Sr. Member
 
Total Posts:  142
Joined:  2008-08-15
 

Please forgive my ignorance as I am a beginner when it comes to PHP…

The ‘Hello World’ example is now working so I would now like to add a PHP script from alistapart.com that allows me to rotate images and target urls on the front page. I have used this script on other non-magento sites before so I know it works fine. However, when I insert it into the Test.php file and reload the page I get this error:

Parse error: syntax error, unexpected $end in /home/site/domains/site.com/public_html/magento/app/code/local/Mayerwin/Custom/Block/Test.php on line 88

Here is my Test.php file - I know I’ve got the code wrong somehow - is it the ?> end etc?

<?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()
      

/*

  Author: Dan Benjamin - http://hivelogic.com/

  Copyright (c) 2004 Automatic, Ltd. All Rights Reserved.

  THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
  ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
  IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
  PURPOSE OR NONINFRINGEMENT.  

  IN NO EVENT SHALL DAN BENJAMIN, A LIST APART, OR AUTOMATIC, LTD. BE LIABLE
  FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR
  LOST PROFITS ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER
  ARISING, INCLUDING NEGLIGENCE), EVEN IF DAN BENJAMIN, A LIST APART, OR
  AUTOMATIC, LTD. IS AWARE OF THE POSSIBILITY OF SUCH DAMAGES.

*/



  # file containg your image descriptions

  
$IMG_CONFIG_FILE 'http://www.site.com/magento/skin/frontend/bf/bf/images/media/images.ini';



  
# You shouldn't need to change anything below this point

  
function showImage$ini=null {
    
global $IMG_CONFIG_FILE;
    
# if no custom ini file has been specified, use the default
    
$ini_file $ini $ini $IMG_CONFIG_FILE;
    
# read the config file into an array or die trying
    
$images = @parse_ini_file($ini_file,true);
    if (! 
$images) {
      
die('Unable to read ini file.');
    
}
    
# pick a random image from the parsed config file
    
$img array_rand($images);
    
# get the selected image's css id if one exists
    
$id $images[$img]['id'?
      
sprintf' id="%s" '$images[$img]['id') :
      
'';
    
# get the selected image's css class if one exists
    
$class $images[$img]['class'?
      
sprintf' class="%s" '$images[$img]['class') :
      
'';
    
# get selected image's dimensions
    
$size = @getimagesize$images[$img]['src');
    
# if an url was specified, output the opening A HREF tag
    
if ( $images[$img]['url'{
      printf
(
        
'<a href="%s" title="%s">',
        
$images[$img]['url'],
        
$images[$img]['title']
      
);
    
}
    
# output the IMG tag
    
printf(
      
'<img src="%s" alt="%s" %s %s%s/>',
      
$images[$img]['src'],
      
$images[$img]['alt'],
      
$size[3],
      
$id,
      
$class
    
);
    
# if an url was specified, output the closing A HREF tag
    
if ( $images[$img]['url'{
      
echo('</a>');
    
}
  }

    ?>    

     
return $html;
  
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Sicarius
Jr. Member
 
Total Posts:  9
Joined:  2008-09-20
 

Change this:

?>    

     
return $html;
  
}
}

Into this:

return $html;
  
}
}
?>

Why? Because all your PHP code must be inside PHP Tags. “?>” is the closing tag.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top