• Resolved steve098

    (@steve098)


    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/

Viewing 15 replies - 1 through 15 (of 28 total)
  • 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/

    Thread Starter steve098

    (@steve098)

    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

    Thread Starter steve098

    (@steve098)

    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

    Plugin Author Marcus (aka @msykes)

    (@netweblogic)

    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!

    Thread Starter steve098

    (@steve098)

    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;
    }
    Thread Starter steve098

    (@steve098)

    [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;
    }

    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.

    Thread Starter steve098

    (@steve098)

    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

    Plugin Author Marcus (aka @msykes)

    (@netweblogic)

    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.

    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?

    Plugin Author Marcus (aka @msykes)

    (@netweblogic)

    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!

    Thread Starter steve098

    (@steve098)

    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();

    Plugin Author Marcus (aka @msykes)

    (@netweblogic)

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

    Thread Starter steve098

    (@steve098)

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

    Plugin Author Marcus (aka @msykes)

    (@netweblogic)

    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.

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘WP to twitter plugin and Events Manager event attributes’ is closed to new replies.