Why isn’t admin_notices displaying my text? [Simple Plugin, Beginner]

Here is some code that is the result of four days of studying WordPress and PHP. Unfortunately, it’s not working the way it’s supposed to. Nothing displays at the admin_notices bar.

Originally, I thought it was the $filename = is_plugin_active($filename) ? “$filename: Active” : “$filename: Disabled”; line, but that was not the problem. What say you?

function show_names()
{                  
  $paths = array();

  foreach (glob("*/plugins/*") as $filename) {                                                                                                          
    $filename = is_plugin_active($filename) ? "$filename: Active" : "$filename: Disabled";
    $filename = str_replace('wp-content/plugins/', '', $filename);
    $paths[] = $filename;   
    }

  $paths = implode("---", $paths);
  echo $paths;
}

add_action("admin_notices", "show_names");

Related posts

Leave a Reply

1 comment

  1. var_dump($paths); shows that the your $paths variable is an empty string. It’s not showing anything because there is nothing to show.

    Since you seem like you’re trying to find all the plugins, you should have a look at get_plugins. glob is going to be relative to the current working directory (with will vary depending on your server setup) and is_plugin_active active takes a plugin_basename.

    get_plugins will return an associative array with the plugin basenames as keys as the file header data as the values (in an array).

    A few other notes:

    admin_notices does not automagically format your notices to look pretty. You can wrap it your notice with <div class="error"> or <div class="updated"> to do that.

    Always give your functions a unique prefix or put them in a namespace (PHP 5.3+ only).

    Revised code:

    <?php
    add_action('admin_notices', 'wpse72637_show_names');
    function wpse72637_show_names()
    {                  
        $paths = array();
    
        foreach(get_plugins() as $p_basename => $plugin)
        {
            $paths[] = "{$plugin['Name']}: " .
                (is_plugin_active($p_basename) ? 'Active' : 'Disabled');
        }
    
        echo '<div class="updated"><p>', implode(' --- ', $paths), '<p></div>';
    }