修改函数以仅返回已登录用户的值

时间:2014-11-07 作者:irishrunner16

我试图修改插件中的一个函数,以便它只返回登录用户的自定义帖子类型(场馆)(默认情况下,它显示所有场馆)。这是我第一次定制一个功能,所以任何帮助都将不胜感激!

这就是我在函数中所做的。php文件:

global $current_user;
get_currentuserinfo();

if ($post->post_author == $current_user->ID) {
    add_action(\'tribe_events_community_form\', \'community_events_venue_select_menu_current_user\');
function community_events_venue_select_menu_current_user( $event_id = null ) {
 if ( !$event_id ) {
     global $post;
     if( isset( $post->post_type ) && $post->post_type == \'tribe_events\' ) {
         $event_id = $post->ID;
     } elseif( isset( $post->post_type ) && $post->post_type == \'tribe_venue\' ) {
         return;
     }
 }
 do_action( \'tribe_venue_table_top\', $event_id );
}
}
尽管我的数据库中有数千个“tribe\\u events”和“tribe\\u venue”帖子类型,但我还是收到了以下两个函数错误:

注意:未定义变量:post

注意:尝试获取非对象的属性

在这一行:if ($post->post_author == $current_user->ID) {

此外,当我将默认函数(插件中)的名称更改为自定义函数名称时,返回以下错误:

Fatal error: Call to undefined function community_events_venue_select_menu_current_user()

2 个回复
SO网友:Matt Keys

请参见下面我对您尝试执行的操作的版本。但是请记住,将此函数添加到函数中。php,并更改插件以指向此函数,将产生一个错误,即该函数不存在。这是因为插件先于函数。因此,当插件加载时,您的函数还不存在。您需要在插件本身内部进行此更改。

修改插件通常不是一个好主意,因为当插件更新时,它会覆盖您的更改。如果您认为在您的情况下这仍然是一个好主意,我建议更改插件的名称,或者将版本号设置得非常高,以防止更新破坏您的代码。当然,从那时起,您将根据需要手动更新/维护插件的自定义版本。

function community_events_venue_select_menu_current_user( $event_id = null ) {

    global $current_user, $post;
    get_currentuserinfo();

    if ( $post->post_author != $current_user->ID ) {
        return; // Do nothing if this post author does not match our current user id
    }

    if ( ! $event_id ) {
        if ( isset( $post->post_type ) && $post->post_type == \'tribe_events\' ) {
            $event_id = $post->ID;
        } elseif ( isset( $post->post_type ) && $post->post_type == \'tribe_venue\' ) {
            return;
        }
    }
    do_action( \'tribe_venue_table_top\', $event_id );
}
add_action( \'tribe_events_community_form\', \'community_events_venue_select_menu_current_user\' );

SO网友:dMcClintock

使用get_currentuserinfo(); 提取当前用户的ID,然后对照$post->post_author 使用if语句。

global $post, $current_user;
get_currentuserinfo();

if ($post->post_author == $current_user->ID) {
    // Do something
}
应该有一个add_action 直接在函数之前或之后;将该行放在if语句中,以便仅当author条件为true时才注册函数。

对于您的情况,请使用以下选项:

add_action(\'tribe_events_community_form\', \'community_events_venue_select_menu_current_user\');
function community_events_venue_select_menu_current_user( $event_id = null ) {
    global $post, $current_user;
    get_currentuserinfo();

    if ($post->post_author == $current_user->ID) {
        if ( !$event_id ) {
            if( isset( $post->post_type ) && $post->post_type == \'tribe_events\' ) {
                $event_id = $post->ID;
            } elseif( isset( $post->post_type ) && $post->post_type == \'tribe_venue\' ) {
                return;
            }
        }
        do_action( \'tribe_venue_table_top\', $event_id );
    }
}

结束

相关推荐

Wp.getUsersBlog XMLRPC暴力攻击/漏洞

周末假期过后,我管理的一个较大的网站遭到了暴力攻击。攻击者试图使用wp.getUsersBlogs 函数和常用用户名和密码列表。快速的研究表明,在成功尝试后,此函数将返回用户是否是管理员。我使用IP黑名单云插件作为安全的一部分,因此它记录了攻击,但由于此攻击方法不使用正常的登录方法,因此不会发生实际的黑名单。无论如何,这都不会有什么帮助,因为每次尝试后,攻击者都会使用一个新的IP(到目前为止总共超过15000个IP)(第二次攻击超过20000个)。我确实找到了一个完全禁用XML-RPC(API)的插件,但