Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

You will need a "form" and an "update" method for your widget class.

  function form($instance) {
    $instance = wp_parse_args(
      (array) $instance,
      array ('show')
    );
    $show = (!empty($instance['show'])) ? $instance['show'] : '';
    echo '<input type="text" name="'.$this->get_field_name('show').'" value="'.esc_attr($show).'" />';
  }

  function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance['show'] = ($new_instance['show']) ? $new_instance['show'] : 1;
    return $instance;
  }

The "form" method creates the back-end form. The "update" method updates the database. Most of the work is handled by the parent widget class.

Then use your 'show' variable-- $instance['show'] to query for a post. I've included a default post to load.

function widget($args, $instance) { // widget sidebar output
    extract($args, EXTR_SKIP);
    echo $before_widget; // pre-widget code from theme
    $pid = (!empty($instance['show'])) ? $instance['show'] : 123; // 123 is a default
    $p = get_post($pid);
    // echo your formatted post however you want
    echo $after_widget; // post-widget code from theme
}

http://codex.wordpress.org/Function_Reference/get_post

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.