Jump to content

Regularly delete "empty" pages


Recommended Posts

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:

  1. Use a hook that is regularly fired? (Which and how)
  2. Check if there are any pages with empty title (and/or other factors) and that are created 24 hours+ ago?
  3. 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

SCR-20230509-tzc.png

Link to comment
Share on other sites

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);
  }
});

 

  • Like 9
  • Thanks 2
Link to comment
Share on other sites

@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

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...