Posted By

nbpurohit on 08/10/16


Tagged

image wordpress upload admin


Versions (?)

image upload


 / Published in: PHP
 

wordpress admin image upload

  1. <script>
  2. jQuery(document).ready(function() {
  3.  
  4. jQuery('#upload_image_button').click(function() {
  5. formfield = jQuery('#upload_image').attr('name');
  6. tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
  7. return false;
  8. });
  9.  
  10. window.send_to_editor = function(html) {
  11. imgurl = jQuery('img',html).attr('src');
  12. jQuery('#upload_image').val(imgurl);
  13. tb_remove();
  14. }
  15.  
  16. });
  17.  
  18. </script>
  19. <?php
  20. /**
  21. * Plugin Name: Custom Option
  22. * Description: Display custom option in admin page.
  23. * Version: 1.0
  24. * Author: Nilay Purohit
  25. * License: A "Slug" license name e.g. GPL12
  26. */
  27.  
  28. /*wp_enqueue_style( 'thickbox' );
  29. wp_enqueue_script( 'thickbox' );
  30. wp_enqueue_script( 'media-upload' );
  31.  
  32. // load script to admin
  33. function wpss_admin_js() {
  34. $siteurl = get_option('siteurl');
  35.   $url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/wptuts-upload.js';
  36.   echo "<script type='text/javascript' src='$url'></script>";
  37. }
  38. add_action('admin_head', 'wpss_admin_js');
  39. ?>
  40.  
  41. <div id="wpss_upload_image_thumb" class="wpss-file">
  42.   <?php if(isset($record->security_image) && $record->security_image !='') { ?>
  43.   <img src="<?php echo $record->security_image;?>" width="65"/><?php } else { echo $defaultImage; } ?>
  44.   </div>
  45.   <input id="wpss_upload_image" type="text" size="36" name="wpss_upload_image" value="" class="wpss_text wpss-file" />
  46.   <input id="wpss_upload_image_button" type="button" value="Upload Image" class="wpss-filebtn" />*/
  47.  
  48.  
  49. // create custom plugin settings menu
  50. add_action('admin_menu', 'my_cool_plugin_create_menu');
  51.  
  52. function my_cool_plugin_create_menu() {
  53.  
  54. //create new top-level menu
  55. add_menu_page('My Cool Plugin Settings', 'Cool Settings', 'administrator', __FILE__, 'my_cool_plugin_settings_page' , plugins_url('/images/icon.png', __FILE__) );
  56.  
  57. //call register settings function
  58. add_action( 'admin_init', 'register_my_cool_plugin_settings' );
  59. }
  60.  
  61.  
  62. function register_my_cool_plugin_settings() {
  63. //register our settings
  64. register_setting( 'my-cool-plugin-settings-group', 'new_option_name' );
  65. register_setting( 'my-cool-plugin-settings-group', 'some_other_option' );
  66. register_setting( 'my-cool-plugin-settings-group', 'option_etc' );
  67. register_setting( 'my-cool-plugin-settings-group', 'upload_image' );
  68. }
  69. function load_custom_wp_admin_style() {
  70. wp_enqueue_script('media-upload');
  71. wp_enqueue_script('thickbox');
  72. wp_register_script('my-upload', WP_PLUGIN_URL.'/custom-option/my-script.js', array('jquery','media-upload','thickbox'));
  73. wp_enqueue_script('my-upload');
  74.  
  75. wp_enqueue_style('thickbox');
  76. }
  77. add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
  78.  
  79.  
  80. if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') {
  81. add_action('admin_print_scripts', 'my_admin_scripts');
  82. add_action('admin_print_styles', 'my_admin_styles');
  83. }
  84. function my_cool_plugin_settings_page() {
  85. ?>
  86. <div class="wrap">
  87. <h1>Your Plugin Name</h1>
  88.  
  89. <form method="post" action="options.php">
  90. <?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
  91. <?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
  92. <table class="form-table">
  93. <tr valign="top">
  94. <th scope="row">New Option Name</th>
  95. <td><input type="text" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td>
  96. </tr>
  97.  
  98. <tr valign="top">
  99. <th scope="row">Some Other Option</th>
  100. <td><input type="text" name="some_other_option" value="<?php echo esc_attr( get_option('some_other_option') ); ?>" /></td>
  101. </tr>
  102.  
  103. <tr valign="top">
  104. <th scope="row">Options, Etc.</th>
  105. <td><input type="text" name="option_etc" value="<?php echo esc_attr( get_option('option_etc') ); ?>" /></td>
  106. </tr>
  107.  
  108. <tr valign="top">
  109.  
  110. <th scope="row">Upload Image</th>
  111. <td><label for="upload_image">
  112. <input id="upload_image" type="text" size="36" name="upload_image" value="<?php echo esc_attr( get_option('upload_image') ); ?>" />
  113. <input id="upload_image_button" type="button" value="Upload Image" />
  114. <br />Enter an URL or upload an image for the banner.
  115. <?php if(get_option('upload_image')){
  116. echo "<br>This is your current logo: <br><img src='". esc_attr( get_option('upload_image') ) ."' style='padding:20px;' />";
  117. echo "<br>To use it in a theme copy this: <blockquote>". htmlspecialchars("<?php do_shortcode('[sitelogo]'); ?>") ."</blockquote><br> To use it in a post or page copy this code:<blockquote>[sitelogo]</blockquote>"; }?>
  118. </label></td>
  119. </tr>
  120. </table>
  121.  
  122. <?php submit_button(); ?>
  123.  
  124. </form>
  125. </div>
  126. <?php }
  127.  
  128. function get_site_logo(){
  129.  
  130. if(get_option('upload_image')){
  131. echo "<img src='". esc_attr( get_option('upload_image') ) ."' style='padding:20px;' />";
  132. } else {echo "Sorry, No logo selected";}}
  133. add_shortcode('sitelogo1', 'get_site_logo');

Report this snippet  

You need to login to post a comment.