Hallo liebe Community,
leider habe ich ein Problem bei der Programmierung eines Widgets und habe auch nach langer Recherche nichts finden können. Soweit funktionieren die Textfelder des Widgets, allerdings gibt es ein Problem bei der Übergabe der Bild Url. Diese wird nicht in das Input Feld geladen, wenn man beim Bild in der "Media Library" auf "in den Beitrag einfügen" klickt. Ich konnte das Problem soweit lokalisieren, dass die Variable "imgurl" in meinem Javascript Code leer ist. Weiß da allerdings nicht mehr weiter.
Schon mal vielen Dank für eure Mühen
Sparkley
Mein PHP Code
PHP
class home_image_box extends WP_Widget { public function add_admin_scripts() { //media btn wp_enqueue_script('media-upload'); wp_enqueue_script('thickbox'); wp_enqueue_script('widgetImage', get_stylesheet_directory_uri() . '/js/widgetImage.js', array( 'jquery' )) ;} public function add_admin_styles() { wp_enqueue_style('thickbox');} function __construct() { $widget_ops = array( 'classname' => 'home_image_box', 'description' => 'Bildbox Startseite', ); parent:: __construct(false, $name = __('Bildbox Startseite'), $widget_ops); add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ) ); add_action( 'admin_enqueue_styles', array( $this, 'add_admin_styles' ) ); } function form($instance) { /*Ausgabe im Backend*/ echo '<h2>Beispiel</h2>'; echo '<p>Hier kann ich etwas dem Benutzer mitteilen!</p>';
echo '<label for="' . $this->get_field_id('home_image_box_title') . '">Titel: </label>'; echo '<input class="widefat" type="text" value="' . $instance['home_image_box_title'] .'" name="' . $this->get_field_name('home_image_box_title') . '" id="' . $this->get_field_id('home_image_box_title') . '" >'; ?>
<label for="<?php echo $this->get_field_name( 'image' ); ?>"><?php _e( 'Bild URL:' ); ?></label> <input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat image-url" type="text" size="36" value="<?php echo esc_url( $image ); ?>" /> <input class="upload_image_button" type="button" value="Upload Image" /> <?php echo '<label for="' . $this->get_field_id('home_image_box_link') . '">Link: </label>'; echo '<input class="widefat" type="text" value="' . $instance['home_image_box_link'] .'" name="' . $this->get_field_name('home_image_box_link') . '" id="' . $this->get_field_id('home_image_box_link') . '" >'; echo '<label for="' . $this->get_field_id('home_image_box_link_text') . '">Linktext: </label>'; echo '<input class="widefat" type="text" value="' . $instance['home_image_box_link_text'] .'" name="' . $this->get_field_name('home_image_box_link_text') . '" id="' . $this->get_field_id('home_image_box_link_text') . '" >'; echo '<label for="' . $this->get_field_id('home_image_box_text') . '">Text: </label>'; echo '<input class="widefat" type="text" value="' . $instance['home_image_box_text'] .'" name="' . $this->get_field_name('home_image_box_text') . '" id="' . $this->get_field_id('home_image_box_text') . '" >'; } function update($new_instance, $old_instance) { $instance['image_uri'] = strip_tags( $new_instance['image_uri'] ); $instance['home_image_box_title'] = $new_instance['home_image_box_title']; $instance['home_image_box_link'] = $new_instance['home_image_box_link']; $instance['home_image_box_link_text'] = $new_instance['home_image_box_link_text']; $instance['home_image_box_text'] = $new_instance['home_image_box_text']; return $instance; } function widget($args, $instance) { /*Ausgabe im Frontend*/ echo $args['before_widget']; ?> <img src="<?php echo esc_url($instance['image_uri']); ?>" /> <?php echo $args['after_widget']; }} add_action('widgets_init', function() { register_widget('upper_bar_widget'); register_widget('home_image_box');});
Mein JS Code
Code
jQuery(document).ready(function($) { $(document).on("click", ".upload_image_button", function() {
jQuery.data(document.body, 'prevElement', $(this).prev());
window.send_to_editor = function(html) {
var imgurl = $('img',html).attr('src');
var inputText = jQuery.data(document.body, 'prevElement');
console.log(inputText);
if(inputText != undefined && inputText != '')
{
inputText.val(imgurl);
$('.image-url').val('imgurl');
}
tb_remove();
};
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
});
Alles anzeigen