Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.0k views
in Q2A Core by
edited by
I need a page with content visible for only registered users.

I created a page in the backend with the name: "My Content Page" for registered users only with everything I need.

The idea is to show a "please register to see this content" section, but let anonymous to see page title and header content.

Anonymous: Please register to see this content.

Registered: Here is the content.

How can I achieve this?

Because I only get the "You do not have permission to perform this operation" alert and I want to tell the users to register to see that content.

Thanks.

1 Answer

+1 vote
by

You didn't tell what kind of page "My Content Page" is? Is it a custom page, or just a question page.

If it is the question page, then you need to override public function q_view_main($q_view) in your theme.

Replace $this->q_view_content($q_view); with

       if (qa_is_logged_in()){
        $this->q_view_content($q_view);
        }
        else {
            $this->output('Please register to see this content.');
        }

For custom pages, or even widget content, you will do the same:

if (qa_is_logged_in()){
        //the custom page or widget content here.
        }
        else {
            echo 'Please register to see this content.' ;
        }

by
It is a normal custom page created in the backend with "visible for users only" option selected.

Can you please extend a bit how to override public function q_view_main($q_view) ?

1- What file to edit with that?
2- How to add that code to my custom page created in the backend?

Thanks.
by
Is it a page created by a plugin, or just a page you create in Admin tab?
by
There are two types of custom pages.
by
Admin > Pages > Add Page.
Visible > Registered Users.

A normal, natural page from the admin, no plugins.

Thanks.
by
For custom pages created in Admin > Pages, the Q2A system didn't even have a separate function or a separate core page. You have to edit Q2A core from this line
https://github.com/q2a/question2answer/blob/7763be86aa74f839f8fad89d30f2130f72716189/qa-include/pages/default.php#L62

I would suggest creating a PAGE plugin, and you can customize anything.
by
edited by
In case you want to edit core file.  (Make sure you set your page visible to anybody).

if (qa_is_logged_in()){
        $qa_content['custom'] = $custompage['content'];
        }
        else {
            return 'Please register to see this content.' ;
        }

This will affect every custom page. If you want to set the view rules to a single specific page, you need its pageid, too. The code will be more complicated. So, again, I would suggest you create a PAGE module plugin.
by
An alternative way is to change the phrase "You do not have permission to perform this operation." into "Please register to see this content." Check your language files if you want to do so.
by
The problem in changing the phrase is that it applies to everything. That is why I need that for this single page, because it will be the only one with content like this. At least for now.

Thank you for sharing that, it seems I will have to wait, I was hoping for an easier solution.

Can you please share about the "PAGE plugin"?

Thanks.
by
Hey, I found another way, but it requires core editing.

https://github.com/q2a/question2answer/blob/1f39ae44adf42c83042be631e39373893a3ec969/qa-include/pages/default.php#L83

Replace this line $qa_content['error'] = qa_lang_html('users/no_permission');

with

$qa_content['error'] =  'Please register to see this content.';
by
But how will that affects only that page?

Because that message is needed for other permission alerts.

Thanks.
by
You didn't test, right?
by
I will try other day with more time to see if I make this work. Too much for a simple task.

Thank you.
by
Anyway, this is the last trick, override this function in your theme.

public function initialize() {

 if ($request=='your-custom-page' ){

 $qa_content['error'] =  'Please register to see this content.';

}
    parent::initialize();
}
by
That looks much more better! Thank you!

Can you just please let me know which file to override in?
by
In your theme. There's a file named qa-theme.php in your theme folder. But I don't think the last trick will work. Best to create a seperate custom page.
by
If you are not keen on coding, you can change the language file into "You either need to register to see this content or you don't have the permission to do so."  Something like that.
by
Thank you for the info.
...