JoshoB Posted May 5, 2020 Share Posted May 5, 2020 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 More sharing options...
Jan Romero Posted May 6, 2020 Share Posted May 6, 2020 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. 3 Link to comment Share on other sites More sharing options...
JoshoB Posted May 6, 2020 Author Share Posted May 6, 2020 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. Link to comment Share on other sites More sharing options...
psy Posted May 6, 2020 Share Posted May 6, 2020 @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 More sharing options...
Jan Romero Posted May 6, 2020 Share Posted May 6, 2020 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 More sharing options...
JoshoB Posted May 7, 2020 Author Share Posted May 7, 2020 I upgraded to the latest version of PW, but no dice. Doesn't work, with or without slash. I also have AIOM installed -- could that be a problem? Link to comment Share on other sites More sharing options...
Jan Romero Posted May 7, 2020 Share Posted May 7, 2020 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 More sharing options...
JoshoB Posted May 14, 2020 Author Share Posted May 14, 2020 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 More sharing options...
dotnetic Posted May 16, 2020 Share Posted May 16, 2020 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 3 Link to comment Share on other sites More sharing options...
JoshoB Posted May 16, 2020 Author Share Posted May 16, 2020 Wow, that is perfect! You should add it to the module repository. Thank you so much! Link to comment Share on other sites More sharing options...
dotnetic Posted May 17, 2020 Share Posted May 17, 2020 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 More sharing options...
tpr Posted May 17, 2020 Share Posted May 17, 2020 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 More sharing options...
kongondo Posted May 17, 2020 Share Posted May 17, 2020 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). ? 1 Link to comment Share on other sites More sharing options...
tpr Posted May 17, 2020 Share Posted May 17, 2020 Ok fair, but that could work for a module version ? 1 Link to comment Share on other sites More sharing options...
JoshoB Posted October 14, 2023 Author Share Posted October 14, 2023 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 More sharing options...
BitPoet Posted October 15, 2023 Share Posted October 15, 2023 Maybw reverse the order, add a URL hook for /preview/(queryparameter)/.* and rewrite/redirect from there? Link to comment Share on other sites More sharing options...
JoshoB Posted October 15, 2023 Author Share Posted October 15, 2023 Sorry, wrong thread: I will have a look if I can reverse the order, but I think the problem will be the same, as PW will look for a URL that doesn't exist. Link to comment Share on other sites More sharing options...
poljpocket Posted October 24, 2023 Share Posted October 24, 2023 (edited) 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 October 24, 2023 by poljpocket Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now