首先,我要说,我不是一个程序员,但我喜欢学习。
情况是这样的。我的用户配置文件中添加了许多用于社交媒体的自定义配置文件字段。用户输入其个人资料的完整URL,然后他们的信息显示在其作者页面上,以及他们的个人简历、个人资料图像、所有帖子等。
我还想只使用推特用户名,这样当用户发表帖子时,该帖子将使用他们的用户名自动推特YOURLS: WordPress to Twitter 插件。这是YOURLS的一个功能,我对设置它没有异议。
我不想要求用户在单独的字段中输入用户名,也不想进入并手动编辑每个用户。此外,我现在确实希望此字段对用户可见。
根据经验,我发现很多人都不清楚自己的推特用户名。很多时候,他们输入@Username或#Username或其完整的配置文件URL或其配置文件URL,而不输入http://Username。这就是我更改概要文件字段以要求完整URL而不是简单的用户名的主要原因。
好吧,这就是我想要的。以下是我认为我需要并且已经做的:
创建自定义字段,供用户输入其Twitter个人资料URL
创建自定义字段,一旦从URL中删除推特用户名,就可以放置该用户名
剥离URL(我想使用regex)
向用户隐藏字段
向您的推文模板添加新字段
// 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-新自定义字段是使用以下方法创建的: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-从Twitter URL中删除Twitter用户名。这就是我不知道自己在做什么的地方。我找到了这个线程,但我不熟悉regex。Regex - Extract Twitter Username from URL同样,一旦获得Twitter用户名,我不知道如何将其添加到单独的字段中。我想这也需要在Twitter URL字段更改时进行更新。。。
5-我使用以下方法对所有非管理员用户隐藏该字段(我还隐藏了整个“个人选项”部分)
//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\\u用户名已添加到YOURLS推文模板中我希望一切都清楚。如果没有,请告诉我,我将尝试更深入地了解。