Code to add image to an event

Home Forums Calendar Products Events Calendar PRO Code to add image to an event

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1065119
    Keith
    Participant

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

    #1065747
    George
    Participant

    Hey @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!
    George

    #1065751
    Keith
    Participant

    Hi 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,
    keith

    #1065950
    George
    Participant

    Sounds 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,
    George

    #1066408
    Keith
    Participant

    Hello 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,
    Keith

    #1066524
    George
    Participant

    Ah, 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

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Code to add image to an event’ is closed to new replies.