Advertisement
AndyWarren

Arthur Gareginyan WordPress Media Upload PHP (modified)

Jul 28th, 2017
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Image Uploader
  4.  *
  5.  * author: Arthur Gareginyan www.arthurgareginyan.com
  6.  * Modified by Andy Warren - Original here: https://mycyberuniverse.com/integration-wordpress-media-uploader-plugin-options-page.html
  7.  */
  8. function arthur_image_uploader($optionName) {
  9.  
  10.     // Set variables
  11.     $options = get_option($optionName);
  12.     $default_image = 'https://www.placehold.it/115x115';
  13.  
  14.     if (!empty($options)) {
  15.         $image_attributes = wp_get_attachment_image_src($options, 'full');
  16.         $src = $image_attributes[0];
  17.         $value = $options;
  18.     } else {
  19.         $src = $default_image;
  20.         $value = '';
  21.     }
  22.  
  23.     // Print HTML field
  24.     echo '
  25.        <div class="upload" style="max-width:400px;">
  26.            <img data-src="' . $default_image . '" src="' . $src . '" style="max-width:100%; height:auto;" />
  27.            <div>
  28.                 <input type="hidden" name="' . $optionName . '" id="' . $optionName . '" value="' . $value . '" />
  29.                <button type="submit" class="upload_image_button button">' . __('Upload', 'igsosd') . '</button>
  30.                <button type="submit" class="remove_image_button button">&times;</button>
  31.            </div>
  32.        </div>
  33.    ';
  34. }
  35.  
  36. /*********
  37. Usage:
  38.  
  39. Call the function with arthur_image_uploader('igsosd_logo'); and replace the "igsosd_logo" with the name of the option field you're saving to
  40.  
  41. to retrieve the image in a template file use $igsosdLogo = get_option('igsosd_logo'); then display it with wp_get_attachment_url($igsosdLogo); while making sure to change igsosd_logo in the get_option() call to be what your field name is.
  42.  
  43. *********/
  44.  
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement