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