Allow users to choose homepage

Status
Not open for further replies.

Divvy

Active Member
806
2009
18
0
Hello guys,

Is there any plugin that allow users to choose the page that they want to see as homepage?

Let me try to explain what I'm looking for:

I have a wordpress site with "SFW" posts in homepage. (/)
And I have a 2nd page with "NSFW" posts. (/nsfw)

What I'm looking is an option for users to choose the page they want as homepage, something like "on/off", example:

ON: SFW content (default)
OFF: NSFW content

Can someone help me please? :)
 
15 comments
Hey Rox,

Thank you for your reply and for trying to help me.

Something like a switch:
SFW: ON/OFF

If ON, will show default homepage
If OFF, will show the page domain.com/category/nsfw/
 
Hey bud,

Thanks but unfortunately doesn't work like I need. Anyway, I already have that plugin option in my theme to exclude categories from frontpage.
Just need to allow my users to choose what page they want as homepage.

Thank you anyway :)
 
You need something a bit like LiveLeak... Safe Mode On and Safe Mode Off.... It appears to be Javascript. At least on that website anyway.

Clicking on the LiveLeak version, you either get redirected to:

https://www.liveleak.com/?safe_mode=on
Or
https://www.liveleak.com/?safe_mode=off

How it remembers this setting when you revisit liveleak.com (without the extra syntax), I don't know?? I suspect it might be using cookies or something??

I know it's not really a helpful response, but hopefully it might give you some idea of how at least liveleak does it.

Coding bit of their button:

Code:
<a href="javascript://" class="red_button" style="position:fixed; top:0px; right:0px;" onclick="if(confirm('Turning the safe mode off will prevent mature preview images and avatars from showing up. Are you sure you want to turn the safe mode ON?')){ var url_part = '?'; current_url = window.location.href.replace(/[?&amp;]safe_mode=(on|off)/g, ''); if(current_url.indexOf('?') != -1)url_part = '&amp;'; window.location.href = current_url + url_part + 'safe_mode=on';}" title="The safe mode is currently off, making mature preview images and mature avatars visible.">Safe Mode: Off</a>
 
You can do it using something like a a query var, or a cookie condition in the home page template as conditions on WP Query. I imagine you might want to set conditions to either include/exclude the NSFW posts in a loop, assuming that both the SFW and NSFW posts are of the same post_type. Or just use another loop for NSFW exclusive homepage when the condition is met, using php's if/else statement.
 
Last edited:
You need something a bit like LiveLeak... Safe Mode On and Safe Mode Off.... It appears to be Javascript. At least on that website anyway.

Clicking on the LiveLeak version, you either get redirected to:

https://www.liveleak.com/?safe_mode=on
Or
https://www.liveleak.com/?safe_mode=off

How it remembers this setting when you revisit liveleak.com (without the extra syntax), I don't know?? I suspect it might be using cookies or something??

I know it's not really a helpful response, but hopefully it might give you some idea of how at least liveleak does it.

Coding bit of their button:

Code:
<a href="javascript://" class="red_button" style="position:fixed; top:0px; right:0px;" onclick="if(confirm('Turning the safe mode off will prevent mature preview images and avatars from showing up. Are you sure you want to turn the safe mode ON?')){ var url_part = '?'; current_url = window.location.href.replace(/[?&amp;]safe_mode=(on|off)/g, ''); if(current_url.indexOf('?') != -1)url_part = '&amp;'; window.location.href = current_url + url_part + 'safe_mode=on';}" title="The safe mode is currently off, making mature preview images and mature avatars visible.">Safe Mode: Off</a>

Thank you bud, that's exactly what I need.
Unfortunately I'm too newbie to do such thing lol, any idea? ;P
 
Last edited:
You need to provide more information. For example, what are the differences between the NSFW and SFW posts you have? Are they in different post_type or is in the same post_type but the NSFW posts have their own category?
 
NSFW posts have their own categories.

So basically:

SFW (default): Categorysfw1, Categorysfw2, Categorysfw3, ...
NSFW: Categorynsfw4, Categorynsfw5, Categorynsfw6, ...
 
Put this in your theme's function.php

PHP:
function nsfw_query_filter( $query ) {
    // Check if in the main query of post index
    if( $query -> is_home() && $query -> is_main_query() ) {


        // Non-associative array of NSFW Category IDs
        $nsfw_cats = array( 13, 15, 25 );


        // Filter by taxonomy & cookie
        if( isset( $_COOKIE['nsfw'] ) && $_COOKIE['nsfw'] == 'on' )
            $opr = 'IN';
        else
            $opr = 'NOT IN';

        $tax_filter = array(
             array(
                 'taxonomy' => 'category',
                 'field' => 'id',
                 'terms' => $nsfw_cats,
                 'operator'=> $opr
             )
        );

        $query -> set( 'tax_query', $tax_filter );
    }
    return;
}
add_action( 'pre_get_posts', 'nsfw_query_filter' );


then put this in your theme's index.php for the button
PHP:
if( isset( $_COOKIE['nsfw'] ) && $_COOKIE['nsfw'] == 'on' )
    echo '<button id="nsfw-switch" type="button" rel="off">Toggle SFW</button>';
else
    echo '<button id="nsfw-switch" type="button" rel="on">Toggle NSFW</button>';


lastly, you can either put this jquery code in the index.php, concatenate it in your theme's already existing .js file, or in its own .js file and enqueue it using WP's wp_enqueue_script()
Code:
<script> // Remove the script tags when putting the code in a .js file
var date = new Date;

date.setDate( date.getDate() + 30 ); //set your expiry date in days

jQuery('#nsfw-switch').click(function(){
    var cookie = jQuery(this).attr('rel');
    document(.)cookie = 'nsfw=' + cookie + '; expires=' + date + '; path=/';
    location.reload();
});
</script>

NOTES:

  • I'm not sure why but WJ seems to block the document(.)cookie word so I had to parenthesize the dot. Just remove the parentheses.
  • I used more generic lines of code like tax_query and two separate php echo for the button to make them more flexible to any changes.
  • I assumed that your entries are Posts. (i.e. post_type = 'post')
  • I also assumed that your front page is the main post index (index.php).
  • Lastly, I wrote but haven't tried the code myself so you might want to try it first on a local installation to make sure if it's working.

EDIT: Corrected some mistakes in the code.
 
Last edited:
WOW Thank you very much bud! :D

I added everything in my wordpress theme like you said, but for some reason, the button is not working.
When I click in the button, the page reloads, but the switch doesn't change.

Any idea how to fix it? :)

I added the javascript correctly and replaced the document(.)cookie with documentDOTcookie
 
WOW Thank you very much bud! :D

I added everything in my wordpress theme like you said, but for some reason, the button is not working.
When I click in the button, the page reloads, but the switch doesn't change.

Any idea how to fix it? :)

I added the javascript correctly and replaced the document(.)cookie with documentDOTcookie

Try seeing the browser's developer tools (F12 in google chrome) for any error messages. Try using another variable for the 'cookie' to see if it fixes the issue like this. If you put the jQuery code directly in the index.php just be sure that it is wrapped in the <script></script> tags and that the button code always comes first.
 
Last edited:
Hey bud,

I already checked but nothing appears in the console.
I added your code in a test site, I'm going to send you login details for you to check, if possible :)
 
Status
Not open for further replies.
Back
Top