This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e9e349c
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# LGND Expire Sticky Posts | ||
This plugin automatically expires sticky posts after a set number of days (by default, 14). To use: | ||
|
||
- Edit the `lgnd-expire-stickies.php` file to set the appropriate value of `LGND_EXPIRE_STICKIES_DAYS` | ||
- Install the plugin by uploading it to the Plugins page in your WordPress backend | ||
- Activate the plugin | ||
|
||
Once installed and activated, the plugin will run using wp-cron **hourly**, selecting all sticky posts older than `LGND_EXPIRE_STICKIES_DAYS` and unstickying them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Expire Stickies | ||
* Description: Automatically expire sticky posts after a set period of days | ||
* Version: 1.0 | ||
* Author: LGND | ||
* Author URI: https://lgnd.com | ||
* Contributors: mccahan | ||
*/ | ||
|
||
define('LGND_EXPIRE_STICKIES_DAYS', 14); | ||
|
||
function lgndCronActivation() | ||
{ | ||
if (!wp_next_scheduled('lgnd_expire_stickies')) | ||
wp_schedule_event(time()+30, 'hourly', 'lgnd_expire_stickies'); | ||
} | ||
register_activation_hook(__FILE__, 'lgndCronActivation'); | ||
|
||
// Delete cronjob on deactivation | ||
function lgndCronDeactivation() | ||
{ | ||
$time = wp_next_scheduled('lgnd_expire_stickies'); | ||
wp_unschedule_event($time, 'lgnd_expire_stickies'); | ||
} | ||
register_deactivation_hook(__FILE__, 'lgndCronDeactivation'); | ||
|
||
// Cronjob function | ||
function lgndExpireStickiesCron() | ||
{ | ||
$before = time() - LGND_EXPIRE_STICKIES_DAYS*86400; | ||
$args = [ | ||
'posts_per_page' => -1, | ||
'post__in' => get_option('sticky_posts'), | ||
'date_query' => [ | ||
[ | ||
'before' => [ | ||
'year' => date('Y', $before), | ||
'month' => date('m', $before), | ||
'day' => date('d', $before) | ||
] | ||
] | ||
] | ||
]; | ||
$query = new WP_Query($args); | ||
|
||
while ($query->have_posts()) | ||
{ | ||
$query->the_post(); | ||
unstick_post(get_the_ID()); | ||
} | ||
wp_reset_postdata(); | ||
} | ||
add_action('lgnd_expire_stickies', 'lgndExpireStickiesCron'); |