0

I am making my first dashboard widget and it is displaying successfully but the output HTML from the function below is showing up before the opening HTML tag on dashboard page.

The code involved is below. Do I need some init call prior to the add_action() call?

function example_add_dashboard_widgets() {
    wp_add_dashboard_widget(
                 'donationsmtcarmel',         // Widget slug.
                 'Mt. Carmel Donations',         // Title.
                 'draw_widget' // Display function.
        );  
 }
add_action( 'wp_dashboard_setup', 'draw_widget' );


function draw_widget() {
    $dObj = new Donation;
    $donations = $dObj->retrieve("`paypal_txn` != '' AND `archived` = '0' ORDER BY `date` DESC");
    echo '
      <div id="normal-sortables-20" class="meta-box-sortables ui-sortable"><div id="donations_postbox" class="postbox" style="margin-top:12px">
        <div class="handlediv" title="Click to toggle"><br></div><h3 class="hndle"><span style="padding-left:12px">Mt. Carmel Donations</span></h3>
          <div class="inside">
            <div class="table table_content" style="padding-left:12px">';
     echo "<p><strong>Recent Donations:</strong></p><p style='padding-left:12px'>";
     if ($donations) { 
        foreach ($donations as $d) {
            echo $d->donor_name.'&nbsp;&nbsp;&nbsp;$'.$d->tx()->mc_gross.'&nbsp;&nbsp;&nbsp;'.date('n/j/Y g:i A', strtotime($d->date))."<br/>\n";
        }
     } else {
         echo 'None.';
     }

     echo '   </p>
              <p><a href="http://www.foo.org/wp-admin/index.php?page=donations">Donations Page</a></p>
            </div>
         </div>
        </div>
       </div>
      ';

}

1 Answer 1

3

Your add_action should call the function that registers the dashboard widget, which then in turn calls the function to render the widget. Right now you have the add_action calling the function to render the widget directly.

change:

add_action( 'wp_dashboard_setup', 'draw_widget' );

to:

add_action( 'wp_dashboard_setup', 'example_add_dashboard_widgets' );
1
  • Thank you! Feel foolish since wp_dashboard_setup() is so self-explanatory. Much obliged! Oct 22, 2013 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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