Multiple post meta inputs to a single property

Hi,

I am using SNIP in conjunction with Events Calendar Pro and am having trouble with setting up the events snippet. 

I am getting an error stating the start date doesnt have the timezone listed. is there a way to add multiple post meta to a property. I am currently using the 

_EventStartDate for the start date but from the documentation can see that it also has _EventTimezone as an option. 

How do i add these together so google can read the rich snippet. 

Feel free to test my link to see what I am saying. 

https://jummedia.com.au/live/mary-gray/

Thanks,

James

6 thoughts on “Multiple post meta inputs to a single property

  1. Hey James,
    and thanks for your post. I hope you’re doing well today.

    Unfortunately there is currently no visual option to add multiple post meta values into one property. However if you’re familiar with PHP you can set-up your own custom field type where you can do whatever you want.

    Hope that helps.

    Greetings
    Florian

  2. Hi Florian,

    Thank you for your reply.

    I am not familar with PHP. is this something you would be able to help with.

    I have followed the code and tried to make changes as per what I require but when activating the plugin get a fatal error on line 73.

    
    <?php
    /*
    Plugin Name: SNIP Basic Field Type - Start Time and Timezone
    Description: A plugin that adds a new Field Type in SNIP.
    Author: Florian Simeth
    Version: 0.1.0
    Author URI: https://rich-snippets.io
    Plugin URI: https://rich-snippets.io/how-to-add-your-own-field-type/
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    } // Exit if accessed directly
    
    /**
     *
     * PHP Version check.
     *
     */
    if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
    	add_action( 'admin_notices', 'snip_fte_old_php_notice' );
    
    	function snip_fte_old_php_notice() {
    
    		printf(
    			'%s',
    			sprintf(
    				__( 'Hey mate! Sorry for interrupting you. It seem\'s that you\'re using an old PHP version (your current version is %s). You should upgrade to at least 7.0 or higher in order to use the SNIP Field Type Example plugin. Thank you!', 'snip-fte' ),
    				esc_html( PHP_VERSION )
    			)
    		);
    	}
    
    	# sorry. The plugin will not work with an old PHP version.
    	return;
    }
    
    add_filter( 'wpbuddy/rich_snippets/fields/internal_subselect/values', 'snip_fte_subselects' );
    
    /**
     * Adds new field to use in Global Snippets in the SNIP plugin.
     *
     * @param array $values
     *
     * @return array
     * @since 0.1.0
     *
     */
    function snip_fte_subselects( $values ) {
    
    	$values['http://schema.org/DateTime'][] = [
    		'id'     => 'snip_fte_starttime_timezone',
    		'label'  => esc_html_x( 'Start Time with Timezone', 'subselect field', 'snip-fte' ),
    		'method' => 'snip_fte_starttime_timezone_callback',
    	];
    
    	return $values;
    }
    /**
     * Returns the value.
     *
     * @param                                     $val
     * @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
     * @param array                               $meta_info
     *
     * @return int|float
     */
    function snip_fte_starttime_timezone_callback( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
    
    	/**
    	 * Load and/or calculate something here.
    	 */
    	$value = _EventStartTime,_EventTimezone;
    
    	return $value;
    }
    
  3. Cool! You’re almost there! 😉

    Change the following line:
    $values['http://schema.org/DateTime'][] = [
    to
    $values['http://schema.org/Date'][] = [

    Also, what’s missing is the function that is actually fetching the post meta. You can replace the last function like this:

    
    function snip_fte_starttime_timezone_callback( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
    
    	/**
    	 * Load and/or calculate something here.
    	 */
    	return sprintf(
    		'%s %s',
    		get_post_meta( $meta_info['current_post_id'], '_EventStartTime', true ),
    		get_post_meta( $meta_info['current_post_id'], '_EventTimezone', true )
    	);
    }
    

    The only thing that I’m not quite sure of is if it’s enough to just string together the two values as the startDate property must be in ISO_8601 format. Just try it and go back it it works or not! 😉

    Greetings
    Flo

  4. Hi Flo,

    Thanks for your work, it did call the start date but it want in the iso 8601 format.

    I have reached out to the events calendar support also to see if I can change the format of the _EventStartDate output to ‘c” which will give me the right format.

    I am now working on trying to get the organization and venue name and address fields in. As you have stated they don’t seem to be listed in the same database table. (I am really not sure how this all works properly)

    I have found this code which I tried to use in the return area of your code

    
    echo apply_filters(
        'the_content', 
        get_post_field( 'post_content', tribe_get_organizer_id() )
    );
    

    My theory is that it would allow me to change the tribe get to the fields i needed? But this doesn’t work.

    Is developing this plugin something I could have you assist me with? I am happy to pay for your time working on it? It would just save alot of time having this automated rather than having to override each input on all new (and past events).

    Thanks,
    James

  5. Hey James,
    I guess I can help at least with the date issue. I’ve installed the Events Calendar plugin (free version) to take a look how they store the dates. This is what I came up with:

    
    
    function snip_fte_starttime_timezone_callback( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
    
    	$start_date = get_post_meta( $meta_info['current_post_id'], '_EventStartDate', true );
    
    	$timezone = get_post_meta( $meta_info['current_post_id'], '_EventTimezone', true );
    
    	return wp_date( 'c', strtotime( $start_date ), $timezone );
    }
    

    Note that the strings are different now. At least the free version uses ‘_EventStartDate’ instead of ‘_EventStartTime’. I hope this is the same with the PRO version (that I guess you’re using).

    For all the other fields, get in touch with me via email at support [at] wp-buddy [dot] com. I’m sure we can find a solution.

    Greetings
    Flo

  6. Hi Flo,

    That seemed to work in getting the format correct, however it returned the time in Zulu time

    2020-07-29T19:00:00Z.

    At least now, my snips are not showing an error when tested. Just got to workout how to get it to display right.

    Thanks for all your help so far. Will email you about the other parts in the next day or so.

Leave a Reply