最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - Modifying Author Link to add Author Meta in URL

matteradmin9PV0评论

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/operator/username but I want to change it to example/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;
   }

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/operator/username but I want to change it to example/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;
   }
Share Improve this question asked Mar 13, 2019 at 6:34 Asmita SubediAsmita Subedi 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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/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/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' );
Post a comment

comment list (0)

  1. No comments so far