0

I'm trying to modify a function in a plugin so that it only returns the custom post type (venues) of the user that’s logged in (by default it displays all venues). This is my first time customizing a function, so any help would be greatly appreciated!

This is what I've put into my functions.php file:

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

I'm getting the following two errors for the function, even though there are thousands of the 'tribe_events' and 'tribe_venue' post types in my database:

Notice: Undefined variable: post

Notice: Trying to get property of non-object

on this line: if ($post->post_author == $current_user->ID) {


Additionally, when I changed the name of the default function (in the plugin) to my custom function name, its returning this error:

Fatal error: Call to undefined function community_events_venue_select_menu_current_user()

  • Has the custom post type being referenced been created yet? – dMcClintock Nov 7 '14 at 21:53
  • @dMcClintock First, thanks for your help! Yes, there are actually thousands of this custom post type in my database. I tried changing TribeEvents::POSTTYPE (thinking that might throw an error since it was code from within the plugin) to tribe_events, which is the exact value of the post_type in my posts table, but that still returns the same two errors. – irishrunner16 Nov 8 '14 at 1:41
  • Refer to my modified answer. Was the plugin working before (sans the desired functionality, of course), prior to adding the author-dependent conditional? – dMcClintock Nov 8 '14 at 3:16
  • 1
    Plugins are loaded before the functions.php file is included, so when the plugin tries to call your function, it does not exist in memory yet. Also you must call global $post; before you can use it in your function. – Matt Keys Nov 8 '14 at 5:48
  • 2
    You are calling $post too late. Define it when you set $current_user – Pieter Goosen Nov 8 '14 at 7:32
1

See below for my version of what you are trying to do. However keep in mind that adding this function to your functions.php, and changing the plugin to point to this function, will produce an error that the function does not exist. This is because plugins are included before functions.php, so when the plugin loads your function doesn't exist yet. You will need to make this change inside the plugin itself.

Modifying plugins is generally not a great idea because as the plugin updates it will overwrite your changes. If you think that it is still a good idea in your case, I would suggest changing the name of the plugin, or setting the version number very high to prevent updates from breaking your code. Of course from then on it will be up to you to manually update/maintain the custom version of the plugin as needed.

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' );
  • Thank you @MattKeys that makes sense now why it was throwing those errors. I tried adding your function to the plugin but I'm getting the following error: Notice: Undefined property: stdClass::$post_author – irishrunner16 Nov 8 '14 at 20:25
  • Change the first if statement's comparison operator back to ==. Then move your entire second if statement (including the do_action line) into the first, replacing the line that currently says return;. You can also eliminate global $post; as you've already made that call at the beginning of the function. – dMcClintock Nov 9 '14 at 4:37
  • dMcClintock is right about the second call to global $post; I missed that. However the rest seems to be just a difference in style, both should work the same. Does the notice you are getting say what line number the error is on? And is that line number inside of this function, or elsewhere? – Matt Keys Nov 9 '14 at 20:23
  • Thank you both. In each case, I'm getting Notice: Undefined property: stdClass::$post_author on this line: if ( $post->post_author != $current_user->ID ) { which is inside of this function. – irishrunner16 Nov 9 '14 at 22:14
0

Use get_currentuserinfo(); to pull the current user's ID, then check it against $post->post_author with an if statement.

global $post, $current_user;
get_currentuserinfo();

if ($post->post_author == $current_user->ID) {
    // Do something
}

There should be an add_action either directly before or after the function; place that line inside the if statement so the function is only registered if the author condition is true.

For your case, use the following:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.