1

My current, hacky way, is this:

Right now, in my functions.php file I have something like this:

/**
 * Create the query var so that WP catches the custom /profile/username url
 */

    function userpage_rewrite_add_var( $vars ) {
        $vars[] = 'profile';
        return $vars;
    }
    add_filter( 'query_vars', 'userpage_rewrite_add_var' );

/**
 * Create rewrite rule
 * @return [type] [description]
 */

    function userpage_rewrite_rule() {
        add_rewrite_tag( '%profile%', '([^&]+)' );
        add_rewrite_rule(
            '^profile/([^/]*)/?',
            'index.php?profile=$matches[1]',
            'top'
        );
    }
    add_action('init','userpage_rewrite_rule');

/**
 * Catch the URL and redirect it to a template file
 * @return [type] [description]
 */

    function userpage_rewrite_catch() {
        global $wp_query;

        if ( array_key_exists( 'profile', $wp_query->query_vars ) ) {
            require_once TEMPLATEPATH . '/author.php';
            exit;
        }
    }
    add_action( 'template_redirect', 'userpage_rewrite_catch' );

And inside the author.php template, something like this:

<?php
    /**
     * Get users nice_name from GET
     * @var [type]
     */

        $nice_name = $wp_query->query_vars['profile'];

    /**
     * Get the user
     * @var [type]
     */

        $user = get_user_by('slug', $nice_name );

    get_header();
        echo '<h1>Hello ' . $user->first_name . '</h1>';
    get_footer();
?>

However, this has numerous disadvantages, amongst others:

  • Feels hacky
  • Can't use functions such as get_the_author_meta()
  • Incorrect page meta (title, og:image, og:url, etc). Currently it graps everything from the "Blog"-page

What is the correct way?

  • What can I do to improve my question, so it'll receive more attention / get answered? – FooBar May 5 '15 at 8:19
  • it's unclear what you would like to achieve. Can you maybe explain the purpose behind your request. Maybe in the expanded part explain: "I would like to make an author.php page appear if so an so... " – hitautodestruct May 5 '15 at 12:10
  • In general the author.php file is a file meant to be displayed when a user accesses the author's page via url. – hitautodestruct May 5 '15 at 12:13
  • @hitautodestruct the issue is that page meta (i.e. the page title, the page thumbnail, and so on) is not correct. I think I need to tell WordPress somehow, that this is an author page, so it will assign the correct page data (like title, etc). – FooBar May 7 '15 at 11:19
  • Wordpress loads the author page based on the URL. You can create a specific file called author.php that will allow you to customise the look of the page. Take a look at this codex.wordpress.org/Template_Hierarchy#Author_display – hitautodestruct May 7 '15 at 11:25

Your Answer

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

Browse other questions tagged or ask your own question.