Let me start by saying, I am not a coder, but enjoy learning.

Here is the situation. I have many custom profile fields for social media added to my user profiles. The user enters the full URL of their profile, then their information is displayed on their author page with their bio, profile image, all posts, etc.

I also want to use ONLY the Twitter username so that when a user makes a post, the post will be automatically tweeted with their username using YOURLS: WordPress to Twitter plugin. This is a feature of YOURLS and I have no issue setting it up.

I do not want to ask the user to enter their username in a separate field and I do not want to go in and manually edit each user. As well, I do now want this field to be visible to the user.

From experience, I have found that many people are unclear as to their actual Twitter username. A lot of times they enter @Username or #Username or their complete profile URL or their profile URL without http:// instead of simply, Username. This is the main reason I changed the profile fields to require the full URL instead of simply the username.

Ok, that's what I want. Here is what I believe I need and have done:

  1. Create the custom field for user to enter their Twitter profile URL

  2. Create the custom field to place the Twitter username once it has been stripped from URL

  3. Strip the URL (using regex, I imagine)

  4. Add the stripped username to the new field

  5. Hide the field from the user

  6. Add new field to YOURLS tweet template

1 - The custom profile fields were were created using (also removed other fields):

// Contact Info fields in profile
add_filter('user_contactmethods','new_contactmethods',10,1);
function new_contactmethods( $contactmethods ) {
$contactmethods['twitter'] = 'Twitter (Full URL)';
$contactmethods['facebook'] = 'Facebook (Full URL)';
$contactmethods['googleplus'] = 'Google+ (Full URL)';
$contactmethods['linkedin'] = 'LinkedIn (Full URL)';
$contactmethods['delicious'] = 'Delicious (Full URL)';
$contactmethods['flickr'] = 'Flickr (Full URL)';
$contactmethods['picasa'] = 'Picasa (Full URL)';
$contactmethods['Vimeo'] = 'Vimeo (Full URL)';
$contactmethods['youtube'] = 'YouTube (Full URL)';
$contactmethods['reddit'] = 'Reddit (Full URL)';
   unset($contactmethods['yim']); // Remove YIM
   unset($contactmethods['aim']); // Remove AIM
   unset($contactmethods['jabber']); // Remove Jabber
   unset($contactmethods['msn']); //Remove Messenger

   return $contactmethods;
}

2 - The new custom field was created using:

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

function my_show_extra_profile_fields( $user ) { ?>

    <table id="tiwtter-username" class="form-table">
        <tr>
            <th><label for="twitter">Twitter Username</label></th>
            <td>
            <input type="text" name="twitter_username" id="twitter_username" value="<?php echo esc_attr( get_the_author_meta( 'twitter_username', $user->ID ) ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
<?php }

3 - Strip the Twitter username from the Twitter URL. This is where I have no idea what I am doing. I found this thread, but I am unfamiliar with regex. Regex - Extract Twitter Username from URL

4 - As well, once getting the Twitter username, I do not know how to add it to a separate field. I suppose this would also need to update whenever the Twitter URL field is changed as well...

5 - And I have used the following method to hide the field from all users who are not Admins (I have also hidden the entire "Personal Options" section)

//Hide Profile Page Options from all except Administrator
if (!current_user_can('administrator')){
function hide_profile_page_options() {
global $user;
$hide_profile_options = "<style type=\"text/css\"> #tiwtter-username, #profile-page h3:first-of-type, #profile-page table:first-of-type, .editform { display: none; }</style>";
print($hide_profile_options);
}
add_action( 'admin_head', 'hide_profile_page_options'  );
} 

6 - twitter_username has been added to the YOURLS tweet template

I hope everything is clear. If not, let me know and I will try to go more in-depth.

没有正确的解决方案

许可以下: CC-BY-SA归因
scroll top