I have just recently created a widget in my Wordpress theme that inserts a widget into the sidebar.
Its based upon the text widget. However the html content is hard coded into the widget and it has its own title, that way if the user ever decided to get rid of the widget out of the sidebar, then the widget would always be available for reuse in the "available widgets" section if they ever changed their mind and wanted it reinstating on that sidebar or another one.
Here is the code:
<?php
/* Plugin Name: Basic Text Widget
Plugin URI: www.lancscps.co.uk
Description: Text to put into the sidebar regarding free consultation
Version: 1.0
Author: Jonathan Beech
Author URI: www.lancscps.co.uk
*/
class freeconsultation extends WP_Widget {
function freeconsultation() {
$widget_ops = array(
'classname' => 'freeconsultation',
'description' => 'Text to put into the sidebar regarding free consultation'
);
$this->WP_Widget(
'freeconsultation',
'Free Consultation',
$widget_ops
);
}
function widget($args, $instance) { // widget sidebar output
extract($args, EXTR_SKIP);
echo $before_widget; // pre-widget code from theme
print <<<EOM
<div class="consultation">
<p>In order to establish how we can proceed together with your needs, our first meeting is free of charge.</p>
</div><!--end sub consultation-->
EOM;
echo $after_widget; // post-widget code from theme
}
}
add_action(
'widgets_init',
create_function('','return register_widget("freeconsultation");')
);
I was hoping there would be a method of inserting a custom post into the widget replacing the HTML that is already there. Perhaps this could be done by id so that the user could adjust the content of the post and therefore the widget as they see fit. Changing the code file wouldn't be feasible for the end user.