• I am looking for a way to use Advanced Custom Fields as event attributes. ACF used to use the regular WordPress custom fields, but now it uses its own fields.

    I am trying to create an events list format that inserts custom fields from the user profile.

    Can you point me in the right direction for achieving this?

    It sounds like the only way may be to do a template override. In this case can I also do an override of the Formats?

Viewing 15 replies - 1 through 15 (of 19 total)
  • I think the best way is to do a template override. When you do a template override you’re just making a copy of the template and you can modify as little or as much as you want. You have complete freedom to display things just the way you like. You can use any of the standard or custom placeholders for the events (e.g., #_CONTACTNAME, #_CONTACTEMAIL).

    You can also override the output of the placeholder values using the em_event_output_placeholder filter. You could pull the values from the ACF values. You can do this even if you don’t override the templates.

    I’m not sure what you mean by “overriding the Formats”.

    Thread Starter AJD

    (@ajd)

    Hi. I think I figure out the Formats thing. in the /templates/ folder there is /templates/templates/ and /templates/formats/ . So I could override on of the formats files there.

    However, in this case I might try a custom placeholder first.

    Another quick question: Is there an easy way in Events Manager to get the ID of the user who created an event? In my usage scenario I need to get the ID of the user who created the event and then get the ACF field added to their profile.

    • This reply was modified 1 year, 4 months ago by AJD.

    The callback for the em_event_output_placeholder filter is passed 2 arguments:

    add_filter('em_event_output_placeholder','bk_em_styles_placeholders',10, 2);
    function bk_em_styles_placeholders($replace, $EM_Event, $result){
        ...
        return $replace;
    }
    
    

    The ID of the user that created the event is $EM_Event->post_author and $result is the placeholder string (e.g. #_CONTACTNAME).

    Thread Starter AJD

    (@ajd)

    Thank you.

    Thread Starter AJD

    (@ajd)

    I am trying to make a simple version that just outputs the user ID, however the code below is throwing an error: Uncaught ArgumentCountError: Too few arguments to function , 2 passed and exactly 3 expected:

     add_filter('em_event_output_placeholder','efti_em_photo_placeholders',10, 2);
    function efti_em_photo_placeholders($replace, $EM_Event, $result){
    	
    	if( $result == '#_MEMBERPHOTO' ){
    		
    		$replace = $EM_Event->post_author ('#_CONTACTID');		 
    	
                   }
        
        return $replace;
    }
    • This reply was modified 1 year, 3 months ago by AJD.
    Thread Starter AJD

    (@ajd)

    Can’t delete extra submission…

    • This reply was modified 1 year, 3 months ago by AJD.

    My mistake, the callback function takes 3 arguments.

    If you want to display the user’s photo it would be something like this (if the url to the user photo is stored in a usermeta with the key “photo”):

     add_filter('em_event_output_placeholder','efti_em_photo_placeholders',10, 3);
    function efti_em_photo_placeholders($replace, $EM_Event, $result){
        if( $result == '#_MEMBERPHOTO' ){
            $replace = "";
            $userid = $EM_Event->post_author;
            if ($userid && $userid > 0) {
                $imageurl = get_user_meta('photo', $userid, true);
                if ($imageurl) {
                    $replace = '<img src="' . $imageurl . '">';  
                }    
            }
        }
        return $replace;
    }
    Thread Starter AJD

    (@ajd)

    Thank you, the ACF field is using image ID and I got it working like this:

    add_filter('em_event_output_placeholder','efti_em_photo_placeholders',10, 3);
    function efti_em_photo_placeholders($replace, $EM_Event, $result){
        if( $result == '#_MEMBERPHOTO' ){
            $replace = "";
            $userid = $EM_Event->post_author;
            if ($userid && $userid > 0) {
    			
    	    $imageID = get_field('custom_user_avatar', 'user_' . $userid);
    
    		if( $imageID ) {
       				$replace=wp_get_attachment_image($imageID, 'large' );
    			}
    
    			else {$replace='NO';} ;
            }
        }
        return $replace;
    }
    Thread Starter AJD

    (@ajd)

    A follow up question, if I need to create a custom conditional placeholder for this custom attribute, is that possible? The usual way of creating custom conditional placeholders seems to be dependent on custom attributes created within the Events settings…

    I don’t think there’s such a thing as a custom conditional placeholder. All the conditional placeholders are the ones created by the plugin. For example:

    {has_bookings}
    <h3>Bookings</h3>
    #_BOOKINGFORM
    {/has_bookings}

    I don’t think there’s a way to create new conditional placeholders. However, you can make the custom attribute output the entire conditional part. For example you could define #_MEMBERPHOTOBLOCK which would output an empty string if there was no photo otherwise it would output a div block which would include the photo.

    add_filter('em_event_output_placeholder','efti_em_photo_placeholders',10, 3);
    function efti_em_photo_placeholders($replace, $EM_Event, $result){
        if( $result == '#_MEMBERPHOTO' ){
            $replace = "";
            $userid = $EM_Event->post_author;
            if ($userid && $userid > 0) {
    			
    	    $imageID = get_field('custom_user_avatar', 'user_' . $userid);
    
    		if( $imageID ) {
       				$replace= '<div>' . wp_get_attachment_image($imageID, 'large' ) . '</div>';
    			}
    
    			else {$replace='NO';} ;
            }
        }
        return $replace;
    }
    Thread Starter AJD

    (@ajd)

    Hi, I am using a bunch of custom conditional placeholders already for event attributes:

    https://pastebin.com/4amjiMwi

    But I do see the possibility of outputting the image and its wrapping code instead of just the image.

    • This reply was modified 1 year, 3 months ago by AJD.
    • This reply was modified 1 year, 3 months ago by AJD.

    I took a look at the code that processes conditional placeholders and it looks like it’s just matching against anything inside curly braces for example the following would be treated as a conditional placeholder.

    {has_photo}
    ...
    {/has_photo}

    To make your own custom conditional placeholder you can use the em_event_output_condition filter to process the custom conditional paceholder.

    add_filter('em_event_output_condition', 'my_em_event_output_condition', 10, 4);
    function my_em_event_output_condition($show_condition, $condition, $key, $EM_Event) {
        if ($condition == 'has_photo') {
            $userid = $EM_Event->post_author;
            if ($userid && $userid > 0) {			
    	    if (get_field('custom_user_avatar', 'user_' . $userid)) {
                     $show_condition = true;
                }
            }
        }
        return $show_condition;
    }
    Thread Starter AJD

    (@ajd)

    Interesting, I thought the conditional p-placeholder was dependent on a defined attribute, but it looks like it will work with any conditional. I added it to my other conditional placeholders like this and it is working:

    function custom_conditional_placeholder($replacement, $condition, $match, $EM_Event){
    	if (is_object($EM_Event)) {
    		switch ($condition) {
    		
     		 // custom {has_member_image}
    		case 'has_member_image': 
    		$userid = $EM_Event->post_author; 			
    	    if (get_field('custom_user_avatar', 'user_' . $userid))           
    		$replacement = preg_replace('/\{\/?has_member_image\}/', '', $match); 
    		else 
    		$replacement = ''; 
    		break; 		
    			
    		// #_ATT{has_display_dates}
    		case 'has_display_dates':
    		if (is_array($EM_Event->event_attributes) && !empty($EM_Event->event_attributes['Event Display Dates']))
    		$replacement = preg_replace('/\{\/?has_display_dates\}/', '', $match);
    		else
    		$replacement = '';
    		break;
    }
    		}
    		return $replacement;
    		}
    	add_filter('em_event_output_condition', 'custom_conditional_placeholder', 10, 4);
    Eugene Zayd

    (@eugenemsk)

    I try not to make too many similar posts …

    Joneiseman , So, if I want to add an ACF field on the event details page, should I change format/event_list_item_format.php ? If so, what exactly should I do to create a placeholder like #_EVENTMASTER (for the speaker, not the organizer/creator of the post)? If I have a taxonomy, should I repeat the process for each of its fields?

    joneiseman

    (@joneiseman)

    What placeholders you create is up to you. I was just trying to show you how to create a placeholder that picks up an ACF field (I’m not sure how you’re adding the ACF fields to the event posts). To modify the single event page see the following thread: https://wordpress.org/support/topic/some-odd-features-showing-on-my-event-page/

    When modifying the single event page you can add your placeholders. You can also modify the event list format to add your own placeholders.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Use an ACF Field for Event Attribute?’ is closed to new replies.