Call-back icon  Enterprise Sales: +1.310.775.2674 (N. America)   +44 20.3286.4137 (UK)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Page 1 of 5
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
 
josh_trivera
Member
 
Avatar
Total Posts:  62
Joined:  2008-02-12
Wisconsin, USA
 

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:  115
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

 Signature 

http://www.billbailey.co.nz Blog about interesting things
http://www.freeblog.co.nz Free Blogging Platform
“Life is about People and the way we treat them”

Regards Bill007

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

why do you need to put php into the cms anyway

 Signature 

http://www.billbailey.co.nz Blog about interesting things
http://www.freeblog.co.nz Free Blogging Platform
“Life is about People and the way we treat them”

Regards Bill007

 
Magento Community Magento Community
Magento Community
Magento Community
 
alanin
Member
 
Total Posts:  64
Joined:  2008-02-25
Meuselwitz, 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:  166
Joined:  2008-01-15
France
 

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

 Signature 

I have finally found the best web hosting ever for Magento! 1 second max pageload with my website, for less than 15€ a month… And a very good support. I strongly advise you to try them : Rackspeed.de, there is even a 7 days free trial.
Erwin Mayer Foundation

 
Magento Community Magento Community
Magento Community
Magento Community
 
mwehrly
Jr. Member
 
Total Posts:  17
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:  166
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.

 Signature 

I have finally found the best web hosting ever for Magento! 1 second max pageload with my website, for less than 15€ a month… And a very good support. I strongly advise you to try them : Rackspeed.de, there is even a 7 days free trial.
Erwin Mayer Foundation

 
Magento Community Magento Community
Magento Community
Magento Community
 
alanin
Member
 
Total Posts:  64
Joined:  2008-02-25
Meuselwitz, 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:  1031
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.

 Signature 

Blog Magento en français | Wiki français | Traduction française de Magento (sa documentation et son fil de discussion)

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  166
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.

 Signature 

I have finally found the best web hosting ever for Magento! 1 second max pageload with my website, for less than 15€ a month… And a very good support. I strongly advise you to try them : Rackspeed.de, there is even a 7 days free trial.
Erwin Mayer Foundation

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  166
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!

 Signature 

I have finally found the best web hosting ever for Magento! 1 second max pageload with my website, for less than 15€ a month… And a very good support. I strongly advise you to try them : Rackspeed.de, there is even a 7 days free trial.
Erwin Mayer Foundation

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1771
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"}} ?

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  166
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”?

 Signature 

I have finally found the best web hosting ever for Magento! 1 second max pageload with my website, for less than 15€ a month… And a very good support. I strongly advise you to try them : Rackspeed.de, there is even a 7 days free trial.
Erwin Mayer Foundation

 
Magento Community Magento Community
Magento Community
Magento Community
 
mayerwin
Sr. Member
 
Avatar
Total Posts:  166
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.

 Signature 

I have finally found the best web hosting ever for Magento! 1 second max pageload with my website, for less than 15€ a month… And a very good support. I strongly advise you to try them : Rackspeed.de, there is even a 7 days free trial.
Erwin Mayer Foundation

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 5
 
© Copyright 2009 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
108662 users|404 users currently online|199735 forum posts