How-To: Create a Custom Page for Logged in users Only in Oxwall

Aliia of the Oxwall forums has released a great solution for everyone looking to make a Custom Page that is accessible only to authenticated users on your Oxwall website! When you’re done with this tut, make sure to thank her!

This tutorial will leave you with a single custom page available to everyone in the menu, but will change the destination depending on who clicked it. If a guest on your site clicks it, they will be taken to the sign-in page and then redirected back to the page. If the user is signed-in, they will access the page. Remember that this will be a plugin and not a mod, good thing to keep in mind. Let’s get started.

Note: If you need a sign-in page or registration page that is similar to Facebook’s, you can simply purchase the Facebook Clone Plugin and customize it to your taste. It’s safer to use because the pages will not be affected by platform update.

1. on  your computer create new folder with the name of your future plugin. Example: “custompage”.

2. within this folded create  plugin.xml  file. More information on what you need to add within this file can be found here: http://docs.oxwall.org/dev:crash-course
Example:

<?xml version="1.0" encoding="UTF-8"?>

<plugin>

    <name>Custom Page</name>

    <key>custompage</key>

    <description>custom page description</description>

    <author>Alia</author>

    <authorEmail>[email protected]</authorEmail>

    <authorUrl>http://oxwall.org</authorUrl>

    <developerKey>your_key_here</developerKey>

    <build>1</build>

    <copyright>(C)All rights reserved.</copyright>

    <license>The BSD License</license>

    <licenseUrl>http://www.opensource.org/...p</licenseUrl>

</plugin>

3.Within your folder create following files: install.php, uninstall.php, init.php, activate.php and deactivate.php.

Leave all files except install.php empty for now.
Add following piece of code into install.php:


<?php



BOL_LanguageService::getInstance()->addPrefix('custompage', 'My Custom Page');

4. Compress your folder into .zip archive
5. Go to your admin panel>>manage plugins>>add new plugin and install your custom plugin, just like any other plugin  you install.
6. At this stage, plugin is insatlled and all needed details are automatically added into the database, but plugin is not doing anything yet – is is just there.
Now it is time to add some functionality:
7.Open your plugin’s folder directly on the server using FTP or file manager.
Within your plugins’ folder create two folders: controllers and views.Create one more controllers folder in the views folder.

8. Let’s foget about  “Views” folder for now. Open “controllers” folder and create our first contoller there. Let’s name it “custom.php“.
This file’s content should be as follows:

class PLUGINKEY_CTRL_Nameofcontroller extends OW_ActionController

{

}

Example:

<?php



class CUSTOMPAGE_CTRL_custom extends OW_ActionController

{

}

You can see that we are using our pluginkey from plugin.xml file in uppercase letters here. Also, note that name of our file “custom.php” and name of the controller (CUSTOMPAGE_CTRL_custom) should be the same.

More information on naming rules can be found here: http://docs.oxwall.org/dev:crash-course ( under “Creating a page” section).

9. Let’s create the first action inside of the class:

<?php



class CUSTOMPAGE_CTRL_custom extends OW_ActionController

{

public function index()

{

$this->setPageTitle("My New Custom Page");

$this->setPageHeading("Custom Page");

}

}

10. For our page to display correctly we should assign a view for the action. For that we need to create an empty file custom_index.html in the views/controllers/ folder. As you can see, the view name contains of the controller file name ( “custom”) and the action name (“index
) separated by an underscore.

11. Finally, we can take a look at our page. The URL of the page looks like this: <domain>/custompage/custom/index
Basically you will see an empty page with the page title. It is up to you to add your custom content within custom_index.html file.

12. The URL looks rather lengthy. No worries though – Oxwall supports nice urls. For our page to be available from a shorter URL (from example, <domain>/custom) we should add the following line to the previously created init.php file:

<?php



OW::getRouter()->addRoute(new OW_Route('custompage.index', 'custompage', "CUSTOMPAGE_CTRL_Custom", 'index'));

Parameters:
route name, custompage.index
path
controller class name
name of the action the route points to.
Now our page open under <domain>/custompage URL.

13. To make the page accessible, let’s add a link to the page within the main menu. For that we should first create main_menu_custom key in Languages with the prefix of our plug-in, and ‘My Cool Title’ as the value. To add new key open your admin panel using dev-tools : www.sitename.com/admin/dev-tools/languages >>click on “Add New Text”>> section: select your plugin’s name within the drop down>> key:main_menu_custom >>text: any text.

14. Once key is added, add the following code to the activate.php file of your plugin:

<?php



OW::getNavigation()->addMenuItem(OW_Navigation::MAIN, 'custompage.index', 'custompage', 'main_menu_custom', OW_Navigation::VISIBLE_FOR_ALL);



Also add following code into deactivate.php:

<?php

OW::getNavigation()->deleteMenuItem('custompage', 'main_menu_custom');

15. From admin panel>>manage plugin>> deactivate and activate your plugin. Now open your site’s homepage. You will see that new menu item for your custom page was added.

16. And we are almost done. At this stage any user can open your new page and see it. Now we need to add the code that checks whether user is loged in or not before showing our custom page to him.
Within the controllers/custom.php file you have created add following piece code:


if ( !OW::getUser()->isAuthenticated() )

{

throw new AuthenticateException();

}

Overall you should have:


<?php



class CUSTOMPAGE_CTRL_custom extends OW_ActionController

{

public function index()

{

$this->setPageTitle("My New Custom Page");

$this->setPageHeading("Custom Page");



if ( !OW::getUser()->isAuthenticated() )

{

throw new AuthenticateException();

}



}



}

17. And we are done. Now when guest user tried to open your custom page, he will get a log in form. Already loged in users will be able to see the content of your page right away.

Well, 17 steps!!! This sounds a bit complicated, but once you do this at least one time – things will be easy. And make sure to check http://docs.oxwall.org/dev:crash-course while doing every step from this forum topic, since this doc explains things in more details, while I am giving just a brief description.

Did you get the answer you were searching for?

Save hours of searching online or wasting money testing unnecessary plugins, get in touch with me and let's discuss a suitable plan for your project. Best thing about this service is that you are never placed on hold and get to talk to an expereinced Oxwall/Skadate developer.

Get Answers for Free!

Ask a question related to this topic and get immediate answers from other community members in 48hrs or less. Contribute by answering members questions.

Ask Question
Premium Service

Whether it's a custom plugin, theme or dedicated support needed to get you started on your project, get professional 24/7 support tailored to your need.

Get in Touch

Or just leave a comment...

Source code of the plugin described in this topic is attached for your reference.
Once more it is only up to you to add your custom content within custom_index.html file.

Leave a Reply