0

I want to write plugin which has the ability to change the start page according to a get parameter:

The following code is the full plugin.

function GET_Different_Page_on_front_parameter() {
if (!session_id()) {
        session_start();
    }

if(!($_SESSION['start'])){
$_SESSION['start']=$_GET['view'];
    }
    else{}


}

add_action( 'init', 'GET_Different_Page_on_front_parameter');


add_action( 'pre_get_posts', 'redirect_another_homepage');
function redirect_another_homepage(){
$front_page_slug=$_SESSION['start'];
$front_page_id=get_page_by_path($front_page_slug);
$content = $content.$front_page_id->ID;
if(is_home()){
$page=get_permalink($front_page_id->ID);
?>
<script type="text/javascript">
<!--
window.location.href = "<?php echo $page;?>";
//–>
</script>
<?php
}else{}}

I thought it was working yesterday but maybe it just look like it was working.

I also could put you as contributor in WordPress plugin repository.

6
  • Could you clarify a bit what precisely you mean by "start page"? It's not WordPress terminology.
    – Rarst
    Commented Apr 8, 2014 at 20:37
  • normally you set a homepage or your blog post are the front page. so intead of this another page according to the get parameter
    – seot
    Commented Apr 8, 2014 at 20:57
  • But that's not what your code does, it seems to attempt to redirect to different location altogether?
    – Rarst
    Commented Apr 8, 2014 at 20:59
  • but this is why it should do it only on if(is_home())
    – seot
    Commented Apr 8, 2014 at 21:05
  • is_home() is not the home page. It returns true on the blog posts index. For the site front page specifically, use is_front_page(). Commented Apr 10, 2014 at 20:30

2 Answers 2

0

I think you could use the template_redirect hook. I needed to change your code a bit for my own readability:

<?php

/*
Plugin Name: Homepage redirect for SEOT
Plugin URI: 
Description: 
Version: 0.0.1
Author: Douglas L Sesar
Author URI: 
License: GPLv2
*/



function GET_Different_Page_on_front_parameter() {

    if (!session_id()):
            session_start();
    endif;

    if(!($_SESSION['start'])):
        $_SESSION['start']=$_GET['view'];
    endif;

}

add_action( 'init', 'GET_Different_Page_on_front_parameter');


add_action( 'template_redirect', 'redirect_another_homepage');

function redirect_another_homepage(){

    $front_page_slug=$_SESSION['start'];

    $front_page_id=get_page_by_path($front_page_slug);


    $content = $content.$front_page_id->ID;

    if(is_home()):

        $page=get_permalink($front_page_id->ID);

        wp_redirect( $page );
        exit();

    endif;

}

I did not check your session logic, but I think this should work for you (did not test). It should at least give you a direction to try.

6
  • as far as I know I can't use wp_redirect() in this context. it doesn't do anything
    – seot
    Commented Apr 8, 2014 at 21:32
  • try it with a known address string wp_redirect( 'http://'.'www.google.com' ); Commented Apr 8, 2014 at 22:20
  • does not work seems it is doing nothing on template redirect. simply thinks its the wrong action hook
    – seot
    Commented Apr 8, 2014 at 22:35
  • it is almost a copy from codex.wordpress.org/Plugin_API/Action_Reference/… Commented Apr 8, 2014 at 22:45
  • however does not work
    – seot
    Commented Apr 9, 2014 at 22:25
0

I found the solution...crazy to answer my own question but here is the solution http://wordpress.org/plugins/get-different-front-page/

Thanks for your help. Why is_home() not working in this context I still don't know.

function GET_Different_Page_on_front_parameter() {
if (!session_id()) {
        session_start();
    }

}

 add_action( 'init', 'GET_Different_Page_on_front_parameter');
 /* The fucntion GET_Different_Page_on_front_parameter() simply starts a session,
 so that one viewer gets the same homepage every time */


add_filter( 'template_redirect', 'redirect_another_homepage_gdfp');

function redirect_another_homepage_gdfp(){

if(!($_SESSION['start'])){
        $_SESSION['start']=$_GET['view'];
    }
    else{} // this if is capturing the GET parameter of the start

$front_page_slug=$_SESSION['start'];
$front_page_id=get_page_by_path($front_page_slug);

$perma=get_permalink( $front_page_id);
if(is_front_page()){ // I still not know why is_home is not working here

     wp_redirect( $perma);
     exit();

}else{}


}
1
  • "Why is_home() not working in this context I still don't know" - because it doesn't do what you think it does. Please refer to the Template Hierarchy, and also this tutorial. TL;DR use is_front_page() instead. Commented Apr 10, 2014 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.