WordPress.org

Support

Support » Plugins and Hacks » Hacks » Widget to work in conjunction with existing plugin

Widget to work in conjunction with existing plugin

  • In converting my portfolio site to a wp site, I came across the wp-portfolio plugin which is working perfectly. In the meanwhile I had come up with the nifty idea that I could create a widget (my first one!) that could display a given number of links for recently launched projects. The front end is currently displaying the most recent record (as it’s supposed to), however, I thought in the admin I could give myself an opportunity to update the sort and limit. The form displays just fine for the widget, but when I click on save nothing happens. Furthermore, after clicking save and then refreshing the front end it is still displaying the most recent project.

    I’d love to share the link with you but I’m currently working on this locally with XAMPP, but if you require more information just let me know. I’ve read almost every page in the wp plugin docs and I’ve done approx 30 googles for tutorials and examples. I’ve just about pulled out all my hair and any help would be greatly appreciated.

    <?php
    /*
     * Plugin Name: Portfolio Widget
     * Version: 1.0
     * Plugin URI:
     * Description: Widget designed specifically for BernadetteMcCarthy.com to work in conjunction with the WP-Portfolio plugin.
     * Author: Bernadette McCarthy
     * Author URI: http://bernadettemccarthy.com/
     */
    class bdm_portfolio_widget extends WP_Widget {
        function bdm_portfolio_widget() {
            parent::WP_Widget(false, $name='Recent Projects Widget');
        } //close bdm_portfolio_widget()
    
        function widget ($args, $instance){
            global $wpdb;
            extract($args);
            $sort = $this->update($instance['sort']);
            $limit = $this->update($instance['limit']);
            echo $before_widget;
            echo $before_title.'Recently Launched Projects'.$after_title;
            echo '<ul id="recently-launched">';
            $results = $wpdb->get_results("SELECT * FROM wp_wpportfolio_websites ORDER BY siteid DESC LIMIT 0,1",ARRAY_A);
            foreach ($results AS $result) {
                $siteid = $result['siteid'];
                $sitename = $result['sitename'];
                $siteurl = $result['siteurl'];
                echo '<li><a href="'.$siteurl.'" target="_blank">'.$sitename.'</a></li>';
            }
            echo '</ul>';
            echo $after_widget;
        } //close widget()
    
        function update ($new_instance,$old_instance){
            $instance = $old_instance;
            $instance['sort'] = $new_instance['sort'];
            $instance['limit'] = $new_instance['limit'];
            return $instance;
        } //close update()
    
        function form ($instance){
            $instance = wp_parse_args((array) $instance, array('sort'=>'siteid','limit'=>1));
            $sort = htmlspecialchars($instance['sort']);
            $limit = htmlspecialchars($instance['limit']);
            echo '<p style="text-align:left;"><label for="'.$this->get_field_name('sort').'">'.__('Sort By: ').'<select name="'.$this->get_field_name('sort').'" id="'.$this->get_field_id('sort').'"><option value="siteid">Site ID</option><option value="sitename">Site Name</option><option value="siteurl">Site URL</option></select></label></p>';
            echo '<p style="text-align:left;"><label for="'.$this->get_field_name('limit').'">'.__('Limit To: ').'<input name="'.$this->get_field_name('limit').'" id="'.$this->get_field_id('limit').'" size="5" /></label></p>';
        } //close form()
    } //close class
    
    function bdm_portfolio_widgetInit() {
        register_widget('bdm_portfolio_widget');
    } //close bdm_portfolio_widgetInit()
    
    add_action('widgets_init','bdm_portfolio_widgetInit');
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Apologies for the delay, I’ve been on holiday.

    The first thing I’d say is that accessing the database directly is asking for trouble! 🙂

    Check the documentation, and there’s actually an API that should help you considerably. There are plans to add widget to the plugin in the future, but it’s not been sponsored yet.

    Kind Regards
    Dan

    My advice, copy one of the widgets included with WordPress, but change the names around .. that way you can start from a working code base and make changes as you go… if it stops working at any point, you can simply retrace the changes you’ve made.. (that’s how i’ve learn alot of what i know). Add in small chunks at a time and test as you go..

    I can’t personally see an issue with your WPDB call, i can’t think what else exists in WP that would be better suited to querying custom tables (unless the plugin has it’s own DB functions for this kind of thing).

    Widgets API (though i’d guess you’ve seen this already):
    http://codex.wordpress.org/Widgets_API

    Source for Default Widgets (as a reference for examples):
    https://core.trac.wordpress.org/browser/trunk/wp-includes/default-widgets.php

    The plugin has it’s own DB calls, yes. It’s also a planned feature.

    Dan

    The plugin has it’s own DB calls, yes. It’s also a planned feature.

    Hi Dan,

    Just for clarity, are you saying the plugin has functions for querying the custom table? and if so, would you mind providing a little info for the user? Some basic instructions on how to call and use them, or even simply point to some documentation if it’s available, please..

    Hi Dan,

    Just for clarity, are you saying the plugin has functions for querying the custom table? and if so, would you mind providing a little info for the user? Some basic instructions on how to call and use them, or even simply point to some documentation if it’s available, please..

    Yes, there are documented APIs for all aspects to do with the plugin. All in the admin area of the plugin once activated (due to the markup to explain the API more clearly than a readme.txt file will allow).

    In admin area: WP Portfolio -> Documentation

    WPPortfolio_getAllPortfolioAsHTML()
    WPPortfolio_getRandomPortfolioSelectionAsHTML

    Fully documented with parameters and examples.

    Dan

    Thanks for that Dan. 😉

    Shiznit, let us know if you get stuck… 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Widget to work in conjunction with existing plugin’ is closed to new replies.
Skip to toolbar