undefined get_current_screen preventing use of events in custom field

Home Forums Calendar Products Events Calendar PRO undefined get_current_screen preventing use of events in custom field

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #987083
    Steve
    Participant

    Hi there,

    I’m using the CMB2 plugin to define a custom field in which editors can select an event. I’m hooking the new_cmb2_box() code to the cmb2_init hook as recommended.

    However, when I perform any query (I’m using tribe_get_events but the same applies for get_posts) to get the events to populate the field, I get this error:

    Fatal error: Call to undefined function get_current_screen() in /var/www/html/young-hackney/public/wp-content/plugins/the-events-calendar/src/Tribe/Query.php on line 283

    I found this related thread, but it’s not very helpful:

    Events Calendar Pro fatal error Call to undefined function get_current_screen()

    I realise this call to get_current_screen is with code hooked to pre_get_posts, which serves the vital purpose of amending the query to get events in the right way. But I’m struggling to see how I can populate this custom field. I’m working on it still, but if you have any ideas that’d be great.

    #987319
    Geoff
    Member

    Hey Mike, thanks for reaching out!

    Man, that sounds like a very cool feature you’re working on. 🙂

    I’m honestly unfamiliar with the CMB2 plugin, but I checked it out and found this example of query for posts inside of a metabox. It would seem that a similar pattern would work by using <i>tribe-events</i> in the post-type query. Does that work for you?

    Then again, I suppose it could also depend on what sort of metabox you’re creating and what you are hoping it will do. Let me know if that example helps or perhaps you can share a little more about how you’re using that query and we cankeep digging. 🙂

    Cheers!
    Geoff

    #987325
    Steve
    Participant

    Hi Geoff,

    Thanks, but no progress.

    You’re right that that method of populating options is a bit cleaner – using a function to return the custom options. I was trying to create the options right before the call to $cmb->add_field(), whereas I should probably be using a function.

    Anyway, that’s just a minor aspect of how to use CMB2. I don’t think the workings of CMB2 are the problem, though. The issue is that the creation of the CMB2 fields need to be hooked to cmb2_init. This fires just after init, and is the recommended way of registering custom fields. But when this happens, and the options need to be populated with events grabbed using tribe_get_events, that function calls Tribe__Events__Query::getEvents(), which does the WP_Query.

    The problem is that the Events Calendar query class has a method pre_get_posts(), which is hooked to pre_get_posts. And (line 283) it uses the get_current_screen() function, which isn’t defined.

    Just done some quick testing and it seems, from the list of hooks spit out by the Query Monitor plugin, get_current_screen() is defined somewhere between wp_loaded and auth_redirect. That means that tribe_get_events() can’t be used (in admin I’m talking about) before auth_redirect.

    That seems strange – are there never instances where tribe_get_events might be needed on init?

    I guess the last resort would be to register custom fields after auth_redirect. Maybe admin_init? Then I’d need to do some fudging to hook the code to cmb2_init (just a more convenient way of hooking to init), making sure that only runs on the front, and also hooking to admin_init to cover the issue with get_current_screen().

    However, this seems very hacky. It’d be great if you could let me know a cleaner way of using tribe_get_events() in admin.

    #987385
    Geoff
    Member

    Hey Mike,

    You’re absolutely spot on. We could open things up here by not depending on the presence of get_current_screen() in Tribe__Events__Query::pre_get_posts(). For example, if it doesn’t exist, we could set $screen to null.

    I don’t have anything that will change that at the moment, but I have made a ticket in our system for us to look into it some more. We’ll do our best to follow up with you directly here if and when we have more to share, but I’ll go ahead and mark this thread as Pending Fix in the meantime.

    Cheers!
    Geoff

    #987453
    Steve
    Participant

    Many thanks! For now I’ll try hooking differently as a workaround, but good to know it’s being addressed. I’ll notify you if I come up against serious issues with hooking later.

    #987459
    Steve
    Participant

    It seems that hooking differently just doesn’t work. I can only use the CMB2 plugin by hooking to cmb2_init as directed.

    I’ll have a word with the CMB2 people to see if there’s any workaround, but at the moment it looks like I may have to hack The Events Calendar plugin to test for get_current_screen being defined.

    Is there any way this change to make it into the next release do I don’t have to keep re-applying the hack?

    #987460
    Steve
    Participant

    OK, I’ve found that CMB2 options can be populated using a callback. Problem solved, I think!

    However, I’d still advise adding a little test for the existence of get_current_screen, if possible – there may be other situations/plugins where a callback isn’t available.

    #988086
    Leah
    Member

    Hi there,

    We wanted to check in here and update you on our progress. Although we weren’t able to address your issue in our upcoming release (3.11) we do still have it on our radar. Thank you for your patience while we continue working on a fix for the problem you reported. We will do our best to keep you posted when we have a solution in place. In the meantime, if you need help or find that the issue you reported here is no longer a problem, please feel free to start another thread.

    Thanks again for your patience and understanding!

    Cheers,
    Leah
    and the rest of The Events Calendar team

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘undefined get_current_screen preventing use of events in custom field’ is closed to new replies.