Reply To: Best way to add custom posts to activity stream
Thanks for your help with this but I am still not sure what I need to do. The function I posted was just an example that I had found from an earlier forum, I do not need to add videos and pictures to the activity stream.
I need to add details of a custom post type every time a custom form is submitted from the frontend by a user. The custom post type is “Files”. Below is a class for the form and it is within the CMB (custom meta boxes) plugin folder. Can you advise on how to implement the code to record these forms posts in the activity stream?
class uploadForm {
// Set prefix
public $prefix = '_cmb_';
/**
* Construct the class.
*/
public function __construct() {
add_filter( 'cmb_meta_boxes', array( $this, 'cmb_metaboxes' ) );
add_shortcode( 'cmb-form', array( $this, 'do_frontend_form' ) );
add_action( 'init', array( $this, 'initialize_cmb_meta_boxes' ), 9 );
/* add_action( 'cmb_save_post_fields', array( $this, 'save_fields' ), 10, 4 );*/
add_action( 'save_post', array( $this, 'save_fields' ), 10, 4 );
/*add_filter( 'bp_blogs_record_comment_post_types', 'add_activity' ); IS THIS CORRECT? */
}
/**
* Define the metabox and field configurations.
*/
public function cmb_metaboxes( array $meta_boxes ) {
/**
* Metabox for the "Memorials" front-end submission form
*/
$meta_boxes['upload_files_metabox'] = array(
'id' => 'files',
'title' => __( 'Upload files', 'cmb' ),
'pages' => array( 'Files' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => __( 'File description', 'cmb' ),
'desc' => __( 'File title or description', 'cmb' ),
'id' => $this->prefix . 'title',
'type' => 'text',
),
array(
'name' => __( 'Project stage', 'cmb' ),
'desc' => __( 'select stage (optional)', 'cmb' ),
'id' => $this->prefix . 'category',
'type' => 'taxonomy_select',
'taxonomy' => 'stages', // Taxonomy Slug
),
array(
'name' => __( 'Link to website', 'cmb' ),
'desc' => __( 'Add url to website (optional)', 'cmb' ),
'id' => $this->prefix . 'url',
'type' => 'text_url',
'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
// 'repeatable' => true,
),
array(
'name' => __( 'Upload image', 'cmb' ),
'desc' => __( 'Upload an image or enter a URL.', 'cmb' ),
'id' => $this->prefix . 'image',
'type' => 'file',
),
array(
'name' => __( 'Upload files', 'cmb' ),
'desc' => __( 'Upload or add multiple images/attachments.', 'cmb' ),
'id' => $this->prefix . 'file_list',
'type' => 'file_list',
),
array(
'name' => __( 'oEmbed', 'cmb' ),
'desc' => __( 'Enter a youtube, twitter, or instagram URL. Supports services listed at <a href="https://codex.wordpress.org/Embeds">https://codex.wordpress.org/Embeds</a>.', 'cmb' ),
'id' => $this->prefix . 'embed',
'type' => 'oembed',
),
),
);
return $meta_boxes;
}
/**
* Shortcode to display a CMB form for a post ID.
*/
public function do_frontend_form() {
// Default metabox ID
$metabox_id = 'upload_files_metabox';
// Get all metaboxes
$meta_boxes = apply_filters( 'cmb_meta_boxes', array() );
// If the metabox specified doesn't exist, yell about it.
if ( ! isset( $meta_boxes[ $metabox_id ] ) ) {
return __( "A metabox with the specified 'metabox_id' doesn't exist.", 'cmb' );
}
// This is the WordPress post ID where the data should be stored/displayed.
$post_id = 0;
if ( $new_id = $this->intercept_post_id() ) {
$post_id = $new_id;
echo 'Thank You for your submission.';
}
// Shortcodes need to return their data, not echo it.
$echo = false;
// Get our form
$form = cmb_metabox_form( $meta_boxes[ $metabox_id ], $post_id, $echo );
return $form;
}
/**
* Get data before saving to CMB.
*/
public function intercept_post_id() {
// Check for $_POST data
if ( empty( $_POST ) ) {
return false;
}
// Check nonce
if ( ! ( isset( $_POST['submit-cmb'], $_POST['wp_meta_box_nonce'] ) && wp_verify_nonce( $_POST['wp_meta_box_nonce'], cmb_Meta_Box::nonce() ) ) ) {
return;
}
// Setup and sanitize data
if ( isset( $_POST[ $this->prefix . 'title' ] ) ) {
//add post data to database
$this->new_submission = wp_insert_post( array(
'post_title' => sanitize_text_field( $_POST[ $this->prefix . 'title' ] ),
'post_author' => get_current_user_id(),
'post_status' => 'publish', // Set to draft so we can review first
'post_type' => 'file',
), true );
// If no errors, save the data into a new post draft
if ( ! is_wp_error( $this->new_submission ) ) {
return $this->new_submission;
}
}
return false;
}
/**
* Grant temporary permissions to subscribers.
*/
public function grant_publish_caps( $caps, $cap, $args ) {
if ( 'edit_post' == $args[0] ) {
$caps[$cap[0]] = true;
}
return $caps;
}
/**
* Save custom fields and uploaded files
*/
public function save_fields( $post_id, $post ) {
if($_POST['_cmb_category'] != ''){
add_post_meta( $post_id, '_cmb_category', $_POST['_cmb_category'], true );
}
if($_POST['_cmb_url'] != ''){
add_post_meta( $post_id, '_cmb_url', $_POST['_cmb_url]'], true );
}
if($_POST['_cmb_image'] != ''){
add_post_meta( $post_id, '_cmb_image', $_POST['_cmb_image'], true );
}
if($_POST['_cmb_image_id'] != ''){
add_post_meta( $post_id, '_cmb_image_id', $_POST['_cmb_image_id'], true );
}
if($_POST['_cmb_file_list'] != ''){
add_post_meta( $post_id, '_cmb_file_list', $_POST['_cmb__file_list'], true );
}
if($_POST['_cmb_embed'] != ''){
add_post_meta( $post_id, '_cmb_embed', $_POST['_cmb_embed'], true );
}
}
/**
* Initialize CMB.
*/
public function initialize_cmb_meta_boxes() {
if ( ! class_exists( 'cmb_Meta_Box' ) ) {
require_once 'init.php';
}
}
/**
* Add to buddypress activity timeline. - HOW???
*/
public function add_activity($post_types) {
/* $activity_id = bp_activity_add( $args );
return $activity_id;*/
/* $post_types[] = 'file';
return $post_types;*/
}
} // end class
$uploadForm = new uploadForm();