WordPress.org

Ready to get started?Download WordPress

Forums

Events Manager
[resolved] WP to twitter plugin and Events Manager event attributes (29 posts)

  1. steve098
    Member
    Posted 11 months ago #

    This is related to another plugin and how it would operate with Events Manager, my apologies if this is inappropriate for this forum

    I would be interested to hear from anyone who has succeeded in forming a tweet that contains event and location attributes.

    I'd like to have a tweet that follows

    New Event at #_LOCATIONNAME on #_EVENTDATES #url#

    WP to twitter says:

    You can also create custom shortcodes to access WordPress custom fields. Use doubled square brackets surrounding the name of your custom field to add the value of that custom field to your status update. Example: custom_field

    I have tried various permutations of [[#_LOCATIONNAME] etc to no avail. My issue is probably around understanding what is classified as a custom field in WP, and how to access the event information in the required format.

    I believe there has been discussion related to the WP to Twitter and EM being otherwise compatible. I can successfully produce tweets using the plugin's shortcodes related to the post, but none of these relate to the event information.

    many thanks

    http://wordpress.org/plugins/events-manager/

  2. Joe Dolson
    Member
    Posted 11 months ago #

    If those fields are stored as custom meta data with the event, WP to Twitter should be able to pick them up; but you'll have to get an answer from Marcus to find out how that data is stored, as I'm not sufficiently familiar with Events Manager to be able to suggest anything.

    Even if the fields are stored in some other way, you can probably access them using custom code through custom template tag filter available in WP to Twitter; although that's a somewhat advanced usage.

    http://www.joedolson.com/articles/2013/06/using-custom-template-tags-with-wp-to-twitter/

  3. steve098
    Member
    Posted 11 months ago #

    Joe thankyou for your quick response. I can see your suggestion of a custom template tag filter working to retrieve the information.

    It will be a steep learning curve for my limited php and EM skills. In the absence of an existing solution or a more simple approach, if I get it working I'll post the code here.

    thankyou

  4. steve098
    Member
    Posted 11 months ago #

    I have come up with a solution which integrates well with Event Manager's placeholders

    in my theme's function.php I added

    add_filter( 'wpt_custom_shortcode', 'my_event_location', 10, 3 );
    function my_url( $value, $post_ID, $field ) {
        if ( $field == 'my_event_location' ) {
    		$EM_Event = em_get_event($post_ID, 'post_id');
    		$mylocation = $EM_Event->output('#_LOCATIONNAME');
    		return $mylocation;
        }
        return $value;
    }

    Then in WP to Twitter's Basic Settings-->Settings for type events-->template for tweet

    New event: #title# at doublesquarebracketsmy_event_location
    doublesquarebrackets

    (this forum won't allow double square brackets to be posted!)

    This may not have been such an achievement for most people...but I am pleased!

    Thankyou for the advice

  5. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    I'm not aware of the filters wp2t offers, but if you have the right filter, the code from our side of things looks fine.

    Good workaround!

  6. steve098
    Member
    Posted 11 months ago #

    thanks

    typo in my code in my original post. Corrected below:

    add_filter( 'wpt_custom_shortcode', 'my_event_location', 10, 3 );
    function my_event_location( $value, $post_ID, $field ) {
        if ( $field == 'my_event_location' ) {
    		$EM_Event = em_get_event($post_ID, 'post_id');
    		$mylocation = $EM_Event->output('#_LOCATIONNAME');
    		return $mylocation;
        }
        return $value;
    }
  7. steve098
    Member
    Posted 11 months ago #

    [EDITED 31-7-13 15:03] There appears to be a timing/triggering issue when including placeholder information in a tweet about a new event.

    Tweets for an updated event are working fine.

    I have set the template for tweets for both new and updated events to be

    Event updated: #title#. [[my_evt_startdate][[my_evt_location].#url#

    where my_evt_startdate and my_evt_location are surrounded with doublebrackets, and defined in my funtions.php as below.

    The tweet for a new event reads

    Event updated: My Event Title. 31/07/2013. goo.gl/shorturl

    (note the missing event location, and also that the event date is today's date)

    Making a minor change to the title of the event and publishing the change, the tweet then reads correctly:

    Event updated: My Event Title2. 09/08/2013 My Location Name. goo.gl/shorturl

    (note that the location has been filled in, and also that the actual date of the event is populated instead of today's date.

    So it appears the the placeholders are populated after the tweet has been triggered, and that #_EVENTDATES returns today's date in the absence of a defined date?

    My limited knowledge thinks it might be similar to the below post?
    http://wordpress.org/support/topic/issue-between-events-manager-wp-to-twitter?replies=13

    This is where my knowledge ends! Perhaps there is a better method of retrieving the location and start date.

    additions to functions.php:

    add_filter( 'wpt_custom_shortcode', 'my_evt_location', 10, 3 );
    function my_evt_location( $value, $post_ID, $field ) {
        if ( $field == 'my_evt_location' ) {
    		$EM_Event = em_get_event($post_ID, 'post_id');
    		$mylocation = $EM_Event->output('#_LOCATIONNAME');
    		return $mylocation;
        }
        return $value;
    }
    
    add_filter( 'wpt_custom_shortcode', 'my_evt_startdate', 10, 3 );
    function my_evt_startdate( $value, $post_ID, $field ) {
        if ( $field == 'my_evt_startdate' ) {
    		$EM_Event = em_get_event($post_ID, 'post_id');
    		$mylocation = $EM_Event->output('#_EVENTDATES');
    		return $mylocation;
        }
        return $value;
    }
  8. Joe Dolson
    Member
    Posted 11 months ago #

    I don't know if this is the case with events manager, but a common problem with accessing custom meta data with WP to Twitter happens if the custom meta data is added to the post after it is published, rather than before.

    WP to Twitter runs when the post is published, but a common (and wrong) model for saving custom meta is this:

    $post_id = wp_insert_post( post_status=publish );
    add_custom_meta( $post_id );

    Instead of this:

    $post_id = wp_insert_post( post_status=draft );
    add_custom_meta( $post_id );
    wp_publish_post( $post_id );

    The former causes problems with WP to Twitter because the meta data is not yet saved when the Tweet is created.

  9. steve098
    Member
    Posted 11 months ago #

    thankyou for your reply Joe,
    I too don't know enought about Events Manager to tell what is happening when an event is saved via the EM admin interface.

    Luckily for me the majority of my events I will be creating via a third party import plugin, in which I can control post-insertion in the manner you suggest above. I will adjust the code and see what happens.

    thanks

  10. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    oops, we're guilty on the front-end :) will fix that in next update

    however, not so sure on the back-end, would have to test it out further. Joe, maybe you already know about hooking in on save_post? at that point we save our meta in the wp-admin area.

  11. Joe Dolson
    Member
    Posted 11 months ago #

    Hooking into save_post should generally be fine, I'd think - save_post runs during wp_insert_post and during wp_publish_post, so as long as you're not doing something tricky to prevent it from running when the post is inserted, I'd expect that to work.

    But if it isn't...maybe we should both take a look and try and see what's going on. I would expect it to be fine; the save_post action runs during wp_insert_post, then the custom shortcode filter runs during publish_{post_type}.

    WP to Twitter runs at priority 16 for publishing, so it should happen after most publishing actions, as well -- where is Events Manager running?

  12. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    We run save_post on priority 10 so I'm guessing save_post will be fine in any case admin-side, given all our meta data gets saved at that point.

    So it's just the front-end save function that needs fixing.

    Thanks for your input Joe!

  13. steve098
    Member
    Posted 11 months ago #

    sorry to throw a cat amongst the pigeons, but the issue of missing placeholder info was happening on new events created from wp-admin area

    wp-admin-->Events--> Add Event
    Enter a title, description, (it autosaves a draft here), then a date and a location,
    I then click PUBLISH button (this is the first time the event has been saved), and I get the tweet:

    New Event: test2. 02/08/2013 at . http://goo.gl/shortcode

    (the event start date is wrong, the event location is missing.

    Still in wp-admin, I then click the UPDATE button (in the same place as the publish button), and I get the tweet

    Event updated: test2. 25/12/2013 MyLocation.http://goo.gl/shortcode

    (event start date is correct, event location is correct.)
    (the tweet templates are the same other than the first two words).

    A workaround is to simply press 'save draft' first, then click 'publish' on a new event, and this fixes the issue.

    Is my use of the custom filters causing the issues? The function names 'my_evt_location' and 'my_evt_startdate' don't exist anywhere else other than with my filters in functions.php for the theme...I don't really know what I'm doing here

    FYI I have a separate plugin that that imports events via separate process, which only tweets EM placeholder info if I save to draft first, then save_meta, then save&publish

    /* new event object
    $event = new EM_Event();
    /*define other event attribs here*/
    /* before saving event, force post to draft */
    $event->force_status = 'draft';
    /*save event*/
    $event->save();
    /* save_meta is called from within save? in case not: */
    $event->save_meta();
    /* play with categories, then save and publish */
    
    $event->force_status = 'publish';
    $event->save();
  14. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    @steve098 thanks for confirming that.

    I'll need to run through the process closely then and see where it might be going wrong. Chances are this'll be pushed to 5.5.1 as we're trying to push out the 5.5 update asap.

  15. steve098
    Member
    Posted 11 months ago #

    no probs, the workaround of saving draft first then publish is no hassle.

  16. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    This has been bugging me because it indicates a problem in some core hooking, so I took a closer look today, sorry for the essay :=)

    Joe, I've figured out why odd things are happening when posting events (this is all admin-side), and it's a gray area as to who's doing what wrong.

    Your real tweeting hook from what I see is publish_event (publish_(post_type)), which thurns out gets executed BEFORE save_post via the wp_transition_post_status function and is done in all the relevant functions; wp_update_post, wp_insert_post and wp_publish_post functions, which is where we come in. So when your plugin tweets, we didn't save our data yet.

    http://core.trac.wordpress.org/browser/tags/3.6/wp-includes/post.php#L2874

    Now as to who's doing it wrong... your way is ok but likely to cause errors with many plugins, as ultimately the problem is with WP itself i think.

    For you it's easier. I'd suggest for your plugin using only save_post with a late priority for all tweet actions and check the post status to see if it's published, it'll prevent problems like this with custom post types because most docs out there (including books) and even the codex - http://codex.wordpress.org/Plugin_API/Action_Reference/save_post - seem to recommend the use of save_post to hook into CPTs.

    For me, there's no easy hook in to CPTs before these traditional hooks and save our post meta, validate and modify the post if needed (e.g. to draft if validation fails). If so, I'd use that way instead and your 'fix' wouldn't be necessary. It's a problem with wp_insert_post and warrants a ticket on core trac imo after a little more investigation.

  17. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    I have made a fix to EM in the upcoming dev update (5.4.4.3+) that'll validate before a post even saves itself which will fix the problem when an event/location doesn't validate

    I also have an adjustment to the above code for a shortcode in your tweet (note that the upcoming update is required) -

    function my_wp2twitter_event_location( $value, $post_ID, $field ) {
        global $EM_Event;
        if ( $field == 'my_event_location' ) {
        	$EM_Event = em_get_event($EM_Event);
    		$mylocation = $EM_Event->output('#_LOCATIONNAME', 'raw');
    		return $mylocation;
        }
        return $value;
    }
    add_filter( 'wpt_custom_shortcode', 'my_wp2twitter_event_location', 10, 3 );
  18. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    opened up a can of worms here :) turns out i'm going to have to backtrack here a litle.

    this approach doesn't work:

    $post_id = wp_insert_post( post_status=draft );
    add_custom_meta( $post_id );
    wp_publish_post( $post_id );

    because a new post won't have a slug if saved as draft, and wp_publish_post doesn't assign a slug either (which may be another wp bug?).

    For now I'm going to have to revert the front-end changes I was intending to add to what it is currently. Since on the front-end wp_insert_post only gets fired if the event is validated, and will only be published by those with the right permissions, the solution mentioned above should still work.

  19. Joe Dolson
    Member
    Posted 11 months ago #

    wp_publish_post doesn't assign a slug? That's surprising to me - I've used this pattern a lot, and I've never had any issues with a missing slug.

  20. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    I know, you'd think, right? If you supply a slug to begin with, then it's fine, but if you don't (which is meant to be acceptable) then things go wrong and no slug gets assigned on wp_publish_post.

    I guess I could just run sanitize_title before wp_insert_post myself, but then there's other issues I need more time to test and investigate so I'll be postponing this for another update, as 5.5 needs to go out asap.

  21. Joe Dolson
    Member
    Posted 11 months ago #

    Well, that's weird. Also, I completely overlooked your prior update concerning save_post. There was a reason I wasn't using save_post previously, although I don't recall what it was right now -- but that was a long time ago, and I don't even support those versions of WordPress anymore, so it's worth looking at again.

    I do agree, however, that this is ultimately a bug in WP - publish_{custom_post_type} should happen in the same sequence as publish_{post}; it's just weird that it doesn't.

    Still, I guess we just have to work around it.

  22. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 11 months ago #

    afaik unless it's due to other plugins messing with yours, save_post would make more sense to me, as it's run last. I'd guess it'd cure more headaches than it causes, but now I'm guessing ;)

  23. steve098
    Member
    Posted 10 months ago #

    [EDIT 06/09/13 01:02] some more detail on the issue:
    If I create an event, save as draft first, then publish, then the subsequent tweet is complete (nothing new from the above posts)
    BUT
    If I change the date of the published event and republish/update, the tweet still shows the old event date, not the updated event date, until I press the UPDATE button a second time (which produces a second tweet also)

    I wonder if this sheds any more light on the original problem of a virgin published event not tweeting placeholders correctly? In this new example, the event already has a slug assigned, but (I think) the meta data for the event is being updated, and the tweet is being triggered prior to this being updated in wp-admin for the event?

    (WP 3.6 EM 5.5.1.)

    function my_wp2twitter_event_location( $value, $post_ID, $field ) {
        global $EM_Event;
        if ( $field == 'my_event_location' ) {
        	$EM_Event = em_get_event($EM_Event);
    		$mylocation = $EM_Event->output('#_LOCATIONNAME', 'raw');
    		return $mylocation;
        }
        return $value;
    }
    add_filter( 'wpt_custom_shortcode', 'my_wp2twitter_event_location', 10, 3 );
    
    add_filter( 'wpt_custom_shortcode', 'my_evt_startdate', 10, 3 );
    function my_evt_startdate( $value, $post_ID, $field ) {
        if ( $field == 'my_evt_startdate' ) {
    		$EM_Event = em_get_event($post_ID, 'post_id');
    		$mylocation = $EM_Event->output('#_EVENTDATES');
    		return $mylocation;
        }
        return $value;
    }
  24. Joe Dolson
    Member
    Posted 10 months ago #

    I wouldn't say it sheds more light, but it's consistent with the previously identified issue.

    My next release of WP to Twitter is intended to try and resolve this issue. The development version is already available here: http://wordpress.org/plugins/wp-to-twitter/developers/

    It's pretty stable, although I'm not prepared to call it finished yet; so you're welcome to give it an install and try it out.

    If you do try it, let me know if it fixes the problem!

  25. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 10 months ago #

    will be a while before I can, but have noted this down, will test asap too!

  26. steve098
    Member
    Posted 9 months ago #

    Just to let you all know, that I have upgraded to
    WP 3.6.1
    EM 5.5.1
    WPtoTwitter 2.7.5
    and this combination now works! I can create a new event in wp-admin area and publish immediately, and it will correctly tweet the event start date and location name.
    For the benefit of others,
    The WP to Twitter template for a new event tweet is
    New Event: #title#. my_evt_startdate at my_wp2twitter_event_location. #url#
    I have included below exact code as I have entered into my theme's functions.php.

    Thankyou both for your great plugins and your discussions around this topic.
    Steve

    functions.php additions:

    add_filter( 'wpt_custom_shortcode', 'my_wp2twitter_event_location', 10, 3 );
    function my_wp2twitter_event_location( $value, $post_ID, $field ) {
        global $EM_Event;
        if ( $field == 'my_wp2twitter_event_location' ) {
        	$EM_Event = em_get_event($EM_Event);
    		$mylocation = $EM_Event->output('#_LOCATIONNAME', 'raw');
    		return $mylocation;
        }
        return $value;
    }
    
    add_filter('wpt_custom_shortcode', 'my_evt_startdate', 10, 3);
    function my_evt_startdate($value, $post_ID, $field)
    {
    global $EM_Event;
        if ($field == 'my_evt_startdate') {
            $EM_Event   = em_get_event($post_ID, 'post_id');
            $mydates = $EM_Event->output('#_EVENTDATES');
            return $mydates;
        }
        return $value;
    }
  27. steve098
    Member
    Posted 9 months ago #

    arg, this forum doesn't like double square brackets! For the WP to Twitter code, you will have to surround my_evt_startdate and my_wp2twitter_event_location with double square brackets.

  28. Joe Dolson
    Member
    Posted 9 months ago #

    Thanks for following up!

    And yeah, if I'd known when I chose them that double square brackets were used by these forums to generate internal links, I would have picked something else...

  29. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 9 months ago #

    yes @steve098 thanks for following up, and thanks Joe for the input! end result - two plugins better off than before :)

Reply

You must log in to post.

About this Plugin

About this Topic