你知道WP指针的概念吗?
下面是一个在仪表板上引入指针的简短示例。
PHP将其放入functions.php
如果您在主题中定义了CPT,或者如果您在其中定义了CPT,则将其放入插件中:
add_action(\'admin_enqueue_scripts\', \'my_pointer_load\', 1000);
function my_pointer_load($hook_suffix) {
// Don\'t run on WP < 3.3
if (get_bloginfo(\'version\') < \'3.3\') return;
$screen = get_current_screen();
$screen_id = $screen->id;
$pointers = apply_filters(\'my_admin_pointers-\'.$screen_id, array());
if (! $pointers || ! is_array($pointers)) return;
// Get dismissed pointers
$dismissed = explode(\',\', (string) get_user_meta(get_current_user_id(),
\'dismissed_wp_pointers\', true));
$valid_pointers = array();
// Check pointers and remove dismissed ones.
foreach ($pointers as $pointer_id => $pointer) {
// Sanity check
if (in_array($pointer_id, $dismissed)
|| empty($pointer)
|| empty($pointer_id)
|| empty($pointer[\'target\'])
|| empty($pointer[\'options\'])
)
continue;
$pointer[\'pointer_id\'] = $pointer_id;
// Add the pointer to $valid_pointers array
$valid_pointers[\'pointers\'][] = $pointer;
}
// No valid pointers? Stop here.
if (empty($valid_pointers)) return;
wp_enqueue_style(\'wp-pointer\');
wp_enqueue_script(
\'my-wp-pointer\',
// For a plugin use plugins_url(\'js/my-wp-pointer.js\', __FILE__)
get_stylesheet_directory_uri().\'/js/my-wp-pointer.js\',
array(\'wp-pointer\')
);
wp_localize_script(\'my-wp-pointer\', \'myWPPointer\', $valid_pointers);
}
// Let\'s add a pointer to the dashboard
add_filter(\'my_admin_pointers-dashboard\', \'my_register_pointer\');
function my_register_pointer($p) {
$p[\'some_pointer_id\'] = array(
// What element do you want to explain?
// Use class (e.g., \'.wrap\') or ID (e.g., \'#dashboard_right_now\')
\'target\' => \'.wrap\',
\'options\' => array(
\'content\' => "<h3>Title</h3><p>What is it that you\'d like to tell?</p>",
\'position\' => array(
\'edge\' => \'top\',
\'align\' => \'middle\',
)
)
);
return $p;
}
如果您想要一个有点烦人和粘人的指针,请从上面删除已解除的代码
my_pointer_load
作用
JavaScript将以下内容放入/js/my-wp-pointer.js
文件(或其他地方)并调整上述内容wp_enqueue_script
):
jQuery(document).ready(function($) {
my_open_pointer(0);
function my_open_pointer(i) {
pointer = myWPPointer.pointers[i];
options = $.extend(pointer.options, {
close: function() {
$.post(ajaxurl, {
pointer: pointer.pointer_id,
action: \'dismiss-wp-pointer\'
});
}
});
$(pointer.target).pointer(options).pointer(\'open\');
}
});
Note: 此答案基于
this tutorial.