Programmatically setting Additional Fields

Home Forums Calendar Products Events Calendar PRO Programmatically setting Additional Fields

Viewing 15 posts - 1 through 15 (of 26 total)
  • Author
    Posts
  • #24134
    Andy Fragen
    Moderator

    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.

    #24203
    Barry
    Member

    Hi 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 ๐Ÿ™‚

    #24262
    Andy Fragen
    Moderator

    Thanks 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

    #24266
    Barry
    Member

    Hmm, 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?

    #24273
    Andy Fragen
    Moderator

    OK, 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?

    #24277
    Barry
    Member

    I 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 ๐Ÿ™‚

    #24278
    Barry
    Member

    *is empty

    #24314
    Andy Fragen
    Moderator

    Barry, 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.

    #24318
    Andy Fragen
    Moderator

    FWIW, 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)

    #24321
    Barry
    Member

    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.

    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.

    #24322
    Barry
    Member

    … 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.

    #24323
    Barry
    Member

    OK, 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?

    #24325
    Andy Fragen
    Moderator

    Yes, 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.

    #24345
    Barry
    Member

    In terms of adding an additional field programmatically (if it doesn’t already exist), does this work for you? http://pastebin.com/s2k0zZHi

    #24376
    Andy Fragen
    Moderator

    Barry, 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’ );

Viewing 15 posts - 1 through 15 (of 26 total)
  • The topic ‘Programmatically setting Additional Fields’ is closed to new replies.