Skip to:
Content
Pages
Categories
Search
Top
Bottom

Best way to add custom posts to activity stream

  • @leanneoleary

    Participant

    I have a custom post type with custom meta which is submitted via the frontend. Whenever the form is submitted I need to register this in the activity stream. I have had a look around the documentation and using the function bp_activity_add() looks like the obvious solution but in previous forum posts it has been mentioned to use the following code instead:

    add_filter ( 'bp_blogs_record_post_post_types', 'activity_publish_custom_post_types' );
    function activity_publish_custom_post_types() {
      $media = array('videos', 'pictures');
      return $media;
    }

    Can anyone advise which would be the better the option and also how to implement the code properly? I am fairly new to both wordpress and buddypress and so do not know whether to add this code to the main functions.php file and if so how.

    many thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • @henrywright

    Moderator

    Hi @leanneoleary

    bp_blogs_record_post_post_types filters the post types to track. The function you posted is telling the Blogs component to track videos and pictures.

    bp_activity_add() simply adds items to the activity stream. See here for how it’s used.

    Now, I think posts of type videos and pictures will be automatically added to the stream due to activity_publish_custom_post_types(). But, if they aren’t you’ll need to use bp_activity_add() as well.

    Does this help?

    @leanneoleary

    Participant

    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();
    

    @henrywright

    Moderator
    function leanneoleary_custom_post_types( $post_types ) {
        $post_types[] = 'files';
        return $post_types;
    }
    add_filter ( 'bp_blogs_record_post_post_types', 'leanneoleary_custom_post_types' );

    If your custom post type is files, then just try that? You’ll need to ensure the Site tracking component is enabled via WP admin.

    @leanneoleary

    Participant

    I have checked and Site Tracking is enabled. I have implemented the code as you have said but nothing happens. The form is submitted as usual but nothing appears in the activity stream. I don’t even know if the function is being activated as I have tried using print_r($post_types); within the function but still nothing.

    @leanneoleary

    Participant

    Where exactly should I be adding this code?

    @leanneoleary

    Participant

    OK, I discovered I needed to remove the code from my class and add it to the main functions.php page to work.

    @henrywright

    Moderator

    Yes, your theme’s functions.php file is where it should go. Glad you got it to work.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Best way to add custom posts to activity stream’ is closed to new replies.
Skip to toolbar