Home › Forums › Calendar Products › Events Calendar PRO › Code to add image to an event
- This topic has 5 replies, 2 voices, and was last updated 10 years, 2 months ago by
George.
-
AuthorPosts
-
January 31, 2016 at 4:12 am #1065119
Keith
ParticipantI found this post describing the basics of creating an Event. Could you provide an example of the recommended way to add an image to an Event. Say, I have an image in /tmp/myimage.jpg and I’ve just created an event, what is the preferred way to add the image to the event? Thanks for your help.
February 1, 2016 at 7:26 am #1065747George
ParticipantHey @Keith,
Thanks for reaching out.
When you say “add an image to the event”, do you mean that you want that image to be the “Featured Image” of the event? Or are you trying to add the image to event content, inline with the text of the description and such?
We cannot help with custom coding, but if you clarify the goal here I will try to write an example of code that you could take and tinker around with 🙂
Thanks!
GeorgeFebruary 1, 2016 at 7:30 am #1065751Keith
ParticipantHi George,
Thanks for getting back to me. Knowing how to do both of those would be useful, but main aim is just to set the featured image.
Cheers,
keithFebruary 1, 2016 at 1:05 pm #1065950George
ParticipantSounds good Keith!
Let’s go over adding a featured image – this is also referred to as “post thumbnail” throughout WordPress Core, so while it sometimes might seem inconsistent, know that both terms are referring to the same thing.
To add a post thumbnail the image needs be in the Media library and needs to have an attachment ID. If you’re not sure what these things imply then, unfortunately, elaborating on these details is a bit outside the scope of support we can provide but you can learn more on http://codex.wordpress.org and other resources.
But as long as the image has an attachment ID, you can use the WordPress Core function set_post_thumbnail().
An example would look like this:
// $event_id is the ID of the event.
if ( $thumbnail_id ) {
set_post_thumbnail( $event_id, $thumbnail_id );
}
Learn more here → https://codex.wordpress.org/Function_Reference/set_post_thumbnail
As for adding images to content, this is a bit simpler because the images DO NOT have to be in the media library. Literally all you need to do is have the HTML for an img tag in the text content, and you’re good to go.
tribe_create_event() is just a wrapper for wp_insert_post(), so learn more about wp_insert_post() here for starters → https://developer.wordpress.org/reference/functions/wp_insert_post/
So, you’d just need to add your img HTML tag to ‘post_content’.
Let’s take the arguments from the original example you linked to:
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'EventStartDate' => '',
'EventEndDate' => '',
'EventStartHour' => '2',
'EventStartMinute' => '29',
... MORE EVENT DETAILS ...
);That’s not all of the args, just a snippet to make the point.
To add an image to the post_content you would just add the image tag to the post_content. Let’s say the image is this one: https://cldup.com/78orE8fvms-3000×3000.png
Here’s the args with that img in the post_content:
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post, and here now is an image:
.',
'post_status' => 'publish',
'post_author' => 1,
'EventStartDate' => '',
'EventEndDate' => '',
'EventStartHour' => '2',
'EventStartMinute' => '29',
... MORE EVENT DETAILS ...
);With this information, researching online, and tinkering around with things a bit, I’m sure you’ll be able to put together the solution you want.
Best of luck with your custom development!
Cheers,
GeorgeFebruary 2, 2016 at 7:25 am #1066408Keith
ParticipantHello George,
I was actually most interested in getting images into TEC from a non-WP location on the server. Following your comments that the process is basically core-WP, I’ve been able to get it working using a straight WP solution.
Thanks for your help,
KeithFebruary 2, 2016 at 10:44 am #1066524George
ParticipantAh, apologies for not addressing that specific use-case head on. I’m glad to hear you were able to concoct a solution nonetheless.
Best of luck with your project!
George -
AuthorPosts
- The topic ‘Code to add image to an event’ is closed to new replies.
