The Nicepage WYSIWYG editor offers the insertion of a block of PHP code in a page, but unfortunately, this works directly only for exportation to Joomla and WordPress. There is no native way to create working normal HTML pages with PHP code.
There are several reasons why this does not work.
One is that the double quotes in the code are all translated into their HTML code which makes the PHP parser throw an error immediately when the server processes the page. Another is that the page will not show any output if it is run with the HTML extension. The most obvious solution is to create a separate PHP page and use links to it from other Nicepage pages, but this does not allow you to take advantage of many features that Nicepage offers, like the uniform headers and footer throughout the entire website, the ease to create multilanguage sites, and others.
I will describe here how I integrated a PHP page into the system with the same feeling and look as the other pages.
I will admit that, although not new to programming, I am a newbie to Nicepage and PHP, and have nearly no knowledge of Java. and I am not super efficient in HTML either, as I don't have a good memory anymore and often need to look up commands, tags, and examples for the tasks that I want to accomplish. So bear with me if my methods may not be the most elegant in your eyes, There is always room for improvement and I will be happy to listen to advice. Meanwhile, this method overcomes a dire need for using PHP in Nicepage at a reasonable cost in work time.
A short description of my project to understand the background.
As the owner of a small apartment hotel in Panama, I am creating a multilanguage website in 5 languages in Nicepage, after having created another one with another system that has some advantages, but more disadvantages.
The main tasks of the website are presenting our apartments to future guests, allowing them to know whether there is an apartment available, telling them how much it costs, and letting them book it.
While we can, and do, use OTAs (online travel agencies) and a channel manager to show the availability and let them book an apartment. I was not satisfied with the price calculation. We offer continuously increasing discounts for longer stays, and none of the OTAs can handle this pricing system. Our channel manager can do such a calculation but fails when different seasonal rates have to be considered.
So, I needed a webpage that calculates the correct price for our algorithm. Creating it in PHP was not a big deal, but when I tried to give it the look of my website with its header and footer, I failed. Unless you use the very same code and CSS of the original pages, it is a major PITA to achieve the same look, not even talking about functionality. So, of course, the PHP block was my savior ... or so I hoped. But unfortunately, it does not work if you export a PHP page to HTML. After a couple of days of research, trial, and error, I have found the solution, though. Here is how it goes.
- Create a Nicepage page with an empty PHP block (let's say the name is "NewForm")
- Insert your PHP code.
- Export to HTML. (You cannot test this page in the preview)
- Rename the exported file "newform.html" to "newform.php". The file will not work with the HTML extension
- Edit the PHP code, replacing all occurrences of "& quote;" with a double quote character. With an editor like Atom, this takes just a few seconds. PHP will not work if you don't replace the symbol. (I had to add a space here because this editor would otherwise replace the code with a blank)
- Check the code if there are cases of quoted elements within quoted elements and replace one set of double quotes with single quote characters.
- Upload the PHP file to the root directory of your website server and tweak the code, as some elements may not show as planned, due to the Nicepage CSS. I used the ATOM editor and the Chrome development tools, which are excellent for this.
- For multilanguage sites, Nicepage copies the file in the base language to the root, creates language subdirectories and automatically copies your files for other languages there with the extension .html. These files are slightly different for each language because of the different headers, If you add this page to your menu, the link will automatically have the extension .html and point to the various directories where Nicepages deploys your file each time you export it. So, you will need to take care that the menu points to the PHP files instead.
If you need to tweak your PHP code, there are several methods to make sure it works in all languages.
To avoid repeating the same procedure for every little change, you would do this directly in the exported HTML file in the root and test it there, by simply uploading the changed file and watching the effect in the browser, which can be done in mere seconds.
If you have only one language, you are done. Otherwise, extract the PHP part and copy it into the code for the other languages. Unless you use PHP also to adapt the header code automatically to the different languages, just copying the entire file from the root to the subdirectory does not do the job because the header code is slightly different for each language due to the language selector.
If you need to change the header or footer later on, these files will of course not reflect the changes unless you repeat the entire procedure with the optimized PHP code, or you just copy the changed header and footer code, taken from any of your other pages, into your exported files. Remember that for multilanguage sites, the header is different for each language.
Hopefully, the company will introduce the necessary changes someday soon to avoid this additional procedure. IMO, this should not be difficult, and this description could work as a guide.
These are the basic steps. As I have limited time right now, I will edit this post later and add more details, especially about how to handle multilanguage sites. Feel free to post comments or questions which I may consider when completing this post.
The Nicepage WYSIWYG editor offers the insertion of a block of PHP code in a page, but unfortunately, this works directly only for exportation to Joomla and WordPress. There is no native way to create working normal HTML pages with PHP code. There are several reasons why this does not work. One is that the double quotes in the code are all translated into their HTML code which makes the PHP parser throw an error immediately when the server processes the page. Another is that the page will not show any output if it is run with the HTML extension. The most obvious solution is to create a separate PHP page and use links to it from other Nicepage pages, but this does not allow you to take advantage of many features that Nicepage offers, like the uniform headers and footer throughout the entire website, the ease to create multilanguage sites, and others. I will describe here how I integrated a PHP page into the system with the same feeling and look as the other pages. I will admit that, although not new to programming, I am a newbie to Nicepage and PHP, and have nearly no knowledge of Java. and I am not super efficient in HTML either, as I don't have a good memory anymore and often need to look up commands, tags, and examples for the tasks that I want to accomplish. So bear with me if my methods may not be the most elegant in your eyes, There is always room for improvement and I will be happy to listen to advice. Meanwhile, this method overcomes a dire need for using PHP in Nicepage at a reasonable cost in work time. A short description of my project to understand the background. As the owner of a small apartment hotel in Panama, I am creating a multilanguage website in 5 languages in Nicepage, after having created another one with another system that has some advantages, but more disadvantages. The main tasks of the website are presenting our apartments to future guests, allowing them to know whether there is an apartment available, telling them how much it costs, and letting them book it. While we can, and do, use OTAs (online travel agencies) and a channel manager to show the availability and let them book an apartment. I was not satisfied with the price calculation. We offer continuously increasing discounts for longer stays, and none of the OTAs can handle this pricing system. Our channel manager can do such a calculation but fails when different seasonal rates have to be considered. So, I needed a webpage that calculates the correct price for our algorithm. Creating it in PHP was not a big deal, but when I tried to give it the look of my website with its header and footer, I failed. Unless you use the very same code and CSS of the original pages, it is a major PITA to achieve the same look, not even talking about functionality. So, of course, the PHP block was my savior ... or so I hoped. But unfortunately, it does not work if you export a PHP page to HTML. After a couple of days of research, trial, and error, I have found the solution, though. Here is how it goes. - Create a Nicepage page with an empty PHP block (let's say the name is "NewForm") - Insert your PHP code. - Export to HTML. (You cannot test this page in the preview) - Rename the exported file "newform.html" to "newform.php". The file will not work with the HTML extension - Edit the PHP code, replacing all occurrences of "& quote;" with a double quote character. With an editor like Atom, this takes just a few seconds. PHP will not work if you don't replace the symbol. (I had to add a space here because this editor would otherwise replace the code with a blank) - Check the code if there are cases of quoted elements within quoted elements and replace one set of double quotes with single quote characters. - Upload the PHP file to the root directory of your website server and tweak the code, as some elements may not show as planned, due to the Nicepage CSS. I used the ATOM editor and the Chrome development tools, which are excellent for this. - For multilanguage sites, Nicepage copies the file in the base language to the root, creates language subdirectories and automatically copies your files for other languages there with the extension .html. These files are slightly different for each language because of the different headers, If you add this page to your menu, the link will automatically have the extension .html and point to the various directories where Nicepages deploys your file each time you export it. So, you will need to take care that the menu points to the PHP files instead. If you need to tweak your PHP code, there are several methods to make sure it works in all languages. To avoid repeating the same procedure for every little change, you would do this directly in the exported HTML file in the root and test it there, by simply uploading the changed file and watching the effect in the browser, which can be done in mere seconds. If you have only one language, you are done. Otherwise, extract the PHP part and copy it into the code for the other languages. Unless you use PHP also to adapt the header code automatically to the different languages, just copying the entire file from the root to the subdirectory does not do the job because the header code is slightly different for each language due to the language selector. If you need to change the header or footer later on, these files will of course not reflect the changes unless you repeat the entire procedure with the optimized PHP code, or you just copy the changed header and footer code, taken from any of your other pages, into your exported files. Remember that for multilanguage sites, the header is different for each language. Hopefully, the company will introduce the necessary changes someday soon to avoid this additional procedure. IMO, this should not be difficult, and this description could work as a guide. These are the basic steps. As I have limited time right now, I will edit this post later and add more details, especially about how to handle multilanguage sites. Feel free to post comments or questions which I may consider when completing this post.Last edited 29 August 2022 by v.piasta