0

I would like to be able to make custom URLs for users. For example, example.com/john, or example.com/jane. These are not WordPress users or authors these are usernames that I am bringing in from external resources. I want all of these URLs to go to a single page where I can display info for that user. I don't want the URL to be anything like example.com/user/john, just example.com/john.

Recap: I am wanting to see if a username is registered in the dabase. If there is a user in the db for example 'john' the url 'example.com/john' should be accessible but if 'john' is not a valid username in the db, go to the 404 page.

I've tried to implement this but don't understand how to make this work. I added the following in my functions.php file.

function codex_custom_init() {
  $args = array(
    'public'  => true,
    'label'   => 'profile',
    'rewrite' => array( 'slug' => '/' )
  );
  register_post_type( 'profile', $args );
}
add_action( 'init', 'codex_custom_init' );
1
  • Have you thought of using custom post types for those pages? Commented Jan 24, 2016 at 10:24

2 Answers 2

0

Try to create a custom post (ex: my_users), then add a rewrite rule @codex

'rewrite' => array( 'slug' => '/' )

At the beginning, the url was my_users/user_name, with the rewrite rule, it will be /user_name.

Define a single tempalte in your theme, in my case single-my_users.php.

But you will not be able to create a page with the same slug.

I hope that help u.

Sign up to request clarification or add additional context in comments.

3 Comments

I've tried implementing this rewrite but am not sure how to do so correctly. Could you possibly elaborate a little more?
Go to codex.wordpress.org/Function_Reference/…, here there is example of how to register a custom post type. (i hope u know how to developp theme or plugin). Change th rewrite line with my code before.
I don't know how to implement this correctly. I updated the description with the code that I have played with, but am not having in luck.
0

Okay, I'm well aware this thread is old as hell, but in the off chance some other poor soul is running into a similar issue that Travis Hoki described above, here's what Nozifel was referring to in context:

      <?php

/**
 * Registers the `fake_post_type` post type.
 */
function fake_post_type_init() {
    register_post_type( 'fake-post-type', array(
        'labels'                => array(
            'name'                  => __( 'Fake post types', 'YOUR-TEXTDOMAIN' ),
            'singular_name'         => __( 'Fake post type', 'YOUR-TEXTDOMAIN' ),
            'all_items'             => __( 'All Fake post types', 'YOUR-TEXTDOMAIN' ),
            'archives'              => __( 'Fake post type Archives', 'YOUR-TEXTDOMAIN' ),
            'attributes'            => __( 'Fake post type Attributes', 'YOUR-TEXTDOMAIN' ),
            'insert_into_item'      => __( 'Insert into fake post type', 'YOUR-TEXTDOMAIN' ),
            'uploaded_to_this_item' => __( 'Uploaded to this fake post type', 'YOUR-TEXTDOMAIN' ),
            'featured_image'        => _x( 'Featured Image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'set_featured_image'    => _x( 'Set featured image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'remove_featured_image' => _x( 'Remove featured image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'use_featured_image'    => _x( 'Use as featured image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'filter_items_list'     => __( 'Filter fake post types list', 'YOUR-TEXTDOMAIN' ),
            'items_list_navigation' => __( 'Fake post types list navigation', 'YOUR-TEXTDOMAIN' ),
            'items_list'            => __( 'Fake post types list', 'YOUR-TEXTDOMAIN' ),
            'new_item'              => __( 'New Fake post type', 'YOUR-TEXTDOMAIN' ),
            'add_new'               => __( 'Add New', 'YOUR-TEXTDOMAIN' ),
            'add_new_item'          => __( 'Add New Fake post type', 'YOUR-TEXTDOMAIN' ),
            'edit_item'             => __( 'Edit Fake post type', 'YOUR-TEXTDOMAIN' ),
            'view_item'             => __( 'View Fake post type', 'YOUR-TEXTDOMAIN' ),
            'view_items'            => __( 'View Fake post types', 'YOUR-TEXTDOMAIN' ),
            'search_items'          => __( 'Search fake post types', 'YOUR-TEXTDOMAIN' ),
            'not_found'             => __( 'No fake post types found', 'YOUR-TEXTDOMAIN' ),
            'not_found_in_trash'    => __( 'No fake post types found in trash', 'YOUR-TEXTDOMAIN' ),
            'parent_item_colon'     => __( 'Parent Fake post type:', 'YOUR-TEXTDOMAIN' ),
            'menu_name'             => __( 'Fake post types', 'YOUR-TEXTDOMAIN' ),
        ),
        'public'                => true,
        'hierarchical'          => false,
        'show_ui'               => true,
        'show_in_nav_menus'     => true,
        'supports'              => array( 'title', 'editor' ),
        'has_archive'           => true,
        'rewrite'               => [ 'slug' => '/' ], // <-- RIGHT HERE!
        'query_var'             => true,
        'menu_position'         => null,
        'menu_icon'             => 'dashicons-admin-post',
        'show_in_rest'          => true,
        'rest_base'             => 'fake-post-type',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
    ) );

}
add_action( 'init', 'fake_post_type_init' );

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.