Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+6 votes
768 views
in Q2A Core by
How to show the sidebar custom HTML module ONLY for registered user only (or editor / moderator)?

Thank you!

1 Answer

+6 votes
by
edited by
 
Best answer

I guess you are looking only for Welcome widget. The first widget (HTML) in sidebar. If so than you can get it with below code

/*
 * Override 'sidepanel' method into your theme or plugin file
 */
public function sidepanel()
{

    // perform check for logged in user
    // if not logged in than unset the sidebar
    if ( ! qa_is_logged_in() )
    {
        unset($this->content['sidebar']);
    }

    //load the default sidepanel
    qa_html_theme_base::sidepanel();

}

If you are looking for all other HTML widget than you can find those into $this->content array. For example for HTML on top (header) the element (key) is body_head etc. So you need to find and unset it with the same way as above.

If you are advanced PHP user than you can use array to perform multiple check at one, which will optimize the code.

Update


Note: Removed ! from qa_is_logged_in so the code will only work for logged in user. ( edited on 09/03/2015 )

public function head_script()
{
    if ( qa_is_logged_in() ) {
        // your javascript
        // see custom-html id on footer()
        $('#custom-html').yourcode
    }
    
    qa_html_theme_base::head_script();
}

public function footer()
{
    /*
     * This is the element (div) where your conent from javascript will load
     * Probably your chatbox markup
     */
    if ( qa_is_logged_in() ) {
        
        // see custom-html id in javascript
        $this->output('<div id="custom-html"></div>');
    }
    
    qa_html_theme_base::footer();

}
by
I'm trying to put the code for the support chat which must be enabled for registered users only. The code I got for this service begins with <script type="text/javascript"> and ends with </script> tags. Thank you!
by
So I guess you have put your javascript code in head_script method. If not than whenever it is.

Wrap the condition to your javascript as well as wrap the condition to the html element which loads html content.

See my updated answer
by
Thank a lot for your help!!!
by
You are welcome..!
by
I'm searching for the method enable service link only for a registered users in the main panel. I'd like to learn how to implement your  code.
Thank you
...