views:

63

answers:

2

I use input format as "PHP Code"...enter PHP code and it looks fine. Next time I go edit it, it removes it all. This is very frustrating.

+3  A: 

The WYSIWYG editor may be hiding the PHP because it is not valid HTML, and looks like a tag. Check the Source view and see if the code is still there, but invisible.

For that matter, you should be editing a PHP text field with a plain text editor, not WYSIWYG, because it may be changing "<" to the HTML entity (& lt;) and so on.

Graham
+1 - the whole point of using a WYSIWYG editor in a web app is to ensure that proper HTML gets inserted in the WYSIWYG enabled field, so it would not do its job if it allowed PHP code to get through unescaped.
Henrik Opel
+2  A: 

Graham's answer explains what's happening, but here's how you might fix it.

But first, caveat: you should really avoid writing PHP in the Drupal web interface, where it gets stored in the database. It's very insecure (you have to be really careful with site permissions) and you don't have the benefit of version control. Consider writing your own small custom modules/themes to get PHP work done.

If you've installed your WYSIWYG using the WYSIWYG module, you can assign a separate configuration of your WYSIWYG for each Input Format. Make sure the "PHP code" input format has no WYSIWYG assigned, and you won't have to worry about Enabled vs Disabled by default. That said, when you start coding in PHP in a text field, you can't switch back and forth between PHP code and an input format that will start the WYSIWYG. This will escape PHP characters as HTML entities, as Graham said, and will destroy your code (I had a client do this to my PHP code in a block, which is another reason not to write PHP within the site UI itself).

If you use the Better Formats module you can assign a default Input Format to each user role you've defined on your site. This is what I do to completely avoid the situation. When I want to do some PHP coding inside Drupal, I use a role that defaults to an input format that doesn't enable my WYSIWYG, so I know I'll never accidentally open a node/block with PHP and have it auto-destroyed by a WYSIWYG editor.

semperos

related questions