为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
绝地无双 2021-08-28 18:31:56
我在 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' );
查看完整描述

3 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

我已经在你的函数中为你写了一些评论 - 但一切都应该有意义


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


查看完整回答
反对 回复 2021-08-28
?
慕哥9229398

TA贡献1877条经验 获得超6个赞

你可以试试这个来禁用帖子标题


jQuery(document).ready(function() {

    post_status = /* your post status here */

    if( post_status != "auto-draft" ) {

    jQuery( "#title" ).attr( 'disabled', true );

});


查看完整回答
反对 回复 2021-08-28
  • 3 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信