Image uploading widget using database
-
I’m new at wordpress widget/plugins development and I’ve came to the following problem. I’m trying to do an image uploading widget so i can have a list of images in my sidebar. The idea is to upload the image, save the image path/name in a DB and then retrieve it and show it via the widget.. But.. i get stuck on the creating of the DB… i guess the install function I have is not firing by the hook.. but I have no idea why. Here’s the code so far:
<?php /* Plugin Name: Friends List Logos Author: CSS Slicers Description: Upload logos to your friends list Author URI: https://www.cssslicers.com */ function widget_friendslist_init() { if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') ) return; function friendslist_install () { global $wpdb; $table_name = $wpdb->prefix . "friendslist"; if($wpdb->get_var("show tables like '$table_name'") != $table_name) { $sql = "CREATE TABLE " . $table_name . " ( id mediumint(9) NOT NULL AUTO_INCREMENT, image VARCHAR(255)NOT NULL, name VARCHAR(255) NOT NULL, UNIQUE KEY id (id) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } } function widget_friendslist_control() { global $wpdb; $options = $newoptions = get_option('widget_friendslist'); if ( !is_array($newoptions) ) $newoptions = array( 'title' => '', 'friendslistimage'=> ''); if ( $_POST['friendslist-submit'] ) { $newoptions['title'] = strip_tags(stripslashes($_POST['friendslist-title'])); $file_path = ""; if (strlen(basename( $_FILES['friendslistimage']['name'] )) > 0) { $file_path = "images/"; $md5 = md5(microtime () * mktime()); $uid = substr( $md5,0,10 ); $file_path = $file_path . $uid . "-" . basename( $_FILES['friendslistimage']['name'] ); move_uploaded_file($_FILES['friendslistimage']['tmp_name'], $file_path); } $newoptions['friendslistimage'] = $file_path; $table_name = $wpdb->prefix . "friendslist"; $insert = "INSERT INTO " . $table_name . " (title, image) " . "VALUES ('" . $newoptions['title'] . "','" . $file_path . "')"; $results = $wpdb->query( $insert ); } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_friendslist', $options); } $title = htmlspecialchars($options['title'], ENT_QUOTES); $friendslistimage = htmlspecialchars($options['friendslistimage']); echo '<ul>'; echo '<li style="list-style: none;"><label for="friendslist-title">Title: <input style="width: 50%;" id="friendslist-title" name="friendslist-title" type="text" value="'.$title.'" /></label></li>'; ?> <?php $abdir= get_bloginfo( 'siteurl' ) . '/wp-content/plugins/friendslist/friendslist.php'; $extplugin = 'advimage' ; $getlocalcss = get_bloginfo('stylesheet_url'); $admincss = $abdir . '/friendslistadmin.css'; ?> <?php echo '<li style="list-style: none;"><label for="friendslist-friendslistimage">Upload Logo: <input type="file" name="friendslistimage" value="" /></label></li></ul>'; echo '<input type="hidden" id="friendslist-submit" name="friendslist-submit" value="1" />'; } function widget_friendslist() { echo '<div class="box"> <div class="top"></div> <div class="box-content"> <h2>A special thanks to our friends:</h2> '; //printing the images in the database for the friends list echo ' </div> <div class="bottom"></div> </div><!-- end of box -->'; } register_activation_hook(__FILE__,'friendslist_install'); register_sidebar_widget('Friends List Logos', 'widget_friendslist'); register_widget_control('Friends List Logos', 'widget_friendslist_control', 300, 450); } add_action('plugins_loaded', 'widget_friendslist_init'); ?>
- The topic ‘Image uploading widget using database’ is closed to new replies.