我将首先警告您,我的编程技能有限。
我需要为user\\u id=21的用户创建一个仪表板小部件,该小部件将显示在WP仪表板中。
到目前为止,我已经尝试了以下代码,但不起作用:
function my_catdb() {
echo \'<a href="http://xxxxxxxxxxx.org/?frm_display=single-catproblem">View the Problem Problem Database.</a>\';
}
/**
* add Dashboard Widget via function wp_add_dashboard_widget()
*/
function my_catdb_setup() {
wp_add_dashboard_widget( \'my_catdb\', __( \'Cataloging Problem Database\' ), \'my_catdb\' );
}
/**
* use hook, to integrate new widget
*/
$user_id = get_current_user_id();
if ( is_user_logged_in() ) {
if ( $user_id = 21 ) {
add_action(\'wp_dashboard_setup\', \'my_wp_dashboard_setup\');
}
}
非常感谢您的帮助。谢谢
SO网友:fuxia
你必须等待wp_loaded
使用get_current_user_id()
.
示例:
add_action( \'wp_loaded\', \'wpse_80061_load_dashboard_widget\' );
function wpse_80061_load_dashboard_widget()
{
if ( 1 === get_current_user_id() )
add_action( \'wp_dashboard_setup\', \'wpse_80061_add_dashboard_widget\' );
// your function:
// add_action( \'wp_dashboard_setup\', \'my_catdb_setup\' );
}
function wpse_80061_add_dashboard_widget()
{
wp_add_dashboard_widget(
\'wpse_80061_widget\',
\'Hey!\',
\'wpse_80061_render_dashboard_widget\'
);
}
function wpse_80061_render_dashboard_widget()
{
echo \'hey!\';
}
有关仪表板小部件的更多详细信息,请参阅我的插件
T5 Table size dashboard widget.