是否有一个在 post.php 之前触发的 WordPress 钩子,我可以从中获取帖子 ID?

问题描述 投票:0回答:1

我有预约软件,客户数据表是 WordPress 中的自定义帖子类型。服务提供商角色无法读取这些客户数据表,除非管理员进行预约,因为此时客户表的帖子作者更改为服务提供商的用户 ID,这允许他们读取和编辑他们的用户数据表。客户。但是,如果该客户与不同的提供商进行了另一次预约,则第一个提供商将失去这些权利并收到“抱歉,不允许您编辑此项目。”

我需要在 post.php 中运行以下代码之前进行挂钩,当前为第 138 行

if ( ! current_user_can( 'edit_post', $post_id ) ) { wp_die( __( 'Sorry, you are not allowed to edit this item.' ) ); }

我有一个修改作者ID的自定义函数。它需要用户尝试访问的客户表的帖子 ID。

我尝试挂接到

load-post.php
但没有传递任何参数。

我尝试挂钩

add_meta_boxes
但它在 post.php 执行后触发
wp_die()

add_action('add_meta_boxes', 'check_user_cap', 10, 2 );
function check_user_cap($post_type, $post){
// this function is never called, but I would do something like...
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
    if ( verify_provider_against_client($post->ID ) ){
         $addr = get_bloginfo( 'url' ).'/wp-admin/post.php?post='.$post->ID.'&action=edit';
         wp_redirect( $addr );
         wp_die();
    }
}

我可以过滤current_user_can()吗?

wordpress hook
1个回答
0
投票

检查此挂钩

user_has_cap
单击此处

您可以使用此挂钩来检查功能,如下所示

<?php 
add_filter('user_has_cap', 'custom_user_capabilities_check', 10, 4);

function custom_user_capabilities_check($allcaps, $caps, $args, $user) {
    // Check if the current operation is 'edit_post'
    if (isset($args[0]) && $args[0] === 'edit_post') {
        $post_id = $args[2];

        // Check if the user doesn't have the 'edit_post' capability
        if (!isset($allcaps['edit_post']) || !$allcaps['edit_post']) {
            // Check if the user has a custom capability (e.g., 'edit_client_sheet')
            if (current_user_can('edit_client_sheet', $post_id)) {
                // Grant the 'edit_post' capability dynamically
                $allcaps['edit_post'] = true;
            }
        }
    }

    return $allcaps;
}

就是这样。如果有帮助请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.