Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
6  
This question - especially including your comments to the answer below - has become a "do my coding work for me" question, and should be closed as not a real question. As originally asked, this question has been answered by @TomJNowell below, and you should accept his answer. –  Chip Bennett Apr 12 '12 at 19:43
    
The question has NOT been answered. His code only returns the username printed at the top of the screen, but breaks the theme and does not allow users to log in. IF anything his answer deserves a down-vote for not doing anything at all that was asked. –  Travis Pflanz Apr 12 '12 at 19:49
5  
To the contrary, his code does exactly what the original question asked. The issue now, apparently, is that you don't know how to implement it. But that's a separate issue entirely from the one originally asked. –  Chip Bennett Apr 12 '12 at 19:57
    
It does this - "Add the stripped username to the new field"? And breaking things that already work should be an acceptable solution? I don't think so. –  Travis Pflanz Apr 12 '12 at 19:58
3  
No. The solution provided breaks nothing. You are just not implementing the code correctly. We are not here to teach you to code, we are here to answer your questions, and that question has been answered. If you need specific, hands-on support, you need to hire a developer, visit a support forum, drop in the #wordpress channel on IRC, or earn enough reputation on this site to drop in on chat. –  EAMann Apr 12 '12 at 20:18
add comment

1 Answer

up vote 7 down vote accepted
+50

You need to do this in steps:

  1. Decide when you are going to parse the user's meta to change the value.
  2. Define a function to do that.
  3. Hook that function to the appropriate action.

The original answer defined what you need to do for step #2, using preg_match() to parse the Twitter URL and extract the username.

function update_the_user( $user ) {
    // grab the twitter URL, I've used some of the code further up in your question to do this
    $twitter_url = esc_attr( get_the_author_meta( 'twitter', $user->ID ) );

    // pass it into a regular expression, and get the result
    $result = preg_match("/https?:\/\/(www\.)?twitter\.com\/(#!\/)?@?([^\/]*)/", $twitter_url, $matches);

    $twitter_username = 'default value';

    // if the Regex worked, the $result is now equal to 1
    if($result == 1){
        // it worked! According to the answer in the question you linked to, the $matches array will contain the username in the 3rd position
        $twitter_username = $matches[3];
    } else {
        // failure!! The regex did not find the username, abort!!!
        return;
    }

    // The twitter username is now inside $twitter_username

    // lets add the extracted value to the users meta as 'twitter_username'
    update_user_meta( $user->ID, 'twitter_username', $twitter_username );
}

You're left deciding when to handle steps 1 and 3.

When to Process the Data

This is entirely an architectural question for you, not for the site. You can process the user when their profile page is edited, when the user is viewed on the front end, or just in a bulk process and handle all users at once. It really depends on your objectives:

Hook to Appropriate Action

Once you've decided when to hook the function, you just tie it in.

If you want to process users when a profile page is loaded, you can hook on to personal_options:

add_action( 'personal_options', 'update_the_user' );

This action passes in a variable, $profileuser, that is populated based on the ID of the user who's profile is open. Use this method, and you can manually load the profile pages for all of your users one at a time to update them.

Alternatively, you can run this function once against all of your users.

function update_all_users() {
    $users = get_users();

    foreach ( $users as $user ) {
        update_the_user( $user );
    }
}

Just hook this function to init or some other loading action, load the site once, and let things flow.

At the end of the day ...

... when you implement this code, where you implement it, and how you implement it is not up to us. You've asked for advice on how you can read out a Twitter URL, parse it, extract the Twitter username, and store it in meta for the user. We've given you that. Actually implementing any and all of the code above is an exercise you'll need to do on your own.

share|improve this answer
5  
Because I am not fullfilling your need, I am answering your question, this is not a support forum, it's a question and answer site. –  Tom J Nowell Apr 5 '12 at 21:47
2  
Imagine fi I said how do you build a car, and you answered, but instead of accepting, I simply went a layer down and asked how combustion engines worked, then I asked how petrol burns, then I asked what a chemical reaction was, then I asked how 2 atoms can bind together, than I asked what a covalent bond was, then I asked what atoms are, then I asked what electrons and protons are, then I asked why they're fundamental particles, then i asked about string theory. Clearly they're all different questions. Related yes, but they're not the same –  Tom J Nowell Apr 5 '12 at 21:49
2  
If this does not suffice then you have two options. You either asked the wrong question, and thus got the correct answer to the incorrect question. OR you asked a question and got an answer but you lack the skills to understand how to apply the new knowledge. You now have a snippet of code that given User A, will produce what you want. I can already see that this si going to lead to a "but I want to do this to every single user" response. –  Tom J Nowell Apr 5 '12 at 21:53
2  
Your question was how to strip the twitter username from the full url in a profile field, then save it in a new one, and that's what my code does.If you agree then accept. If my code does not do what you asked then please explain. –  Tom J Nowell Apr 5 '12 at 21:53
2  
Code and explanation updated. If you have further problems with this code, edit your original question to explain what you did (what code you used and were) and what the results were. Do not continue leaving comments here. –  EAMann Apr 12 '12 at 20:19
show 19 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.