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 1 of 6
How do I use PHP in CMS pages
 
Stromberg
Member
 
Total Posts:  46
Joined:  2008-02-22
Hessen, Germany
 

Hi,

didn’t find anything in Forum or Wiki about using own php-code in a cms-page.
When I try to ad <? code ?> it’s not doing anything.

Thanks for you answers.

Stromberg

 
Magento Community Magento Community
Magento Community
Magento Community
 
JoshBelke
Member
 
Total Posts:  64
Joined:  2008-02-12
NY, NY
 

It seems that everything CMS is saved into the db as a string - in turn are concatenated to form the final output utilizing the structural/content block templating system.

Very strong, very powerful, and also keeps users from hosing the system (so secure) but this makes it harder for you…

Currently, unless i am mistaken, you will need to use their find/replace filtering system in order to make a request within that string and have it detected and replaced.

ex syntax:
calling static blocks…
{{block type="cms/block" block_id="home_static_left" template="cms/content.phtml"}}
{{block type="cms/block" block_id="home_static_right_top" template="cms/content.phtml"}}
{{block type="cms/block" block_id="home_static_right_bottom" template="cms/content.phtml"}}

As far as php.. you should?//will need to make a template… add it to the layout and load it via the same mechanism

So rather than “How do i use PHP in the CMS pages” your real question may be what Syntax already exists and where is a list, if not the location of the code within magento and where can i create my own custom calls.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Stromberg
Member
 
Total Posts:  46
Joined:  2008-02-22
Hessen, Germany
 

Thanks for your answer, but i think i have a different problem. The browser simply doens’t inteprete (don’t know if that word is correct) my code. when i load the page i can still see my code (<?php ... ?> in the source code of the page.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bill007
Sr. Member
 
Avatar
Total Posts:  179
Joined:  2008-01-15
New Zealand
 

If all you see is code then this probably the answer

you need to enable short open tags in your php settings on your server

Post some of what you see

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bill007
Sr. Member
 
Avatar
Total Posts:  179
Joined:  2008-01-15
New Zealand
 

why do you need to put php into the cms anyway

 
Magento Community Magento Community
Magento Community
Magento Community
 
alanin
Member
 
Total Posts:  67
Joined:  2008-02-25
Nordhausen, Germany
 

Hello,

i would be interested in something like that aswell…

We are going to swap to Magento soon and trying to figure out how its working at the moment. We have alot of costumerrelated stuff in our current shop and have to provide that services with magento aswell. So we could add a CMS page and add our PHP Code there and boom - it would be fully integrated in the layout - without hazzling with the whole model…

Sure thats not the best way to implement that kind of stuff - but its an easy way to do…

edit: would it be better to write an complete own module for this?

Al

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

I would be interested in a workaround! Keep me updated if you find something.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mwehrly
Jr. Member
 
Total Posts:  22
Joined:  2008-01-09
U.S.A
 

Not sure if this would work, as I have not tried it.  I am also not sure how this would react when the store is behind https: but would it be possible to create the file.php and then place it into an inline frame?  not sure, might be worth a try.  I am experiencing some difficulties sending a query string to an external database and trying to figure out the easiest way to submit the query string and return results to a CMS page.  Anyway, not sure if it would work, thought I would mention it since it just popped into my head.

matt.

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

I think I have a simple idea grin. We only have to modify the phtml template file used by the CMS page so that using regular expressions, it will recover PHP code that is between dedicated tags (for example {{embeddedPHP}} and {{/embeddedPHP}} and include it as executable code.
I’ll try it someday, but it is clear that an official bot for that would be better.

 
Magento Community Magento Community
Magento Community
Magento Community
 
alanin
Member
 
Total Posts:  67
Joined:  2008-02-25
Nordhausen, Germany
 

how about writing own module and calling the files via the url rewrite stuff? think this would be more magento…

 
Magento Community Magento Community
Magento Community
Magento Community
 
SeL_
Magento Team
 
Avatar
Total Posts:  1140
Joined:  2007-10-10
Paris, France
 

I just read Moshe’s post.
So it looks like we can have php code in phtml file and call this file from CMS block.
I haven’t tried this yet.

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

I got it! It’s still not the perfect way, but better than nothing:
http://www.magentocommerce.com/boards/viewreply/5169/

I have not tested it yet.

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

The above solution from Moshe did not work, neither did my attempt to add “echo” to the CMS HTML content and make PHP code be executed (there was a problem with quotes inside the HTML apparently, and I didn’t manage to solve it, otherwise this would have been a good solution).

However, my friends, I have found a pretty nice workaround, waiting for an official more integrated extension. After that, you’ll be able to put PHP inside the CMS pages just as you would do in any PHP file.

1. Open the template .phtml file that is used for your CMS page or static block. You may find it in /templates/page.

2. Replace

$this->getChildHtml('content')
With :
$contentString $this->getChildHtml('content');
$tmpfname tempnam ("/tmp""FOO");
$fp fopen($tmpfname"w");
fwrite($fp$contentString);
fclose($fp);
 include(
$tmpfname);
unlink($tmpfname);

3. Save and close. You’re done!

This solution creates a temporary file with the whole CMS HTML and PHP code, then include it in the template. Maybe this can lower the performance, that’s why a traditionnal bot like PHP Evaluation Mambot for Joomla would be better.
Also, I have not checked whether there can be a security issue, suppose that the user has written PHP code somewhere in his account, and that this piece of PHP is then evaluated when the page load!

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

I am personally against giving capability entering unsensored PHP from administration panel, this introduces great security risk for store owners.

Do you really need your content administrators enter PHP directly in admin?

Or maybe you could create PHP files as blocks extending Mage_Core_Block_Abstract, overload _toHtml() method and use them in CMS blocks/pages as {{block type="mycustom/php_code"}} ?

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

I understand your point, Moshe, but this is much easier and could be all right if you are the only administrator. I think at least there should be the possibility for the administrator to allow that, even temporarily, so that a migration from existing PHP files would be very quick. However I’m concerned by security issues, that’s why I ask people to be very careful when using a workaround.

The solution you propose interests me a lot (and it requires to have a write access on the server which is good for security), could you explain more precisely how “to extend Mage_Core_Block_Abstract” and “to overload _toHtml() method”?

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

I have another workaround to embed PHP into the CMS or static blocks, which is not much better regarding security, but maybe less resource consuming.

1. Open your .phtml file used as template, which should be in /templates/page

2. Replace:

=$this->getChildHtml('content')
With:
// Evaluated output using kl PHP script
$contentString $this->getChildHtml('content');
$published true// To evaluate or not the PHP code. If turned off, PHP code will be removed.
function botKL_PHP(&$contentString,$published{
    $PHPopenTag 
"<\?php"// or {kl_php}
    
$PHPcloseTag "\?>"// or {/kl_php}
    
$regex "#".$PHPopenTag."(.*?)".$PHPcloseTag."#s";
    
// perform the replacement
    
if($published){
        $contentString 
preg_replace_callback$regex'botKL_PHP_replacer'$contentString );
    
}else {
        $contentString 
preg_replace$regex''$contentString );
    
}
    
return $contentString;
}

function botKL_PHP_replacer( &$matches {
    $kl_debug 
false// Debug mod
    
$kl_entities true//If a WYSIWYG editor is used in the CMS editor, then this should be activated
    
$kl_ob true// Use Output Buffering, useful if echo or print.    
    
    
if ($kl_debug{
        
print 'Debug-KL_PHP:<br />------<br />';
        
print_r($matches[1]);
        print 
'<br />------<br />';
    
}
    
if ($kl_obob_start();
    if (
$kl_entities)
        
$code eval(html_entity_decode(@$matches[1] ));
    else
        
$code eval(@$matches[1]);
    if (
$kl_ob) {
        $buf 
ob_get_contents();
        
ob_end_clean();
        
$code .= $buf;
    
}
    
return $code;
}                            
echo botKL_PHP($contentString,$published);

As you may see, I have been inspired by the original Kl_php bot for Joomla.

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