1

I'm creating a plugin to modify the user profile into wp panel. I'd like to add an author template with the same plugin.

I don't know if there is the file author.php into directory of theme. So I'd like to create a file author.php (if author.php not exist) or modify it (if author.php exist). All this via plugin.

How can I?

2

You can riff off of how WooCommerce and other plugins are providing templates in their plugin, while allowing the theme to override them if they exist.

Basically, you hook into the template_include function, which is where WordPress decides what template to load for a given query.

Since you want to do something with the author.php template, you will want to test for the is_author() conditional tag. Now author.php is something that is pretty common in a theme, so if you want to hijack that to something custom I would probably rename the template to something else.

add_filter( 'template_include', 'wpa_155871_template_loader' );

function wpa_155871_template_loader( $template ) {

    $file = '';

    if ( is_author() ) {
        $file   = 'custom-author.php'; // the name of your custom template
        $find[] = $file;
        $find[] = 'plugin-name/' . $file; // name of folder it could be in, in user's theme
    } 

    if ( $file ) {
        $template       = locate_template( array_unique( $find ) );
        if ( ! $template ) { 
            // if not found in theme, will use your plugin version
            $template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' . $file;
        }
    }

    return $template;
}

Untested, so your mileage may vary and there could be typos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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