Jump to content

Redirect to front-end on page save


onjegolders
 Share

Recommended Posts

I'm trying to create a simple module that on pages of a certain template, when they are saved, redirect to the front-end. 

The thinking here is that I have created simple front end buttons on a blog to add either a "gallery post" or a "blog post" (different template same parent). I don't really want to handle the uploading of images part in the front-end and so would like to use the admin for the post creation but I would like the user to return instantly to the front end when they have added their new post.

When they click the relevant front-end button, the relevant page gets created with the required template. They get sent to the newly created admin page where they can add their content. 

Front end bit:

<?php

if ($user->isSuperuser() && $input->get->type == "blog") {

	$new_post = new Page();
	$new_post->of(false);
	$new_post->parent = $page;
	$new_post->template = "blog_post";
	$new_post->title = "New Blog Post";

	$new_post->save();

	$session->redirect($config->urls->admin . "page/edit/?id=" . $new_post->id . "&new=1");

}

if ($user->isSuperuser() && $input->get->type == "gallery") {

	$new_post = new Page();
	$new_post->of(false);
	$new_post->parent = $page;
	$new_post->template = "gallery_post";
	$new_post->title = "New Gallery Post";

	$new_post->save();

	$session->redirect($config->urls->admin . "page/edit/?id=" . $new_post->id . "&new=1");

}

?>

<?php if ($page->template == "journal" && $user->isLoggedin()) { ?>

<a href="<?php echo $page->url . '?type=blog'; ?>">Add Blog Post</a>
<a href="<?php echo $page->url . '?type=gallery'; ?>">Add Gallery Post</a>

<?php } ?>

This works fine, creates the page with the right template and redirects to the correct admin form page.

Now when they have added their content, I would like them to be redirected to the front-end to see the immediate effects of their new post.

I've created the module but I'm having trouble finding the right combination of hook and before/after.

So far I can either seem to manage the redirect without the changes or save the changes but the redirect doesn't take effect.

This is the basic module I have so far but obviously the hooks/processes are incorrect. Does anyone have any experience of getting this working? Thanks

<?php

class BlogRedirect extends WireData implements Module {

    public static function getModuleInfo() {

        return array(
            'title' => 'Redirects back to front end journal',
            'version' => 101,
            'summary' => 'Redirects back to front end journal page.',
            'singular' => true,
            'autoload' => true,
            );
    }

    public function init() {
        $this->pages->addHookBefore('save', $this, 'redirectToBlog');
    }

    public function redirectToBlog($event) {

        $page = $event->arguments[0];
        $session = wire("session");

// make sure that the initial create page doesn't redirect
    	if ($page->parent->template == "journal" && $page->id) {
        	$session->redirect("http://localhost/route66_pw/journal");
        }

    }

}
Link to comment
Share on other sites

Here is a module I use (I reduced it down to the more relevant bits). Perhaps this will help.

<?php

class AdminPageRedirects extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Admin Page Redirects',
            'version' => 1,
            'summary' => 'Redirect to after save for certain template types.',
            'singular' => true,
            'autoload' => true
            );
    }

    protected $redirects = array(
        "news" => "/processwire/news/",
        "events" => "/processwire/events/",
        "venues" => "/processwire/venues/",
    );

    public function init() {
        $this->pages->addHookAfter('save', $this, 'redirect');
        $this->pages->addHookAfter('trashed', $this, 'redirect');
        $this->pages->addHookAfter('trash', $this, 'redirect');
    }

    public function redirect($event){
        
        $page = $event->arguments(0);
        
        $errors = false;

        // check notices for errors before redirecting
        foreach(wire('notices') as $notice){
            if($notice instanceof NoticeError) $errors = true;
        }

        if (array_key_exists("{$page->template}", $this->redirects) && !$errors){
            $this->session->redirect($this->config->url->admin . $this->redirects["{$page->template}"]);
        }

    }

}

EDIT: Just wanted to mention that I didn't read your post all that closely, so my apologies if you are looking for something more detailed — short on time. :D

  • Like 7
Link to comment
Share on other sites

Update, thanks Reno, I've got it working now, not sure entirely what wasn't working before but now it's doing the job :)

Below the code in case it helps someone at a later date

<?php

class BlogRedirect extends WireData implements Module {

    public static function getModuleInfo() {

        return array(
            'title' => 'Redirects back to front end journal',
            'version' => 101,
            'summary' => 'Redirects back to front end journal page.',
            'singular' => true,
            'autoload' => true,
            );
    }

    public function init() {
        $this->pages->addHookAfter('save', $this, 'redirect');
        $this->pages->addHookAfter('trash', $this, 'redirect');
        $this->pages->addHookAfter('trashed', $this, 'redirect');
    }

    public function redirect($event){
        
        $page = $event->arguments(0);
        $errors = false;

        // check notices for errors before redirecting
        foreach(wire('notices') as $notice){
            if($notice instanceof NoticeError) $errors = true;
        }

        // if errors
        if ($errors) {
            return;
        }

        // if no created date is set it means just generated so don't redirect
        if ($page->created == 0) {
            return;
        }   

        // set return URL
        $goto = wire("pages")->get("template=journal")->url;

        // if it's one of the chosen templates, redirect
        if ($page->template == "blog_post" || $page->template == "gallery_post"){
            wire("session")->redirect($goto);
        }

    }

}
  • Like 5
Link to comment
Share on other sites

Nice, Glad you got it working.

Might be an ever so slight performance improvement by checking the page template before doing anything else.

public function redirect($page){

    $page = $event->arguments(0);

    if ($page->template == "blog_post" || $page->template == "gallery_post")){

        $errors = false;

        // check notices for errors before redirecting
        foreach(wire('notices') as $notice){
            if($notice instanceof NoticeError) $errors = true;
        }

        // if errors or if no created date is set it means just generated so don't redirect
        if ($errors || $page->created == 0) {
            return;
        }

        wire("session")->redirect(wire("pages")->get("template=journal")->url);
    }
}
  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...