Home › Forums › Calendar Products › Events Calendar PRO › undefined get_current_screen preventing use of events in custom field
- This topic has 7 replies, 3 voices, and was last updated 10 years, 8 months ago by
Leah.
-
AuthorPosts
-
July 16, 2015 at 9:58 am #987083
Steve
ParticipantHi 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.
July 17, 2015 at 5:04 am #987319Geoff
MemberHey 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!
GeoffJuly 17, 2015 at 5:43 am #987325Steve
ParticipantHi 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 afterinit, and is the recommended way of registering custom fields. But when this happens, and the options need to be populated with events grabbed usingtribe_get_events, that function callsTribe__Events__Query::getEvents(), which does theWP_Query.The problem is that the Events Calendar query class has a method
pre_get_posts(), which is hooked topre_get_posts. And (line 283) it uses theget_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 betweenwp_loadedandauth_redirect. That means thattribe_get_events()can’t be used (in admin I’m talking about) beforeauth_redirect.That seems strange – are there never instances where
tribe_get_eventsmight be needed oninit?I guess the last resort would be to register custom fields after
auth_redirect. Maybeadmin_init? Then I’d need to do some fudging to hook the code tocmb2_init(just a more convenient way of hooking toinit), making sure that only runs on the front, and also hooking toadmin_initto cover the issue withget_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.July 17, 2015 at 7:56 am #987385Geoff
MemberHey 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!
GeoffJuly 17, 2015 at 10:25 am #987453Steve
ParticipantMany 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.
July 17, 2015 at 11:05 am #987459Steve
ParticipantIt 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?
July 17, 2015 at 11:11 am #987460Steve
ParticipantOK, 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.
July 20, 2015 at 1:23 pm #988086Leah
MemberHi 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 -
AuthorPosts
- The topic ‘undefined get_current_screen preventing use of events in custom field’ is closed to new replies.
