Advertisement
Guest User

Untitled

a guest
May 25th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. /* Подключаем скрипты в админку через wp_enqueue_script() и wp_enqueue_media() */
  2. function true_include_myuploadscript() {
  3. // у вас в админке уже должен быть подключен jQuery, если нет - раскомментируйте следующую строку:
  4. // wp_enqueue_script('jquery');
  5. // дальше у нас идут скрипты и стили загрузчика изображений WordPress
  6. if ( ! did_action( 'wp_enqueue_media' ) ) {
  7. wp_enqueue_media();
  8. }
  9. // само собой - меняем admin.js на название своего файла
  10. wp_enqueue_script( 'myuploadscript', get_stylesheet_directory_uri() . '/admin.js', array('jquery'), null, false );
  11. }
  12.  
  13. add_action( 'admin_enqueue_scripts', 'true_include_myuploadscript' );
  14.  
  15. /* PHP-функция добавления полей в форму */
  16. function true_image_uploader_field( $name, $value = '', $w = 115, $h = 90) {
  17. $default = get_stylesheet_directory_uri() . '/img/no-image.png';
  18. if( $value ) {
  19. $image_attributes = wp_get_attachment_image_src( $value, array($w, $h) );
  20. $src = $image_attributes[0];
  21. } else {
  22. $src = $default;
  23. }
  24. echo '
  25. <div>
  26. <img data-src="' . $default . '" src="' . $src . '" width="' . $w . 'px" height="' . $h . 'px" />
  27. <div>
  28. <input type="hidden" name="' . $name . '" id="' . $name . '" value="' . $value . '" />
  29. <button type="submit" class="upload_image_button button">Загрузить</button>
  30. <button type="submit" class="remove_image_button button">&times;</button>
  31. </div>
  32. </div>
  33. ';
  34. }
  35.  
  36. /* Использование в метабоксах */
  37. /*
  38. * Добавляем метабокс
  39. */
  40. function true_meta_boxes_u() {
  41. add_meta_box('truediv', 'Настройки', 'true_print_box_u', 'post', 'normal', 'high');
  42. }
  43.  
  44. add_action( 'admin_menu', 'true_meta_boxes_u' );
  45.  
  46. /*
  47. * Заполняем метабокс
  48. */
  49. function true_print_box_u($post) {
  50. if( function_exists( 'true_image_uploader_field' ) ) {
  51. true_image_uploader_field( 'uploader_custom', get_post_meta($post->ID, 'uploader_custom',true) );
  52. }
  53. }
  54.  
  55. /*
  56. * Сохраняем данные произвольного поля
  57. */
  58. function true_save_box_data_u( $post_id ) {
  59. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  60. return $post_id;
  61. update_post_meta( $post_id, 'uploader_custom', $_POST['uploader_custom']);
  62. return $post_id;
  63. }
  64.  
  65. add_action('save_post', 'true_save_box_data_u');
  66.  
  67. /* Использование на странице настроек */
  68.  
  69. <?php
  70. function true_add_options_page_u() {
  71. if ( isset( $_GET['page'] ) == 'uplsettings' ) {
  72. if ( 'save' == isset( $_REQUEST['action'] ) ) {
  73. update_option('uploader_custom', $_REQUEST[ 'uploader_custom' ]);
  74. header("Location: ". site_url() ."/wp-admin/options-general.php?page=uplsettings&saved=true");
  75. die;
  76. }
  77. }
  78. add_submenu_page('options-general.php','Дополнительные настройки','Настройки','edit_posts', 'uplsettings', 'true_print_options_u');
  79. }
  80.  
  81. function true_print_options_u() {
  82. if ( isset( $_REQUEST['saved'] ) ){
  83. echo '<div class="updated"><p>Сохранено.</p></div>';
  84. }
  85. ?><div class="wrap">
  86. <form method="post">
  87. <?php
  88. if( function_exists( 'true_image_uploader_field' ) ) {
  89. true_image_uploader_field('uploader_custom', get_option('uploader_custom'));
  90. }
  91. ?><p class="submit">
  92. <input name="save" type="submit" class="button-primary" value="Сохранить изменения" />
  93. <input type="hidden" name="action" value="save" />
  94. </p>
  95. </form>
  96.  
  97. </div><?php
  98. }
  99.  
  100. add_action('admin_menu', 'true_add_options_page_u');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement