Jump to content

Preview unpublished page with GET variable


JoshoB
 Share

Recommended Posts

So I'm the editor of a large website and it's useful for me if my authors can check their articles before they are published on the web. I'd prefer to do this without needing them to sign in (with either their own account or a preview account). I figured the easiest way to do this is with a GET variable, so if someone goes to e.g. this URL:

https://www.examples.com/articles/test?preview=true

They would be able to see the specific article in question. But building the override eludes me; I tried to stick it in the template, but it's not working (using $pages->find or $pages->get with check_access=0).

I feel like this ought to be simple, but I cannot figure it out. Does anyone have a suggestion on how to solve this?

Many thanks in advance, as always!

Link to comment
Share on other sites

You could put this in your site/init.php:

$this->addHookAfter("Page::viewable", function($event) {
    if ($event->object->isUnpublished() && wire('input')->get('preview') === 'true') {
        $event->return = true;
    }
});

It won’t work in site/ready.php because the 404 will already be thrown by the time you get there. Of course, you may also make a module out of it.

  • Like 3
Link to comment
Share on other sites

@JoshoB

In my case the user is already logged in on the Front End, but you could test with an $input->get var:

<?php

        $vars = ['page'=> $yourPageObject];
        $out = $files->render('your-template', $vars);
        echo $out;
        exit();

 

Link to comment
Share on other sites

6 hours ago, JoshoB said:

Thank you for the suggestion, but it doesn't work: I still get a 404 error, even when I put it in site/init.php.

Mh, it works for me on PW 3.0.155. Although I noticed it needs a slash in front of the ? because of my template settings, so your specific example would not work in my setup.

Link to comment
Share on other sites

I don’t see why it should, but fwiw I’m not running it. I’m a little lost here. Any more info on your setup and how you’re testing exactly? Perhaps someone else can chime in, I’m sure there are other ways of doing something like this. Likely more elegant ones too ? A less elegant way that comes to mind would be to do the check at the top of your 404 template, then change the status header to 200 render the page you want instead…

Link to comment
Share on other sites

I'm testing it by putting the code in init.php (which gets loaded first), then going to the page in question via another browser with ?preview=true, and I get a 404 page instead of the article I want to preview.

But maybe forcing the 404 page to reload would be an idea. (Even though I cannot get that to work either.)

Link to comment
Share on other sites

On 5/6/2020 at 12:36 AM, JoshoB said:

$pages->find or $pages->get with check_access=0

The first selectors would not return unpublished pages. You have to use

$pages->find('your-selector, include=unpublished')

or include=all instead. The $pages->get() method already  includes hidden and unpublished results, as you are explictly requesting pages.

Regarding your problem:

You want to hook the ProcessPageView::pageNotFound method and modify the output. I made a little proof-of-work module for you.

It is a combination of the hooks for pageNotFound and Page::viewable. Maybe there could be a better and easier solution.

Install the attached module to test it out.

previewOption.module.php

  • Like 3
Link to comment
Share on other sites

Your are welcome. I don't think that this should be in the module repository, as it is for a very special case. And If users or search engines guess your preview variable, they are able to see/index your content, which is not yet to be meant to be read, hence the unpublished status.

Link to comment
Share on other sites

2 hours ago, dotnetic said:

If users or search engines guess your preview variable

Unless view was restricted to the user who created the content (or to role).

Link to comment
Share on other sites

1 hour ago, tpr said:

Unless view was restricted to the user who created the content (or to role).

...requirement...

On 5/5/2020 at 11:36 PM, JoshoB said:

I'd prefer to do this without needing them to sign in (with either their own account or a preview account).

?

 

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

I was using this to share early access to articles with Patreon users. However, Patreon now filters query strings from URLs, which is a PITA. I wanted to rework this module to use URL segments, so that any URL that ends in /preview/ becomes a preview link, but no dice: ProcessWire throws a 404 every time, regardless of what I try.

Any suggestions or ideas? Thanks again. 🙂

Link to comment
Share on other sites

  • 2 weeks later...

You souldn't abuse the unpublished state for that. A very watertight solution is to make use of Patreon's OAuth to make sure the page viewer is actually one of your supporters. This also eliminates the problem that any link out there which relies on "not being known to the public" doesn't add any form of security or "privateness". Imagine one of your Patreons sharing the direct link to his friends.

EDIT: This would be a real candidate for a module 🙂

Edited by poljpocket
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...