Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active February 5, 2026 14:49
Show Gist options
  • Select an option

  • Save damiencarbery/b06b00c71e7e184f9899b2dec235809d to your computer and use it in GitHub Desktop.

Select an option

Save damiencarbery/b06b00c71e7e184f9899b2dec235809d to your computer and use it in GitHub Desktop.
Example extensions for my 'Downloads for logged in users' plugin (https://wordpress.org/plugins/downloads-for-logged-in-users/)
<?php
/*
Plugin Name: Allow specific download always
Plugin URI: https://www.damiencarbery.com/downloads-for-logged-in-users/
Description: With the <a href="https://wordpress.org/plugins/downloads-for-logged-in-users/">Downloads for logged in users</a> plugin, always allow one download.
Author: Damien Carbery
Version: 0.1
Requires Plugins: downloads-for-logged-in-users
*/
add_filter( 'spdownload_check_perms', 'dcwd_allow_specific_download_always', 10, 2 );
function dcwd_allow_specific_download_always( $user_logged_in, $post_id ) {
if ( 2919 == $post_id ) {
return true;
}
return $user_logged_in;
}
<?php
/*
Plugin Name: Allow unrestricted downloads on Wednesdays
Plugin URI: https://www.damiencarbery.com/downloads-for-logged-in-users/
Description: With the <a href="https://wordpress.org/plugins/downloads-for-logged-in-users/">Downloads for logged in users</a> plugin, allow unrestricted downloads on Wednesdays.
Author: Damien Carbery
Version: 0.1
Requires Plugins: downloads-for-logged-in-users
*/
add_filter( 'spdownload_check_perms', 'dcwd_allow_downloads_on_wednesdays', 10, 2 );
function dcwd_allow_downloads_on_wednesdays( $user_logged_in, $post_id ) {
$day_of_week = date( 'w' );
// Wednesday is 3 (Sunday is 0).
if ( 3 == $day_of_week ) {
return true;
}
return $user_logged_in;
}
<?php
/*
Plugin Name: Downloads counter
Plugin URI: https://www.damiencarbery.com/downloads-for-logged-in-users/
Description: With the <a href="https://wordpress.org/plugins/downloads-for-logged-in-users/">Downloads for logged in users</a> plugin, count each download and display the number in a new column in the Downloads admin page.
Author: Damien Carbery
Version: 0.1
Requires Plugins: downloads-for-logged-in-users
*/
class DLIU_Counter {
private $cpt_name;
private $meta_key;
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
$this->cpt_name = 'dcwd_simple_download';
$this->meta_key = 'dl_count';
$this->init();
}
// Set up WordPress specfic actions.
public function init() {
add_action( 'spdownload_after_download', array( $this, 'increment_downloads_counter' ) );
add_filter( 'manage_' . $this->cpt_name . '_posts_columns', array( $this, 'add_count_column' ), 15 );
add_action( 'manage_' . $this->cpt_name . '_posts_custom_column', array( $this, 'display_count_column' ), 10, 2 );
}
function increment_downloads_counter( $post_id ) {
$download_count = get_post_meta( $post_id, $this->meta_key, true );
if ( is_numeric( $download_count ) ) {
$download_count++;
}
else {
$download_count = 1;
}
update_post_meta( $post_id, $this->meta_key, $download_count );
}
// Add custom column to dcwd_simple_download post type admin list
function add_count_column( $columns ) {
// Insert the File column after the title
$new_columns = array();
foreach ( $columns as $key => $value ) {
$new_columns[$key] = $value;
if ( $key === 'protected_file' ) {
$new_columns[ $this->meta_key ] = 'Download count';
}
}
return $new_columns;
}
// Display the download count.
function display_count_column( $column, $post_id ) {
if ( $column === $this->meta_key ) {
$download_count = get_post_meta( $post_id, $this->meta_key, true );
if ( is_numeric( $download_count ) ) {
echo wp_sprintf( '%d', $download_count );
return;
}
else {
echo '—';
}
}
}
}
$DLIU_Counter = new DLIU_Counter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment