{"id":1730597,"date":"2010-10-21T00:23:09","date_gmt":"2010-10-21T00:23:09","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/widget-to-work-in-conjunction-with-existing-plugin\/"},"modified":"2016-08-19T20:08:07","modified_gmt":"2016-08-19T20:08:07","slug":"widget-to-work-in-conjunction-with-existing-plugin","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/widget-to-work-in-conjunction-with-existing-plugin\/","title":{"rendered":"Widget to work in conjunction with existing plugin"},"content":{"rendered":"<p>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&#8217;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.<\/p>\n<p>I&#8217;d love to share the link with you but I&#8217;m currently working on this locally with XAMPP, but if you require more information just let me know. I&#8217;ve read almost every page in the wp plugin docs and I&#8217;ve done approx 30 googles for tutorials and examples. I&#8217;ve just about pulled out all my hair and any help would be greatly appreciated.<\/p>\n<pre><code>&lt;?php\n\/*\n * Plugin Name: Portfolio Widget\n * Version: 1.0\n * Plugin URI:\n * Description: Widget designed specifically for BernadetteMcCarthy.com to work in conjunction with the WP-Portfolio plugin.\n * Author: Bernadette McCarthy\n * Author URI: http:\/\/bernadettemccarthy.com\/\n *\/\nclass bdm_portfolio_widget extends WP_Widget {\n    function bdm_portfolio_widget() {\n        parent::WP_Widget(false, $name=&#039;Recent Projects Widget&#039;);\n    } \/\/close bdm_portfolio_widget()\n\n    function widget ($args, $instance){\n        global $wpdb;\n        extract($args);\n        $sort = $this-&gt;update($instance[&#039;sort&#039;]);\n        $limit = $this-&gt;update($instance[&#039;limit&#039;]);\n        echo $before_widget;\n        echo $before_title.&#039;Recently Launched Projects&#039;.$after_title;\n        echo &#039;&lt;ul id=&quot;recently-launched&quot;&gt;&#039;;\n        $results = $wpdb-&gt;get_results(&quot;SELECT * FROM wp_wpportfolio_websites ORDER BY siteid DESC LIMIT 0,1&quot;,ARRAY_A);\n        foreach ($results AS $result) {\n            $siteid = $result[&#039;siteid&#039;];\n            $sitename = $result[&#039;sitename&#039;];\n            $siteurl = $result[&#039;siteurl&#039;];\n            echo &#039;&lt;li&gt;&lt;a href=&quot;&#039;.$siteurl.&#039;&quot; target=&quot;_blank&quot;&gt;&#039;.$sitename.&#039;&lt;\/a&gt;&lt;\/li&gt;&#039;;\n        }\n        echo &#039;&lt;\/ul&gt;&#039;;\n        echo $after_widget;\n    } \/\/close widget()\n\n    function update ($new_instance,$old_instance){\n        $instance = $old_instance;\n        $instance[&#039;sort&#039;] = $new_instance[&#039;sort&#039;];\n        $instance[&#039;limit&#039;] = $new_instance[&#039;limit&#039;];\n        return $instance;\n    } \/\/close update()\n\n    function form ($instance){\n        $instance = wp_parse_args((array) $instance, array(&#039;sort&#039;=&gt;&#039;siteid&#039;,&#039;limit&#039;=&gt;1));\n        $sort = htmlspecialchars($instance[&#039;sort&#039;]);\n        $limit = htmlspecialchars($instance[&#039;limit&#039;]);\n        echo &#039;&lt;p style=&quot;text-align:left;&quot;&gt;&lt;label for=&quot;&#039;.$this-&gt;get_field_name(&#039;sort&#039;).&#039;&quot;&gt;&#039;.__(&#039;Sort By: &#039;).&#039;&lt;select name=&quot;&#039;.$this-&gt;get_field_name(&#039;sort&#039;).&#039;&quot; id=&quot;&#039;.$this-&gt;get_field_id(&#039;sort&#039;).&#039;&quot;&gt;&lt;option value=&quot;siteid&quot;&gt;Site ID&lt;\/option&gt;&lt;option value=&quot;sitename&quot;&gt;Site Name&lt;\/option&gt;&lt;option value=&quot;siteurl&quot;&gt;Site URL&lt;\/option&gt;&lt;\/select&gt;&lt;\/label&gt;&lt;\/p&gt;&#039;;\n        echo &#039;&lt;p style=&quot;text-align:left;&quot;&gt;&lt;label for=&quot;&#039;.$this-&gt;get_field_name(&#039;limit&#039;).&#039;&quot;&gt;&#039;.__(&#039;Limit To: &#039;).&#039;&lt;input name=&quot;&#039;.$this-&gt;get_field_name(&#039;limit&#039;).&#039;&quot; id=&quot;&#039;.$this-&gt;get_field_id(&#039;limit&#039;).&#039;&quot; size=&quot;5&quot; \/&gt;&lt;\/label&gt;&lt;\/p&gt;&#039;;\n    } \/\/close form()\n} \/\/close class\n\nfunction bdm_portfolio_widgetInit() {\n    register_widget(&#039;bdm_portfolio_widget&#039;);\n} \/\/close bdm_portfolio_widgetInit()\n\nadd_action(&#039;widgets_init&#039;,&#039;bdm_portfolio_widgetInit&#039;);\n?&gt;<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-1730597","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1730597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1730597\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=1730597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}