wp-user-profiles icon indicating copy to clipboard operation
wp-user-profiles copied to clipboard

Best Practices > Override profile.php

Open baden03 opened this issue 2 years ago • 1 comments

A couple of years ago I used this plugin on a project. I had to make changes to existing tabs, and add a few new ones, so I just hard-coded them into my own fork of the plugin. Now I am revisiting this for another project, and tough I would take the time to revisit this and where I can a) use best practices to customize the output without changing the core plugin or b) submit pull requests to allow for greater flexibility.

First order is how best to modify the output of profiles.php? From what I can tell the following things happen.

  1. the profiles.php file is required once when the plugin is loaded: require_once $plugin_path . 'includes/sections/profile.php';
  2. the profile.php file extends the base WP_User_Profile_Section class: class WP_User_Profile_Profile_Section extends WP_User_Profile_Section {
  3. the init action fires the 'wp_user_profiles_register_profile_section' function: add_action( 'init', 'wp_user_profiles_register_profile_section' ); that is defined in sections.php that creates the profile tab: new WP_User_Profile_Profile_Section( ...

What I did before is just hard-code changes to the profile.php file and moved on.

Now, I have a few ways to go here.

What I am about to do is the following:

  1. remove_action > wp_user_profiles_register_profile_section
  2. in my child-theme / plugin define my own WP_User_Profile_Profile_Section class that extends the plugins base class and
  3. add_action to fire my own instance This would not require any changes to the plugin (yet)... but to extend the class, we would need to make sure the class has already been loaded, etc.

It might be nicer to use the child-theme overwrite concept: create a single filter that allows a user defined folder that would hold any section overwrite files (profile.php, for example). If a file with the same name is found, use that, if not, use the default that came with the plugin.

Thoughts?

baden03 avatar Nov 15 '21 14:11 baden03

After attempting the remove_action route mentioned above, it seems much more elegant to allow the core files to be filtered. The above pull-request allows any of the core files to be filtered by another plugin or theme.

baden03 avatar Nov 15 '21 16:11 baden03

Merged your PR into the master branch 💫

JJJ avatar Dec 29 '22 22:12 JJJ