How to Set limit of uploads per article in WordPress

If you are using WordPress and if you have a lot of authors. In other words, If you are running a multi author website, then you’ll always have this problem with how to set the limit of uploading photos for each article.

So, today I have come up with a tremendous solution for you so that you can handle the upload system of your website without using any third party plugin. But you need to create your own version of plugin. So, follow the steps given below :

File Structure

Folder structure : Create a main folder named ‘Limit Upload’ . Now again create a folder named ‘classes’ under ‘limit Upload’ folder.

File structure : Create a file named ‘limit-image-upload.php’ in main folder. Now Create Two files in ‘classes’ folder. One should be named ‘class-fetch_plugin_header.php’ and another should be named ‘class-limit_image_upload.php’.

Now you’re ready to make your plugin Functions. Let’s Go:

Start with ‘limit-image-upload.php’

Now open the file in notepad or any text editor and start coding as i have done below:

<?php
/**
Plugin Name: Limit Upload 
Plugin URI: http://aranjra.com
Text Domain: limit-image-upload
Domain Path: /languages
Description: Limit the number of uploads on posts
Version: 1.0.4
Author: Frank Bültge, Ralf Albert,AR
Author URI: http://aranjra.com
License: GPLv3
*/
if(!function_exists('add_filter')){
echo "Hi there! I'm just a part of plugin, not much I can do when called directly.";
exit;
}
class Init_Limit_Upload{
private static $instance=NULL;
public function construct(){ 
if(!is_admin()) 
return;$this->load_classes(); 
$this->start_limit_image_upload();
} 
public static function get_instance(){ 
if(!self::$instance)self::$instance=new self;
return self::$instance;
 }
protected function load_classes(){ 
foreach(glob(dirname(__FILE).'/classes/class-*.php')as $class)
require_once $class;
}
public function start_limit_image_upload(){
$plugin_header=new Fetch_Plugin_Header(FILE);
$args=array('max_uploads'=>2,
'post_types'=>array(),
'mime_types'=>array('pdf'=>'application/pdf','doc|docx'=>'application/msword','png'=>'image/png','jpe'=>'image/jpeg',
'jpeg'=>'image/jpeg',
'jpg'=>'image/jpeg',
'bmp'=>'image/bmp',
'gif'=>'image/gif',
'tif'=>'image/tiff',
'tiff'=>'image/tiff',
),
'textdomain'=>$plugin_header->get_textdomain(),
);
unset($plugin_header);
new Limit_Image_Upload($args);
}
}
if(function_exists('add_filter')&&class_exists('Init_Limit_Upload'))
add_action('plugins_loaded',array('Init_Limit_Upload','get_instance'));
Open ‘class-fetch_plugin_header.php’ file and write on:
<?php
/**
Class for WordPress to read the data from the plugin header
*
PHP version 5.3
*
@category PHP
@package WordPress
@subpackage Limit Upload
@author Frank Bültge, AR
@license GPLv3 http://www.gnu.org/licenses/gpl-3.0.txt
@version 0.2
@link http://aranjra.com
*/

class Fetch_Plugin_Header{
public $errors=array();
public $plugin_data=array();
public $absfile='';
public function __construct($absfile=''){
if(!empty($absfile)&&is_string($absfile))
$this->absfile=$absfile;
if(empty($this->plugin_data)&&!empty($this->absfile))
$this->get_plugin_header($this->absfile);
$this->load_plugin_textdomain();
}
protected function check_absfile(){
if(empty($this->absfile)&&!is_string($this->absfile)){
array_push($this->errors[],'No filepath was set.');
return NULL;
}
else{
return TRUE;
}
}
protected function check_plugin_data(){
if(empty($this->plugin_data)){
return $this->get_plugin_header();
}
else{
return TRUE;
}
}
public function get_plugin_header(){
if(TRUE!==$this->check_absfile())
return NULL;
if(!function_exists('get_plugin_data'))
require_once(ABSPATH.'/dashboard/includes/plugin.php');
$this->plugin_data=get_plugin_data($this->absfile);
if(empty($this->plugin_data)){
array_push($this->errors,'Can not read plugin header');
return NULL;
}
else{
return TRUE;
}
}
public function get_value($value='TextDomain'){
if(TRUE!==$this->check_plugin_data())
return NULL;
return(isset($this->plugin_data[$value]))?$this->plugin_data[$value]:NULL;
}
public function get_textdomain(){
return $this->get_value('TextDomain');
}
public function load_plugin_textdomain(){
if(TRUE!==$this->check_plugin_data()||empty($this->plugin_data['TextDomain'])){
return NULL;
}
if(!isset($this->plugin_data['DomainPath'])||empty($this->plugin_data['DomainPath'])){
array_push($this->errors,'DomainPath was not set');
return NULL;
}
else{
return load_plugin_textdomain($this->plugin_data['TextDomain'],FALSE,dirname(plugin_basename($this->absfile)).$this->plugin_data['DomainPath']);
}
}
} // end class

Interesting Article : How to remove ‘Quick Edit’ Option from Post in WordPress

Open ‘class-limit_image_upload.php’ and type the code there which is given below :
<?php
/**
Class for WordPress to limit the number and type of uploads
*
PHP version 5.3
*
@category PHP
@package WordPress
@subpackage Limit Upload
@author Frank Bültge, AR
@license GPLv3 http://www.gnu.org/licenses/gpl-3.0.txt
@version 0.2
@link http://aranjra.com
*/
class Limit_Image_Upload{
public static $limit_upload=3; // You can change it as per your requirements
public static $post_types=array();
public static $mime_types=array();
public static $textdomain='';
public function construct($args=array()){
 $def_args=array('max_uploads'=>self::$limit_upload,
                 'post_types'=>self::$post_types,
                 'mime_types'=>self::$mime_types,
                 'textdomain'=>self::$textdomain,);
 $args=array_merge($def_args,$args);
 self::$limit_upload=intval($args['max_uploads']);
 self::$post_types=$args['post_types'];
 self::$mime_types=$args['mime_types']; 
self::$textdomain=$args['textdomain']; $filters=array('wp_handle_upload_prefilter'=>'limit_handle_upload_prefilter',
'media_upload_tabs'=>'control_media_upload_tabs',
'media_upload_default_tab'=>'set_media_upload_default_tab',
'upload_mimes'=>'restrict_mime_types', );
foreach($filters as $hook=>$method) add_filter($hook,array(&$this,$method));
if(!empty(self::$mime_types)) add_action('post-upload-ui',array(&$this,'restrict_mime_types_hint'));
unset($def_args,$args,$filters);
 }
public function get_textdomain(){
return self::$textdomain;
 }
public function limit_handle_upload_prefilter($file){
if(in_array($GLOBALS['post_type'],
self::$post_types))
return $file; 
if($file['type']=='application/octet-stream'&&isset($file['tmp_name'])){
 $file_size=getimagesize($file['tmp_name']); if(isset($file_size['error'])&&0!=$file_size['error']){
$file['error']=('Unexpected Error:',self::$textdomain).' '.$file_size['error'];
return $file;
}
else{$file['type']=$file_size['mime'];
}
}
if($this->get_count_attachments()>self::$limit_upload-1){
$file['error']=sprintf(__('Sorry, you cannot upload more than %d images.',
self::$textdomain),self::$limit_upload);
}
return $file;
}
public function control_media_upload_tabs($tabs){
if($this->get_count_attachments()>self::$limit_upload){
unset($tabs['type']);
unset($tabs['type_url']);
}
return $tabs;
}
public function set_media_upload_default_tab($current){
if($this->get_count_attachments()>=self::$limit_upload)$current='gallery';
return $current;
}
protected function get_count_attachments(){
if(!isset($_REQUEST['post_id']))
return;
$post_id=intval($_REQUEST['post_id']);
$args=array('post_type'=>'attachment',
'numberposts'=>-1,
'post_status'=>null,
'post_parent'=>$post_id);
return count(get_posts($args));
}
public function restrict_mime_types($mime_types){
return self::$mime_types;
}
public function restrict_mime_types_hint(){
echo '
';_e('Accepted MIME types:',self::$textdomain);
echo ' '.implode(', ',array_flip(self::$mime_types)).'';
}
}

Copy the main folder ‘Limit Upload’ and paste it in WordPress Plugin Directory or you can easily compress the folder in zip format and upload it via custom plugin uploader in WordPress.

Interesting Article : How to remove Media Library Tab from WordPress without Plugin

Activate your Custom Plugin and test it.

Comment me if you find any issue…….

Comments to: How to Set limit of uploads per article in WordPress

Your email address will not be published. Required fields are marked *

Login

welcom
....

You can make anything by Reading and Writing.
Join Aranjra