michaellevelup

Restrict download by role

May 5th, 2022 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. function king_add_download($text){
  2.  
  3.     global $post;
  4.     $user = wp_get_current_user();
  5.     $thumb_URL = get_the_post_thumbnail_url( get_the_ID(), 'full' );
  6.     $download_class = "_YOUR_CLASS_HERE";
  7.  
  8.     if(in_array('administrator', $user->roles)){
  9.  
  10.         if(($post->post_type == 'post')){
  11.  
  12.             $last_tag = strrpos( $text, "</" );
  13.             $text = substr_replace( $text, '<div="'.$download_class.'"><a href="'.$thumb_URL.'">Download this Image</a></div>', $last_tag, 0 );
  14.         }
  15.  
  16.     }
  17.     else{
  18.  
  19.         $last_tag = strrpos( $text, "</" );
  20.         $text = substr_replace( $text, '<div="'.$download_class.'"><a href="#">Download this Image</a></div>', $last_tag, 0 );
  21.     }
  22.  
  23.     return $text;
  24. }
  25.  
  26. add_filter('the_content','king_add_download', 1);
Add Comment
Please, Sign In to add comment