อ่า .. ใช่แล้ว ตัวชี้ WordPress คุณรู้ไหมว่ามันมีความรู้สึกปนกันมากมายเมื่อพูดถึงการใช้พอยน์เตอร์)
คุณกำลังติดตามถูกต้องด้วยรหัสของคุณข้างต้น แต่มีปัญหาอยู่สองสามข้อ
@GM ถูกต้องเกี่ยวกับpointer('open')คำสั่งที่เปิดตัวชี้ทั้งหมดของคุณในครั้งเดียว นอกจากนี้คุณไม่ได้ให้วิธีการล่วงหน้าผ่านตัวชี้
ฉันต่อสู้ในประเด็นเดียวกันนี้และเกิดขึ้นกับแนวทางของฉันเอง ฉันใช้ตัวแปรแบบสอบถามใน url โหลดหน้าไปยังหน้าผู้ดูแลระบบที่ฉันต้องการแสดงตัวชี้ถัดไปและให้ jQuery จัดการส่วนที่เหลือ
WP Pointers Class
ฉันตัดสินใจที่จะเขียนสิ่งนี้เป็นชั้นเรียน แต่ฉันจะแสดงทีละน้อยเพื่อช่วยให้คุณเข้าใจสิ่งที่เกิดขึ้นดีขึ้น
การเริ่มชั้นเรียน
// Create as a class
class testWPpointers {
    // Define pointer version
    const DISPLAY_VERSION = 'v1.0';
    // Initiate construct
    function __construct () {
        add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));  // Hook to admin_enqueue_scripts
    }
    function admin_enqueue_scripts () {
        // Check to see if user has already dismissed the pointer tour
        $dismissed = explode (',', get_user_meta (wp_get_current_user ()->ID, 'dismissed_wp_pointers', true));
        $do_tour = !in_array ('test_wp_pointer', $dismissed);
        // If not, we are good to continue
        if ($do_tour) {
            // Enqueue necessary WP scripts and styles
            wp_enqueue_style ('wp-pointer');
            wp_enqueue_script ('wp-pointer');
            // Finish hooking to WP admin areas
            add_action('admin_print_footer_scripts', array($this, 'admin_print_footer_scripts'));  // Hook to admin footer scripts
            add_action('admin_head', array($this, 'admin_head'));  // Hook to admin head
        }
    }
    // Used to add spacing between the two buttons in the pointer overlay window.
    function admin_head () {
        ?>
        <style type="text/css" media="screen">
            #pointer-primary {
                margin: 0 5px 0 0;
            }
        </style>
        <?php
    }
- เราได้กำหนดชั้นเรียน
- admin_enqueue_scriptsเราสร้างชั้นเรียนและเพิ่มการดำเนินการไปยัง
- เราพิจารณาแล้วว่าตัวชี้ของเราถูกไล่ออกไปแล้ว
- ถ้าไม่เราจะทำการเข้าคิวสคริปต์ที่จำเป็นต่อไป
คุณไม่จำเป็นต้องเปลี่ยนแปลงอะไรในฟังก์ชั่นแรกเหล่านี้
การตั้งค่าอาร์เรย์ของรายการตัวชี้
ขั้นตอนต่อไปคือการกำหนดพอยน์เตอร์แต่ละตัว มีห้ารายการที่เราจำเป็นต้องกำหนด (ยกเว้นสำหรับตัวชี้ล่าสุด) เราจะทำสิ่งนี้โดยใช้อาร์เรย์ ลองดูฟังก์ชั่น:
// Define footer scripts
function admin_print_footer_scripts () {
    // Define global variables
    global $pagenow;
    global $current_user;
    //*****************************************************************************************************
    // This is our array of individual pointers.
    // -- The array key should be unique.  It is what will be used to 'advance' to the next pointer.
    // -- The 'id' should correspond to an html element id on the page.
    // -- The 'content' will be displayed inside the pointer overlay window.
    // -- The 'button2' is the text to show for the 'action' button in the pointer overlay window.
    // -- The 'function' is the method used to reload the window (or relocate to a new window).
    //    This also creates a query variable to add to the end of the url.
    //    The query variable is used to determine which pointer to display.
    //*****************************************************************************************************
    $tour = array (
        'quick_press' => array (
            'id' => '#dashboard_quick_press',
            'content' => '<h3>' . __('Congratulations!', 'test_lang') . '</h3>'
                . '<p><strong>' . __('WP Pointers is working properly.', 'test_lang') . '</strong></p>'
                . '<p>' . __('This pointer is attached to the "Quick Draft" admin widget.', 'test_lang') . '</p>'
                . '<p>' . __('Our next pointer will take us to the "Settings" admin menu.', 'test_lang') . '</p>',
            'button2' => __('Next', 'test_lang'),
            'function' => 'window.location="' . $this->get_admin_url('options-general.php', 'site_title') . '"'  // We are relocating to "Settings" page with the 'site_title' query var
            ),
        'site_title' => array (
            'id' => '#blogname',
            'content' => '<h3>' . __('Moving along to Site Title.', 'test_lang') . '</h3>'
            . '<p><strong>' . __('Another WP Pointer.', 'test_lang') . '</strong></p>'
            . '<p>' . __('This pointer is attached to the "Blog Title" input field.', 'test_lang') . '</p>',
            'button2' => __('Next', 'test_lang'),
            'function' => 'window.location="' . $this->get_admin_url('index.php', 'quick_press_last') . '"'  // We are relocating back to "Dashboard" with 'quick_press_last' query var
            ),
        'quick_press_last' => array (
            'id' => '#dashboard_quick_press',
            'content' => '<h3>' . __('This concludes our WP Pointers tour.', 'test_lang') . '</h3>'
            . '<p><strong>' . __('Last WP Pointer.', 'test_lang') . '</strong></p>'
            . '<p>' . __('When closing the pointer tour; it will be saved in the users custom meta.  The tour will NOT be shown to that user again.', 'test_lang') . '</p>'
            )
        );
    // Determine which tab is set in the query variable
    $tab = isset($_GET['tab']) ? $_GET['tab'] : '';
    // Define other variables
    $function = '';
    $button2 = '';
    $options = array ();
    $show_pointer = false;
    // *******************************************************************************************************
    // This will be the first pointer shown to the user.
    // If no query variable is set in the url.. then the 'tab' cannot be determined... and we start with this pointer.
    // *******************************************************************************************************
    if (!array_key_exists($tab, $tour)) {
        $show_pointer = true;
        $file_error = true;
        $id = '#dashboard_right_now';  // Define ID used on page html element where we want to display pointer
        $content = '<h3>' . sprintf (__('Test WP Pointers %s', 'test_lang'), self::DISPLAY_VERSION) . '</h3>';
        $content .= __('<p>Welcome to Test WP Pointers admin tour!</p>', 'test_lang');
        $content .= __('<p>This pointer is attached to the "At a Glance" dashboard widget.</p>', 'test_lang');
        $content .= '<p>' . __('Click the <em>Begin Tour</em> button to get started.', 'test_lang' ) . '</p>';
        $options = array (
            'content' => $content,
            'position' => array ('edge' => 'top', 'align' => 'left')
            );
        $button2 = __('Begin Tour', 'test_lang' );
        $function = 'document.location="' . $this->get_admin_url('index.php', 'quick_press') . '";';
    }
    // Else if the 'tab' is set in the query variable.. then we can determine which pointer to display
    else {
        if ($tab != '' && in_array ($tab, array_keys ($tour))) {
            $show_pointer = true;
            if (isset ($tour[$tab]['id'])) {
                $id = $tour[$tab]['id'];
            }
            $options = array (
                'content' => $tour[$tab]['content'],
                'position' => array ('edge' => 'top', 'align' => 'left')
            );
            $button2 = false;
            $function = '';
            if (isset ($tour[$tab]['button2'])) {
                $button2 = $tour[$tab]['button2'];
            }
            if (isset ($tour[$tab]['function'])) {
                $function = $tour[$tab]['function'];
            }
        }
    }
    // If we are showing a pointer... let's load the jQuery.
    if ($show_pointer) {
        $this->make_pointer_script ($id, $options, __('Close', 'test_lang'), $button2, $function);
    }
}
โอเค .. ลองมาดูบางสิ่งที่นี่
อันดับแรก$tourอาร์เรย์ของเรา นี่คืออาร์เรย์ที่เก็บพอยน์เตอร์ทั้งหมดยกเว้นตัวชี้แรกที่แสดงต่อผู้ใช้ (เพิ่มเติมในภายหลัง) ดังนั้นคุณจะต้องเริ่มต้นด้วยตัวชี้ที่สองที่คุณต้องการแสดง .. และดำเนินการต่อจนถึงตัวชี้สุดท้าย
ต่อไปเรามีบางรายการที่สำคัญมาก
- $tourคีย์อาร์เรย์ต้องไม่ซ้ำกัน (quick_press, SITE_TITLE, quick_press_last นั้นเป็นตัวอย่างข้างต้น)
- คำสั่ง 'id' จะต้องตรงกับองค์ประกอบ html id ของรายการที่คุณต้องการแนบกับตัวชี้
- functionคำสั่งจะโหลด / ย้ายหน้าต่าง นี่คือสิ่งที่ใช้แสดงตัวชี้ถัดไป เราต้องโหลดหน้าต่างใหม่หรือย้ายไปที่หน้าผู้ดูแลระบบถัดไปซึ่งตัวชี้จะปรากฏขึ้น
- เราเรียกใช้get_admin_url()ฟังก์ชันด้วยตัวแปรสองตัว แรกคือหน้าผู้ดูแลระบบที่เราต้องการไปต่อไป; และที่สองคือคีย์อาร์เรย์ที่เป็นเอกลักษณ์ของตัวชี้ที่เราต้องการแสดง
if (!array_key_exists($tab, $tour)) {ต่อไปลงที่คุณจะเห็นรหัสที่ขึ้นต้น นี่คือที่เราตรวจสอบว่ามีการตั้งค่าตัวแปรแบบสอบถาม URL ถ้ามันไม่ได้เราต้องกำหนดตัวชี้แรกที่จะแสดง
ตัวชี้นี้ใช้id, content, button2, and functionรายการเดียวกับที่ใช้ใน$tourอาร์เรย์ด้านบน โปรดจำไว้ว่าอาร์กิวเมนต์ที่สองของget_admin_url()ฟังก์ชันจะต้องตรงกับคีย์อาร์เรย์ใน$tourตัวแปร นี่คือสิ่งที่บอกให้สคริปต์ไปที่ตัวชี้ถัดไป
ส่วนที่เหลือของฟังก์ชั่นจะใช้ถ้ามีการตั้งค่าตัวแปรแบบสอบถามไว้ใน URL แล้ว ไม่จำเป็นต้องปรับฟังก์ชั่นอีกต่อไป
การได้รับ URL ของผู้ดูแลระบบ 
ฟังก์ชั่นถัดไปเป็นฟังก์ชั่นตัวช่วยจริง ... ใช้เพื่อรับ URL ผู้ดูแลระบบและเลื่อนตัวชี้ไปข้างหน้า
// This function is used to reload the admin page.
// -- $page = the admin page we are passing (index.php or options-general.php)
// -- $tab = the NEXT pointer array key we want to display
function get_admin_url($page, $tab) {
    $url = admin_url();
    $url .= $page.'?tab='.$tab;
    return $url;
}
จำไว้ว่ามีสองข้อโต้แย้ง; หน้าผู้ดูแลระบบที่เรากำลังจะไป .. และแท็บ แท็บจะเป็น$tourคีย์อาร์เรย์ที่เราต้องการไปต่อไป  เหล่านี้ MATCH
ดังนั้นเมื่อเราเรียกใช้ฟังก์ชันget_admin_url()และส่งผ่านตัวแปรทั้งสอง ตัวแปรแรกจะกำหนดหน้าผู้ดูแลระบบต่อไป .. และตัวแปรที่สองกำหนดตัวชี้ที่จะแสดง
สุดท้าย ... ในที่สุดเราก็สามารถพิมพ์สคริปต์ผู้ดูแลระบบไปยังส่วนท้าย
// Print footer scripts
function make_pointer_script ($id, $options, $button1, $button2=false, $function='') {
    ?>
    <script type="text/javascript">
        (function ($) {
            // Define pointer options
            var wp_pointers_tour_opts = <?php echo json_encode ($options); ?>, setup;
            wp_pointers_tour_opts = $.extend (wp_pointers_tour_opts, {
                // Add 'Close' button
                buttons: function (event, t) {
                    button = jQuery ('<a id="pointer-close" class="button-secondary">' + '<?php echo $button1; ?>' + '</a>');
                    button.bind ('click.pointer', function () {
                        t.element.pointer ('close');
                    });
                    return button;
                },
                close: function () {
                    // Post to admin ajax to disable pointers when user clicks "Close"
                    $.post (ajaxurl, {
                        pointer: 'test_wp_pointer',
                        action: 'dismiss-wp-pointer'
                    });
                }
            });
            // This is used for our "button2" value above (advances the pointers)
            setup = function () {
                $('<?php echo $id; ?>').pointer(wp_pointers_tour_opts).pointer('open');
                <?php if ($button2) { ?>
                    jQuery ('#pointer-close').after ('<a id="pointer-primary" class="button-primary">' + '<?php echo $button2; ?>' + '</a>');
                    jQuery ('#pointer-primary').click (function () {
                        <?php echo $function; ?>  // Execute button2 function
                    });
                    jQuery ('#pointer-close').click (function () {
                        // Post to admin ajax to disable pointers when user clicks "Close"
                        $.post (ajaxurl, {
                            pointer: 'test_wp_pointer',
                            action: 'dismiss-wp-pointer'
                        });
                    })
                <?php } ?>
            };
            if (wp_pointers_tour_opts.position && wp_pointers_tour_opts.position.defer_loading) {
                $(window).bind('load.wp-pointers', setup);
            }
            else {
                setup ();
            }
        }) (jQuery);
    </script>
    <?php
}
} 
$testWPpointers = new testWPpointers();
อีกครั้งไม่จำเป็นต้องเปลี่ยนแปลงอะไรข้างต้น สคริปต์นี้จะกำหนดและส่งออกสองปุ่มในหน้าต่างซ้อนทับตัวชี้ หนึ่งจะเป็นปุ่ม "ปิด" เสมอ และจะอัปเดตdismissed_pointersตัวเลือกเมตาผู้ใช้ปัจจุบัน
ปุ่มที่สอง (ปุ่มการกระทำ) จะดำเนินการฟังก์ชั่น (วิธีการย้ายหน้าต่างของเรา)
และเราปิดชั้นเรียน
นี่คือรหัสที่ครบถ้วน
WP Pointer Class
คุณสามารถคัดลอก / วางที่ลงในไซต์ dev ของคุณและไปที่หน้า "แผงควบคุม" มันจะแนะนำคุณตลอดการเดินทาง
โปรดจำไว้ว่ามันสับสนเล็กน้อยที่ตัวชี้แรกถูกกำหนดครั้งสุดท้ายในรหัส นั่นคือวิธีที่มันควรจะทำงาน อาร์เรย์จะเก็บพอยน์เตอร์ที่เหลือทั้งหมดที่คุณต้องการใช้
โปรดจำไว้ว่ารายการอาร์เรย์ 'id' ต้องตรงกับอาร์กิวเมนต์ที่สองของget_admin_url()ฟังก์ชันจากคำสั่ง 'function' ของรายการอาร์เรย์ก่อนหน้า นี่คือวิธีที่พอยน์เตอร์ 'คุย' ซึ่งกันและกันและรู้วิธีก้าวหน้า
สนุก!! :)