• Resolved danielsolsmith

    (@danielsolsmith)


    Issue: Fatal Error in bp-profile-search Plugin – has_cap() on Null Role

    Description:

    I’m experiencing a fatal error in the bp-profile-search plugin, specifically in bps-start.php on line 132. The issue occurs because get_role(‘administrator’) is returning null, causing a call to has_cap() on a non-object.

    Error Log Output:

    [06-Mar-2025 18:43:45 UTC] Checking role: administrator
    [06-Mar-2025 18:43:45 UTC] PHP Fatal error:  Uncaught Error: Call to a member function has_cap() on null in /opt/bitnami/apache/htdocs/wp-content/plugins/bp-profile-search/bps-start.php:132
    Stack trace:
    #0 /opt/bitnami/apache/htdocs/wp-includes/class-wp-hook.php(324): bps_post_type()
    #1 /opt/bitnami/apache/htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
    #2 /opt/bitnami/apache/htdocs/wp-includes/plugin.php(517): WP_Hook->do_action()
    #3 /opt/bitnami/apache/htdocs/wp-settings.php(704): do_action()
    #4 /opt/bitnami/apache/htdocs/wp-config.php(130): require_once('...')
    #5 /opt/bitnami/apache/htdocs/wp-load.php(50): require_once('...')
    #6 /opt/bitnami/apache/htdocs/wp-admin/admin-ajax.php(22): require_once('...')
    #7 {main}
      thrown in /opt/bitnami/apache/htdocs/wp-content/plugins/bp-profile-search/bps-start.php on line 132

    Steps to Reproduce:

    1. Enable the bp-profile-search plugin.

    2. Perform any action that triggers bps_post_type().

    3. The error occurs when get_role(‘administrator’) is executed.

    Possible Cause:

    ? The function is running before WordPress has fully initialized roles, leading to get_role() returning null.

    ? The issue is likely happening because bps-start.php calls get_role() before the init action.

    Suggested Fix:

    Ensure that get_role() is called after WordPress initializes roles, by wrapping it inside an init hook:

    add_action('init', function() {
        global $wp_roles;
        if (!isset($wp_roles)) {
            $wp_roles = new WP_Roles(); // Ensure roles are loaded
        }
    
        $role = get_role('administrator');
        if (!$role) {
            error_log("Error: 'administrator' role not found!");
            return; // Prevent fatal error
        }
    
        foreach ($form_caps['administrator'] as $cap) {
            if (!$role->has_cap($cap)) {
                $role->add_cap($cap);
            }
        }
    }, 20); // Run late to ensure roles exist

    Environment Details:

    ? Plugin Version: 5.8.1

    ? Hosting: Bitnami LAMP on AWS Lightsail

    Request:

    Could you please update the plugin to ensure that get_role() is called only after roles have been initialized? Let me know if you need more debugging information.

    Thanks!

    This should give the developer all the details they need. Let me know if you want to add anything else! ??

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Andrea Tarantini

    (@dontdream)

    Hi Daniel,

    Thanks for your report!

    Unfortunately I am unable to reproduce the issue. The code with the has_cap() call has not changed in years and is actually wrapped in an init action hook.

    I also see that the stack trace starts with an AJAX call. Can you share how to get that stack trace?

    Thread Starter danielsolsmith

    (@danielsolsmith)

    Hi, I think this issue is on my end.
    The admin role had gotten corrupted during a migration. I fixed that, and the plugin is working againl Thanks for your response!

    Plugin Author Andrea Tarantini

    (@dontdream)

    Thank you for replying, I’m glad you solved the problem!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.