wordpress中的php函数仅在用户不是admin时才适用
问题描述:
I have a php function in WordPress which automatically assigns a users first and last name to a post title. This is intended in the frontend. However, in the backend when an administrator edits the same post it should not overwrite the post with the administrators values.
How can this be modified so that A) either it does not run in the backend i.e. only frontend or B) only executes if the user is not an admin? Any help is much appreciated. Thank you all.
1 2 3 4 5 6 7 8 9 |
function wpse67262_change_title( $data ) { if( \'gd_place\' != $data[\'post_type\'] ) return $data; $user = wp_get_current_user(); $data[\'post_title\'] = $user->first_name . \' \' . $user->last_name; return $data; } add_filter( \'wp_insert_post_data\', \'wpse67262_change_title\' ); |
网友观点:
I have written some comments for you inside your function here - But everything should make sense
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function wpse67262_change_title( $data ) { if( \'gd_place\' != $data[\'post_type\'] ){ return $data; //This is for your pos type only? } $user = wp_get_current_user(); if(!is_admin() && !current_user_can(\'administrator\')){ //So this makes sure, that the following does NOT run in the backend and also takes the admin role into account $data[\'post_title\'] = $user->first_name . \' \' . $user->last_name; return $data; } else { //one of the conditions failed - So do nothing new return $data; } } add_filter( \'wp_insert_post_data\', \'wpse67262_change_title\' ); |
A little cleaner function could be :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function wpse67262_change_title( $data ) { if(!is_admin() && !current_user_can(\'administrator\') && \'gd_place\' == $data[\'post_type\']){ //So this makes sure, that the following does NOT run in the backend and also takes the admin role into account, and checks the post type $user = wp_get_current_user(); $data[\'post_title\'] = $user->first_name . \' \' . $user->last_name; return $data; } else { //one of the conditions failed - So do nothing new return $data; } } add_filter( \'wp_insert_post_data\', \'wpse67262_change_title\' ); |
###
You can try this to disable post title
1 2 3 4 5 6 |
jQuery(document).ready(function() { post_status = /* your post status here */ if( post_status != \\\"auto-draft\\\" ) { jQuery( \\\"#title\\\" ).attr( \'disabled\', true ); }); |
###
You could check if the current user is admin with something like that :
1 2 3 4 5 6 |
if ( current_user_can( \'administrator\' ) ) { /* A user with admin privileges */ } else { /* A user without admin privileges */ } |
current_user_can function documentation : https://codex.wordpress.org/Function_Reference/current_user_can
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » wordpress中的php函数仅在用户不是admin时才适用