Last active
November 18, 2015 12:09
-
-
Save damianwajer/b5ce733185572e36bf4e to your computer and use it in GitHub Desktop.
[WordPress] How to add multiple media uploader buttons on custom option page (an example).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(".image-upload-button").on("click", function (e) { | |
| var $area = $(this).closest(".image-upload-area"), | |
| send_to_editor_backup = window.send_to_editor; | |
| tb_show('', 'media-upload.php?type=image&TB_iframe=true'); | |
| window.send_to_editor = function (html) { | |
| var imgUrl = $("img", html).attr("src"); | |
| $area.find(".image-url").val(imgUrl); | |
| $area.find(".image-preview").attr("src", imgUrl); | |
| tb_remove(); | |
| window.send_to_editor = send_to_editor_backup; | |
| }; | |
| e.preventDefault(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Load all necessary assets. | |
| * | |
| * @link https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts | |
| * | |
| * @param $hook_suffix | |
| */ | |
| function namespace_options_scripts( $hook_suffix ) { | |
| wp_register_script( 'admin', get_template_directory_uri() . '/js/admin.js', array( | |
| 'jquery', | |
| 'media-upload', | |
| 'thickbox' | |
| ), '1.0', true ); | |
| if ( 'custom_settings' == $hook_suffix ) { | |
| wp_enqueue_script( 'jquery' ); | |
| wp_enqueue_script( 'media-upload' ); | |
| wp_enqueue_script( 'thickbox' ); | |
| wp_enqueue_style( 'thickbox' ); | |
| wp_enqueue_script( 'admin' ); | |
| } | |
| } | |
| add_action( 'admin_enqueue_scripts', 'namespace_options_scripts' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Get array with images | |
| $images = array( | |
| '', // 1st image URL | |
| '', // 2nd image URL | |
| ); ?> | |
| <table class="form-table"> | |
| <tbody> | |
| <tr valign="top"> | |
| <th scope="row"> | |
| <label for="image_url">Image URL</label> | |
| </th> | |
| <td class="image-upload-area"> | |
| <input type="url" name="image_url" id="image_url" class="regular-text image-url" | |
| value="<?php echo esc_attr( $images[0] ); ?>"> | |
| <button class="button image-upload-button" type="button"><?php _e( 'Select Image' ); ?></button> | |
| <p> | |
| <img src="<?php echo esc_url( $images[0] ); ?>" class="image-preview" alt=""> | |
| </p> | |
| </td> | |
| </tr> | |
| <tr valign="top"> | |
| <th scope="row"> | |
| <label for="image_url_2">Image URL 2</label> | |
| </th> | |
| <td class="image-upload-area"> | |
| <input type="url" name="image_url_2" id="image_url_2" class="regular-text image-url" | |
| value="<?php echo esc_attr( $images[1] ); ?>"> | |
| <button class="button image-upload-button" type="button"><?php _e( 'Select Image' ); ?></button> | |
| <p> | |
| <img src="<?php echo esc_url( $images[1] ); ?>" class="image-preview" alt=""> | |
| </p> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment