在窗口小部件中使用Wordpress插件功能

时间:2014-08-24 21:16:17

标签: php wordpress-plugin wordpress

我有一个Wordpress插件,其中包含一个处理函数的文件(userinfo.php) - generateUserInfo()。我想在侧边栏小部件上使用此功能。我不确定如何完成这项工作。

以下是我的插件中userinfo.php的代码。

require_once("../../../wp-load.php");

//need the current user's ID
$user = get_current_user_id();

$meetings = generateUserInfo($user, 4, 3, 'meetings');  
$events = generateUserInfo($user, 6, 2, 'events');  

function generateUserInfo($user,$taxonomy_id,$num_required_events,$event_type){
global $wpdb, $table_prefix;

$num_total_events = 0;
$num_actual_events = 0;

//Get the name of the taxonomy term for events (ID: 6)
$events = get_term_by('term_taxonomy_id', $taxonomy_id, 'events_categories');
$event_category_name = $events->name;

$event_posts = get_posts(array('post_type' => 'ai1ec_event', 'events_categories' => $event_category_name, 'posts_per_page' => -1));
foreach($event_posts as $event){
    $attended_events_query = $wpdb->get_results("SELECT vote FROM ".$table_prefix."attendance_list WHERE post = " . $event->ID . " AND user = " . $user);

    if(count($attended_events_query)>0 && is_array($attended_events_query)) {
        foreach($attended_events_query as $evt){
            //add to the acutal events attended
            if($evt->vote == 1) $num_actual_events++;
        }
    }   
    $num_total_events++;
}

$events_msg = '<div>You have attended <br> <span class="big-num">'. $num_actual_events . '</span> / ' . $num_total_events . ' ' . $event_type . '</br>('. $num_required_events.' required)<br><br></div>';

echo $events_msg;

return $events_msg;
}

这是我的小部件的开头:

 function your_widget_display($args) {
   extract($args);
   echo $before_widget;
   echo $before_title . 'Attendance Tracker' . $after_title;
   echo $after_widget;
   // print some HTML for the widget to display here
   echo 'whatever';
}

wp_register_sidebar_widget(
    'yac_attendance_widget',        // your unique widget id
    'YAC Member Attendance Tracker',          // widget name
    'your_widget_display',  // callback function
    array(                  // options
        'description' => 'Description of what your widget does'
    )
);

1 个答案:

答案 0 :(得分:0)

echo $after_widget;之前,添加以下内容

if(function_exists('generateUserInfo')) {
   generateUserInfo();
}