• Resolved Dekadinious

    (@dekadinious)


    I have made a helper function that takes two arguments. A message, and type of notice. This helper function displays the message either as a WooCommerce Notice if applicable, or in the case of the action happening on an admin page, a normal admin notice.

    My problem is that when notices fire inside wp_pre_insert_user_data, I can’t get them to show in the admin. I have even tried passing the message by adding query args to the URL, but those do not appear.

    How can I get admin notices to show when they need to be added inside wp_pre_insert_user_data?

Viewing 1 replies (of 1 total)
  • Thread Starter Dekadinious

    (@dekadinious)

    Solved, but there are probably better ways?

    /**
     * Notice utility function. Adds WC_notice if not admin page, adds admin notice if admin page
     */
    
    function dd_notice($message, $type, $userid)
    {
        if (is_admin()) {
            update_option('dd_notice', $message);
            update_option('dd_notice_userid', $userid);
        } else {
            wc_add_notice(__($message, 'woocommerce'), $type);
        }
    }
    
    function dd_add_notice()
    {
        if (get_option('dd_notice')) {
    
            //Get the URL of the current page
            $currentUrl = wp_get_referer();
    
            //Check if URL contains query parameters and set ? or & as character
            if (!strpos($currentUrl, '?')) {
                $queryChar = '?';
            } else {
                $queryChar = '&';
            }
    
            //Notice to be added
            echo sprintf(
                '<div class="notice notice-error"><p>Notice for %s: %s</p><p><a href = "%s%sdd_remove_notice">Remove notice</a></p></div>',
                get_user_meta(get_option('dd_notice_userid'), 'billing_email', true),
                get_option('dd_notice'),
                $currentUrl,
                $queryChar
            );
        }
    }
    add_action('admin_notices', 'dd_add_notice');
    
    //Remove notice if remove link was clicked
    function dd_remove_notice()
    {
        if (isset($_GET['dd_remove_notice'])) {
            delete_option('dd_notice');
        }
    }
    add_action('init', 'dd_remove_notice');
Viewing 1 replies (of 1 total)
  • The topic ‘Can’t get admin notices to display’ is closed to new replies.