saschapi Posted May 9, 2023 Share Posted May 9, 2023 In one of my projects editors "constantantly" create pages without titles and abandon them (see screenhot). Due to some circumstances I had to activate the "Name for children" and autogenerate the name. So when the editors create a page in that section it is automatically saved with an cryptic name when they add a new page. When the editor just closes the browser or due to other reasons does not use this page there are several "empty" pages that no one uses. I would love to delete those empty pages after 24 hours or so. Question: What would be a good way to achieve this? I'm thinking aloud here: Use a hook that is regularly fired? (Which and how) Check if there are any pages with empty title (and/or other factors) and that are created 24 hours+ ago? Delete the page? I have never really used any hooks and so I'm a bit lost where to start and how to "cron" this task. Any hint would be helpful, maybe I even miss something that could be used to more clean up those abandond pages more easy. Thanks in advance for any help, Cheers Sascha Link to comment Share on other sites More sharing options...
BitPoet Posted May 10, 2023 Share Posted May 10, 2023 Looks like a perfect application for LazyCron. A quick and dirty snippet of code you can place in site/ready.php (or inside a template's PHP file, just make sure that a page with that template is viewed regularly): <?php namespace ProcessWire; // Add daily hook to clean up incomplete, stale pages wire()->addHook('LazyCron::everyDay', null, function(HookEvent $event) { $ts = time() - 86400; // 24 hours ago // Find matching pages, adapt selector however you want (e.g. limit to certain templates or parents) $stalePages = $pages->find("created<$ts, title=''"); foreach($stalePages as $stalePage) { $pages->trash($stalePage); } }); 10 3 Link to comment Share on other sites More sharing options...
saschapi Posted May 10, 2023 Author Share Posted May 10, 2023 @BitPoet That's exactly what I'm looking for. Works perfectly out of the box! ❤️ I just wanted a hint into the right direction and you posted the complete solution! THANKS so much, that really saved me some time ? 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