• Resolved lucspe

    (@lucspe)


    Hi,

    I understand that is possible to change the general capability for accessing Business Profile (from: https://gist.github.com/NateWr/ba547879b3e6357cc3a47d8666f134eb), however what if I wanted to prevent specific access to a few settings, like for example modifying the Google Maps API key or enable/disable multiple locations.

    In other words, I don’t know/understand how could I use the bpfwp_settings_page filter to do that, if possible at all.

    Thanks

    • This topic was modified 7 years, 9 months ago by lucspe. Reason: missing word
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @lucspe,

    The short answer is that you can’t. These settings use WordPress’s Settings API, and it only really takes a user capability argument on a per-page basis.

    Furthermore, all of these settings are saved in a single serialized entry in the database. So even if you hooked into bpfwp_settings_page and removed those fields for users without a certain capability, they’d end up overwriting the data in the database.

    I presume what you want to do is prevent a client or lower-level manager from making changes. I can see two possible solutions to this.

    First, the simplest thing to do would be to just enqueue some CSS on that page in the admin which hides those settings. It wouldn’t be safe from a malicious user, who could still uncover and modify those settings. But if you just want to prevent accidental overriding or modification, that should work.

    Alternatively, you can use the option_{$option_name} hook to force the value of the API key and multiple language options whenever they’re retrieved. In your wp-config.php you’d add some constants like this:

    
    define('BP_GMAPS_API_KEY', 'your-api-key');
    define('BP_MULTIPLE_LOCATIONS', true);
    

    Then you’d hook in with something like this;

    
    add_filter( 'option_bpfwp-settings', 'luscpe_override_bp_options' );
    function luscpe_override_bp_options( $val, $option_name ) {
      $val['google-maps-api-key'] = BP_GMAPS_API_KEY;
      $val['multiple-locations'] = BP_MULTIPLE_LOCATIONS;
      return $val;
    }
    

    That’s just an example. You may need to inspect $val and make sure you’re overriding these values correctly. But once done it should make the values match what’s in your config file, regardless of what someone has put into the fields in their Business Profile.

    Thread Starter lucspe

    (@lucspe)

    Hi,

    To prevent a client or lower-level manager from making changes to the API key was exactly the case.

    Hiding those settings via CSS was my first thought however the TR in the form on the admin page doesn’t have a class assigned so having to rely on nth-child is a bit risky in terms of future-proofing it.

    I tried modifying the code in includes/class-settings.php to add CSS classes – as add_settings_field allows to pass that in the args array – however it seems that it is not supported by the framework used to add the settings. But I am not familiar with it so I might be missing something there.

    The option to force values via constants seems like a good workaround functionally, however it would have been best to be able to “visually remove” those fields.

    Ah, I didn’t know about that option for add_settings_field. Fancy issuing a PR to simple-admin-pages?

    https://github.com/NateWr/simple-admin-pages

    Add a documented $args param to the base settings class here:

    https://github.com/NateWr/simple-admin-pages/blob/master/classes/AdminPageSetting.class.php#L25

    Then make sure that gets passed into the add_settings_field call here:

    https://github.com/NateWr/simple-admin-pages/blob/master/classes/AdminPageSetting.class.php#L272

    I’ve created an issue for it:

    https://github.com/NateWr/simple-admin-pages/issues/14

    Thread Starter lucspe

    (@lucspe)

    Sure.

    I’ve sent you a pull request. I think that should be it.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filtering out specific settings’ is closed to new replies.