Use preview switch in your code
12345678910// This switch is not required to use the preview mode
// but can be used as an additional means of hiding
// or showing some features
switch(true){
case !$USER->login():
echo "Log in to use preview mode"; break;
case $PAGE->preview():
echo "Use the controller at the bottom of the page switch mode"; break;
default:
echo 'Click to enter <a href="' .$PAGE->preview(true). '">preview mode</a>'; break;}
Log in to use preview mode