1

I have a plugin which adds a custom role called the trip_vendor. I am trying to edit or modify the profile url of this role. Currently, it is example.com/operator/username but I want to change it to example.com/operator/company-name.

company-name is a meta value of the user (whose role is trip_vendor). All in all, I want to have meta value in the author profile url instead of the author name. I am able to change the slug author (default) to operator but couldn't add the author meta.

function wpte_add_permastruct(){
       add_permastruct( '%author_trip_vendor%', 'operator/%author%', [
           'ep_mask' => EP_AUTHORS,
       ]);
   }


   function wpte_vendor_profile_url( $link, $author_id, $author_nicename ){
       if ( user_can( $author_id, 'trip_vendor' ) ) {
           $link = '/operator/' . $author_nicename;
           $link = home_url( user_trailingslashit( $link ) );
       }
       return $link;
   }
0

I don't know the context, but i got your use case working like this:

Add new permastruct and make sure to regenerate permalinks (Settings > Permalinks > Save)

/**
 * Add additional permalink
 *
 * @uses https://codex.wordpress.org/Plugin_API/Action_Reference/init
 */
function wpte_add_permastruct(){

    add_permastruct( '%author_trip_vendor%', 'operator/%author%', [
        'ep_mask' => EP_AUTHORS,
    ]);
}

add_action( 'init', 'wpte_add_permastruct' );

Added this to init action.

Next alter the WP Query on author query (is_author() === true condition)

/**
 * Change query when is_author() is true
 *
 * @uses https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
 */
function wpse33150_alter_query($query) {

    // Do not apply if not $querying "author"
    if ( ! is_author() )
        return;

    // Set query for author meta
    $query->set( '_private_author_query', [
        [
            'key' => 'company_name',
            'value' => get_query_var('author_name'),
            'compare' => '=',
        ]
    ]);

    return $query;
}

add_action( 'pre_get_posts', 'wpse33150_alter_query' );

This is added to pre_gest_posts action

Now when you enter endpoint /operator/company-name the query will be altered to query by "company-name".

Just for reference i'm also adding implementation of the author meta field.

/**
 * Add meta field company_name to user
 */
function wpse33150_user_profile_fields( $user ) { ?>
    <table class="form-table">
        <tr>
            <th>
                <label for="company_name"><?php _e('Company name'); ?></label>
            </th>
            <td>
                <input type="text" name="company_name" id="company_name" value="<?php echo esc_attr( get_the_author_meta( 'company_name', $user->ID ) ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
<?php }

add_action( 'show_user_profile', 'wpse33150_user_profile_fields' );
add_action( 'edit_user_profile', 'wpse33150_user_profile_fields' );

/**
 * Update meta field company_name to user on update
 */
function save_wpse33150_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
        return false; 
    }

    update_user_meta( $user_id, 'company_name', $_POST['company_name'] );
}

add_action( 'personal_options_update', 'save_wpse33150_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_wpse33150_user_profile_fields' );

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.