Skip to content

Snippets / PHP

PHP

Post Submitted: Add timeline post to wall

T [email protected] · 219 views · Updated 1 year ago
With the new 1.5 features, many users are likely building community spaces. In this example, a community/social website has a marketplace available. When a user submits a new item in the marketplace, we want to automatically add a timeline post to the marketplace's wall so that all users who are following the marketplace will see the timeline post in their newsfeed.
This code uses the app event located in Voxel -> App Events -> Your Post Type -> User Submitted a New PostYou can download and install the Code Snippets plugin and paste this code into a new snippet. Make sure to replace the beginning of this code with your own version. **** add_action( 'voxel/app-events/post-types/YOUR_POST_TYPE/post:submitted', function( $event )   Make sure to replace this ID with the post ID of the post wall you want to create a timeline post for. **** 'post_id' => 196,
// Handler for post submission event to create timeline entry
add_action( 'voxel/app-events/post-types/equioment/post:submitted', function( $event ) {
global $wpdb;

// Get required data
$post_id = $event->post->get_id();
$user_id = $event->post->get_author()->get_id();
$permalink = get_permalink($post_id);

// Get featured image URL
$thumbnail_id = get_post_thumbnail_id($post_id);
$image_url = wp_get_attachment_url($thumbnail_id);

// Prepare the details array
$details = array(
'link_preview' => array(
'url' => $permalink,
'title' => $event->post->get_title() . ' - ' . parse_url(get_site_url(), PHP_URL_HOST),
'image' => $image_url
)
);

// Prepare data for insert
$data = array(
'user_id' => $user_id,
'post_id' => 196,
'feed' => 'post_wall',
'content' => $permalink,
'details' => wp_json_encode($details),
'moderation' => 1,
'created_at' => current_time('mysql'),
'like_count' => 0,
'reply_count' => 0,
'_index' => str_replace(array(':', '/', '.', '-'), array('U_3A', 'U_2F', 'U_2E', 'U_2D'), $permalink)
);

// Insert the record
$table_name = $wpdb->prefix . 'voxel_timeline';
$result = $wpdb->insert($table_name, $data);

// Log the result if debugging is enabled
if (defined('WP_DEBUG') && WP_DEBUG === true) {
if ($result === false) {
error_log('Timeline insert failed for post ID: ' . $post_id . ' - ' . $wpdb->last_error);
} else {
error_log('Timeline insert successful for post ID: ' . $post_id . ' - Timeline ID: ' . $wpdb->insert_id);
}
}
});
PHP · 49 lines

Discussion

Ask a question or share how you used this.

Log in to join the discussion.

No comments yet — be the first.

Voxel Guide assistant
Ask about snippets, addons & how-tos

Hey — how can I help?

I can dig through the library for snippets, addons, and tutorials.

AI can be wrong — double-check anything important.