0

I have a plugin with a line calling wp_capabilities. This works with the default prefix. If someone changes the prefix to something else (bluehost scripts do this), how do I fix this code? Can you also explain why?

I can hard code it for the individual site but how do I make it 'universal'?

Change:

$current_user->data->wp_capabilities[$role_name];

To:

$current_user->data->wp_xyz_capabilities[$role_name];

I understand (and use) {$wpdb->prefix} in an SQL statement works fine.

I've looked at $table_prefix as well as $wpdb->prefix but the combinations I've tried in PHPStorm state expecting semicolons or red lines appear stating there is a problem.

Thank you for your time and explanation.

3
  • Change $table_prefix = 'wp_'; in your wp-config.php file? Commented May 4, 2014 at 7:09
  • If there are red lines indicating there is a problem - you must know what the actual problem is. Else it's hard to give you feedback on that issue. Commented May 4, 2014 at 7:11
  • I'm trying to make the one line universal - regardless of the prefix. currently that line hard codes wp_ and it needs to be changed so the plugin can be installed if any user has changed their prefix. All code I've tried leads to object errors - thus asking here - how can that line be changed.
    – LPH
    Commented May 4, 2014 at 12:58

2 Answers 2

1

The prefix refers only to the database tables prefix, other uses of the wp_ prefix in function, class, variable, (array) index or (class) property names are entirely unaffected and remain as wp_.

As such, so long as all plug-ins (and themes) you use avoid using SQL statements with a hardcoded wp_ prefix (and, as you point out, use $wpdb->prefix) you will be fine.

It's impossible to comment on what PHPStorm is flagging up, but it will be entirely unrelated to the change of table prefix.

3
  • Thank you but the key to my question is how to make the line of code universal, without hard coding wp_ . I mentioned phpstorm to explain I've tried things but failed. the focus is that line of code - not SQL, wp_config or other things people write about. I have not seen an answer on making the plugins php universal when someone changes the prefix.
    – LPH
    Commented May 4, 2014 at 13:03
  • 1
    Please reread my answer. The table prefix does not change the wp_ prefix of function names, array indices, class properties and the like. They are hardcoded. You can't, and you don't need to, change these to make your code universal. Also I don't think there is a wp_capabilities property of $current_user->data. Even if there was current_user->data->wp_capabilities[$role_name]; doesn't make sense. What are you trying to get? Commented May 4, 2014 at 13:16
  • When I say "hard coded" ... I mean wp_capabilities and not anyprefix_capabilities . Yes, my code works for anyone with a prefix of wp_. Instead, last night someone installed the plugin and I had to change wp_capabilities to theirprefix_capabilities in that line to get the role written to the database properly. Isn't there a way to write {$wpdb->prefix}capabilities but not toss an object error?
    – LPH
    Commented May 4, 2014 at 15:39
0

Here:

$current_user->data->wp_capabilities[$role_name];

wp_capabilities is not a WordPress table or prefix. So please keep it as it is. When you are use any query for fetch data from table at that time you need to add prefix ( $wpdb->prefix ) of table. In your case it's not needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.