仅当用户不是管理员时,wordpress 中的 php 函数才适用

我在 Wordpress 中有一个 php 函数,它会自动将用户的名字和姓氏分配给帖子标题。这是在前端设计的。但是,在后台,当管理员编辑同一帖子时,不应使用管理员值覆盖帖子。


如何修改它以便 A)它不在后端运行,即仅在前端运行或 B)仅在用户不是管理员时才执行?任何帮助深表感谢。谢谢你们。


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' );


绝地无双
浏览 112回答 3
3回答

DIEA

我已经在你的函数中为你写了一些评论 - 但一切都应该有意义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' );一个更清洁的功能可能是: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' );

慕哥9229398

你可以试试这个来禁用帖子标题jQuery(document).ready(function() {    post_status = /* your post status here */    if( post_status != "auto-draft" ) {    jQuery( "#title" ).attr( 'disabled', true );});
打开App,查看更多内容
随时随地看视频慕课网APP