Home › Forums › Calendar Products › Events Calendar PRO › Programmatically setting Additional Fields
- This topic has 25 replies, 3 voices, and was last updated 8 years, 10 months ago by Support Droid.
-
AuthorPosts
-
August 26, 2012 at 2:01 pm #24134Andy FragenModerator
I’m trying to add an Additional Field in a function. It fails with the following errors.
The if statement fails with the following error.
Can’t use method return value in write context….
Inside the if statement fails with the following error.
Call to a member function save_meta_option() on a non-object….
The code is at https://gist.github.com/3483530
Any help is appreciated.
August 28, 2012 at 7:51 am #24203BarryMemberHi Andy!
The first error is because you are using isset() incorrectly. Perhaps something like:
$alarm = $tecm->get_custom_field_by_label(‘Alarm’)
if (!empty($alarm)) { /* … */ }Would be better here. The thing is that isset() tests if a variable is set and not null, whereas you are not supplying a variable – you are supplying the return value of a function.
The second error looks basically like a typo. You’ve defined $tcem first of all then use $tecm later.
Hope that helps ๐
August 28, 2012 at 5:52 pm #24262Andy FragenModeratorThanks Barry. gist updated but now I get a real interesting error.
Class ‘TribeEvents’ not found in /path-to-plugins/events-calendar-pro/lib/tribe-ecp-custom-meta.class.php on line 151
August 28, 2012 at 7:31 pm #24266BarryMemberHmm, I can’t replicate that error – but then I’m not quite sure at which stage or in what context you are using that snippet.
It would certainly have to be late enough in the lifetime of the request that both The Events Calendar and Events Calendar PRO have finished loading.
Can you provide a context for this?
August 28, 2012 at 8:28 pm #24273Andy FragenModeratorOK, I got rid of all the errors, but it doesn’t create an Additional Field. I’m calling this from my Events Calendar PRO Alarm plugin. I want to create the required Additional Field if it doesn’t exist. But it’s not working.
Am I misreading the class TribeEventsCustomMeta? Is there no method to directly save data and create an Additional Field?
August 29, 2012 at 7:45 am #24277BarryMemberI think you might be misreading TribeEventsCustomMeta::save_meta_options() slightly. Essentially it exists as a filter, calling it directly will not cause anything in the database to be updated, for instance.
If you look at the ECP code itself it’s the job of the setOptions() method in the TribeEvents class to actually save stuff.
Two other points, though I guess they might be be moot now, are:
1. You should probably remove the false ! operator from your isset() statement. The first time I looked at your code I was really just doing a quick scan, I wasn’t thinking about what you were trying to achieve. But in this case I think you want to take action whenever the variable isempty.
2. PHP is tolerant enough that this will work anyway – but save_meta_options() is a static method and that’s how it should be called, not as an instance method (so the :: operator instead of the -> operator)
Anyway, that’s just some thoughts, will let you take things from here ๐
August 29, 2012 at 7:45 am #24278BarryMember*is empty
August 29, 2012 at 3:08 pm #24314Andy FragenModeratorBarry, thanks for the explanation and PHP teaching. I can always use more.
As for the purpose of my question, I guess it’s really not practical to try and set this Additional Field within the plugin code, better to just report that it’s necessary for the plugin. If this is truly the case, consider this thread closed.
Thanks again.
August 29, 2012 at 4:01 pm #24318Andy FragenModeratorFWIW, I did the following
var_dump(TribeEventsCustomMeta::get_custom_field_by_label(‘Alarm’));
or
$alarm = TribeEventsCustomMeta::get_custom_field_by_label(‘Alarm’);
var_dump($alarm);The result is always bool(false)
August 29, 2012 at 4:53 pm #24321BarryMemberAs for the purpose of my question, I guess itโs really not practical to try and set this Additional Field within the plugin code, better to just report that itโs necessary for the plugin.
I don’t doubt there is a way to do what you want, unfortunately this sort of thing sits outside the scope of support – so I can’t allocate too much time to it, unfortunately – but we can let this sit topic sit and see if anyone else can pitch in here, it’s entirely possible that someone else has had a bash at this.
August 29, 2012 at 4:58 pm #24322BarryMember… Actually … I think I see what you are getting at here. Let me check something with one of the developers. I’m not 100% sure but it could be that we are looking at a bug here.
August 29, 2012 at 5:06 pm #24323BarryMemberOK, gotta question for you. Have you set up the Alarm field in the Events > Settings > Additional Fields page, either manually or by adding the additional field programmatically?
August 29, 2012 at 5:49 pm #24325Andy FragenModeratorYes, I have set up an Additional Field via Events > Settings > Additional Fields. I couldn’t get it to happen programatically, that’s what I was originally after. As TribeEventsCustomMeta::save_meta_options is more of a filter than an action.
I’ve settled for just making a warning if there is no Alarm field present. I went into the code of TribeEventsCustomMeta::get_custom_field_by_label to do the checking. Using the method directly just doesn’t work. I assume you mean that being the bug I stumbled upon.
August 30, 2012 at 7:15 am #24345BarryMemberIn terms of adding an additional field programmatically (if it doesn’t already exist), does this work for you? http://pastebin.com/s2k0zZHi
August 30, 2012 at 3:53 pm #24376Andy FragenModeratorBarry, code looks very promising but I get the following error.
Call to a member function flush_rules() on a non-object in /…/wp-content/plugins/the-events-calendar/lib/the-events-calendar.class.php on line 1386
I’m calling this in add_action( ‘plugins_loaded’, ‘myscript’ );
-
AuthorPosts
- The topic ‘Programmatically setting Additional Fields’ is closed to new replies.