0

I am using the below two functions to add and show the Columns Content for the Custom Post in my Plugin

    add_filter("manage_{$this->post_slug}_posts_columns", array($this, 'browseColumns'));
add_action("manage_{$this->post_slug}_posts_custom_column", array($this, 'browseCustomColumns'), 10, 2);

 public function browseColumns($columns) {
$new = array();
foreach ($columns as $key => $title) {
    if ($key == 'date') {
    $new['wp_type'] = __('Type', self::$lang_slug);
    $new['wp_cost'] = __('Cost', self::$lang_slug);
    }
    $new[$key] = $title;
}
return $new;
}

   public function browseCustomColumns($column, $post_id) {
switch ($column) {
    case 'wp_type' :
    $type = get_post_meta($post_id, '_wp_type', true);
    echo empty($type) ? '-' : (int) $type;
    break;
    case 'wp_cost' :
    $cost = PluginClass::getCost($post_id);
    echo empty($cost) ? '-' : (int) $cost;
    break;

}
}

Below is the code for adding a Dashboard Widget for the Custom Plugin

/*
 *
 * Add a widget to the dashboard.
 *
 * This function is hooked into the 'add_dashboard_setup' action below.
 */

add_action('wp_dashboard_setup', array($this, 'add_dashboard_widget'));

  public function add_dashboard_widget() {
    add_meta_box('idx_dashboard_widget', 'WP WebinarSystem', array($this, 'compile_dashboard_widget'), 'dashboard', 'normal', 'high' );
}

/*
 *
 * Function to output the contents of our Dashboard Widget.
 *
 */
public function compile_dashboard_widget()
{
    echo $this->dashboard_widget_html();
    $this->loadPluginScripts();
    $this->loadFrontScripts();
}

public function dashboard_widget_html()
{


}

I want to display the Type and Cost Columns for my Custom Post into the Dashboard Widget.

I want to know if there are any hooks available in order to reuse or should I proceed writing the html ?

0

To answer your specific question: no, there are no hooks that would allow you to "reuse". However, there is nothing keeping you from calling your browseCustomColumns() function in your function that renders the dashboard widget. However, in order to do that, you'd have to make a minor change your compile_dashboard_widget() function:

public
function
compile_dashboard_widget ()
{
    // removed the call to echo, just call $this->dashboard_widget_html () and have
    // $this->dashboard_widget_html () do all the echo'ing
    $this->dashboard_widget_html () ;
    $this->loadPluginScripts () ;
    $this->loadFrontScripts () ;

    return ;
}

public
function
dashboard_widget_html ()
{
    global $post ;

    $args = array (
        'post_type' => $this->post_slug,
        // any other WP_Query args you want
        ) ;
    $posts = new WP_Query ($args) ;

    if ($posts->has_posts ()) {
        while ($posts->has_posts ()) {
            $posts->the_post () ;

            // you'd probably want addition markup here
            // (e.g., a table, which each post in a <tr> and title/type/cost as <td>)
            // but you can figure out how you want your dashboard widget to look
            the_title () ;

            $this->browseCustomColumns ('wp_type', $post->ID) ;
            $this->browseCustomColumns ('wp_cost', $post->ID) ;
            }
        }
    else {
        echo 'no custom posts' ;
        }

    wp_reset_postdata () ;

    return ;
}

Hope this helps.

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.