Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Wordpress » Tạo nút media upload trong wordpress

Tạo nút media upload trong wordpress

Thứ Năm, 03/07/2014 by Hoàng Quách

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Bước đầu tiên chèn thư viện wordpress.

//chen thu vien wordpress
function my_admin_scripts() {
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
    wp_register_script('my-upload', WP_PLUGIN_URL.'/my-script.js', array('jquery','media-upload','thickbox'));
    wp_enqueue_script('my-upload');
    //style
    wp_enqueue_style('thickbox');
}
add_action('admin_print_scripts', 'my_admin_scripts');

Bước 2: Tạo nút upload và text field để trả về giá trị. Bạn có thể tạo menu trong wordpress admin để test và copy đoạn html sau.

<input type="text" name="image_url" id="slide_image" value=""/>
<input type="button" id="upload_image" name="upload_image" class="upload-button" onclick="javascript:focus_editor();formfield = jQuery('#slide_image').attr('name'); tb_show('','media-upload.php?type=image&amp;TB_iframe=true'); return false;" value="Image"/>

Khi click vào nút upload sẽ hiển thị trình media uploader, trình uploader sẽ làm việc với text editor chính, mặc định media uploader làm việc với mce editor để tạo bài viết trong wordpress. Hàm dưới đây trình uploader sau khi upload ảnh sẽ Image url và lưu vào input text rồi trả uploader về editor chính.

<script>
function focus_editor(){
    window.restore_send_to_editor = window.send_to_editor;
    // send url back to plugin editor
    window.send_to_editor = function(html){
        if(formfield){
         imgurl = jQuery('img',html).attr('src');
         var data=formfield.val();
         formfield.val((data? data.replace(/[,\s]+$/g,'')+',':'')+imgurl);
 
         formfield=null;
        }
        tb_remove();
        window.send_to_editor=window.restore_send_to_editor;
    }
}
    </script>

Chúc bạn thành công.

Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Hoangweb trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Wordpress Tìm kiếm: media uploader

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.