We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    I was in need of a script that sets the unpub_date 1 year after the pubdate, so the page will be automatically de-published once it's one year old. So now you don't need to set the unpub_date manually.

    Here you find the code. Create a plugin and check OnDocFormSave

    <?php
    
    // Only execute when there is no unpub_date set
    if ($resource->get('unpub_date') == 0)
    	{
    
    	// All children of 765 will be kept 1 year
    	if ($resource->get('parent') == 765)
    		{
    		$publishedon = $resource->get('publishedon');
    
    		//Substract time from publishedon
    		$publishedon = substr($publishedon, 0, -9);
    		$date = new DateTime($publishedon);
    
    		// Add 1 year
    		$date->add(new DateInterval('P1Y'));
    		$resource->set('unpub_date', $date->format('Y-m-d'));
    		$resource->save();
    		}
    
    	// All children of 767 will be kept 2 years
    	elseif ($resource->get('parent') == 767)
    		{
    		$publishedon = $resource->get('publishedon');
    
    		//Substract time from publishedon
    		$publishedon = substr($publishedon, 0, -9);
    		$date = new DateTime($publishedon);
    
    		// Add 2 years
    		$date->add(new DateInterval('P2Y'));
    		$resource->set('unpub_date', $date->format('Y-m-d'));
    		$resource->save();
    		}
    	}
    


    Enjoy! [ed. note: michelle84 last edited this post 11 years, 2 months ago.]
      • 3749
      • 24,544 Posts
      Great idea. I think you can simplify it and make it slightly more efficient with this code:

      $t = strtotime('+1 year', $resource->get('publishedon'));
      $resource->set('unpub_date', $strftime('Y-m-d', $t));



      If it were connected to OnDocPublished, I think this would work, since it would use the current time:

      $t = strtotime('+1 year');
      $resource->set('unpub_date', $strftime('Y-m-d', $t));
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 19328
        • 433 Posts
        Thanks Bob!