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.
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.
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.
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?
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.
I think I have a simple idea . 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.
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.
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!
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"}} ?
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”?
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.