Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Activity Custom Post Type.
<?php
/**
* Add Activity Custom Post Type.
*/
class YZC_Activity_Custom_Post_Type {
public $post_title;
public $post_type;
public $post_action;
public $enable_upload_attachments;
public $form_button_icon;
function __construct( ) {
// Init Variables
$this->post_title = 'Problem';
$this->post_type = 'activity_problem';
$this->post_action = 'Added a new problem';
// Enable Upload Attachments, Accepts ( on/off ).
$this->enable_upload_attachments = 'on';
// Form Button icon. Check over 1000 icon from : https://fontawesome.com/
$this->form_button_icon = 'fas fa-exclamation-triangle';
// Filters.
add_filter( 'youzify_wall_show_everything_filter_actions', array( $this, 'show_everything_filter' ) );
add_filter( 'youzify_wall_post_types_visibility', array( $this, 'visibility' ) );
add_filter( 'youzify_get_activity_content_body', array( $this, 'content' ), 20, 2 );
add_filter( 'youzify_wall_form_post_types_buttons', array( $this, 'form_button' ) );
add_filter( 'youzify_allowed_form_post_types', array( $this, 'allow_post_type' ) );
// Actions.
add_action( 'youzify_before_adding_wall_post', array( $this, 'validate' ), 30, 2 );
add_action( 'youzify_after_adding_wall_post', array( $this, 'save' ), 30, 2 );
add_action( 'bp_register_activity_actions', array( $this, 'register_action' ) );
add_action( 'youzify_after_wall_post_form_textarea', array( $this, 'form_fields' ) );
}
/**
* Custom Post Type Form Field HTML.
*/
function form_fields() {
?>
<div class="youzify-wall-custom-form" data-post-type="<?php echo $this->post_type; ?>">
<div class="youzify-wall-cf-item">
<input type="text" class="youzify-wall-cf-input" name="text_field_example" placeholder="<?php _e( 'Text Field Example', 'youzify' ); ?>" />
</div>
<div class="youzify-wall-cf-item">
<textarea name="textarea_field_example" class="youzify-wall-cf-input" placeholder="<?php _e( 'Description Field Example', 'youzify' ); ?>"></textarea>
</div>
<div class="youzify-wall-cf-item">
<label class="youzify-cf-option-item">
<input type="checkbox" class="youzify-cf-option-input youzify-cf-checkbox-input" name="checkbox_field_example"/>
<span class="youzify-cf-option-title"><?php _e( 'Checkbox Field Example', 'youzify' ); ?></span>
</label>
</div>
</div>
<?php
}
/**
* Post Content
*/
function content( $content, $activity ) {
if ( $this->post_type == $activity->type ) {
$content = $this->get_post_content( $activity );
}
return $content;
}
/**
* Poll Post Content
*/
function get_post_content( $activity ) {
// Get Form Data.
$text_field = bp_activity_get_meta( $activity->id, 'text_field_example' );
$textarea_field = bp_activity_get_meta( $activity->id, 'textarea_field_example' );
$checkbox_field = bp_activity_get_meta( $activity->id, 'checkbox_field_example' );
// Make checkbox field returns on or off.
$checkbox_field = $checkbox_field == 'on' ? 'on' : 'off';
ob_start();
?>
<div class="activity-inner">
<p><?php echo $activity->content; ?></p>
<div class="youzify-custom-post-data">
<p><strong>Text field value : </strong><?php echo $text_field; ?></p>
<p><strong>Textarea field value : </strong><?php echo $textarea_field; ?></p>
<p><strong>Checkbox field value : </strong><?php echo $checkbox_field; ?></p>
</div>
<!-- Call Attachments -->
<?php $this->attachments( $activity->id ); ?>
</div>
<?php
ob_flush();
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Get Post Attachments.
*/
function attachments( $activity_id ) {
// Get Attchments.
$attachments = youzify_get_activity_attachments( $activity_id );
if ( empty( $attachments ) ) {
return;
}
// Image Extensions
$image_extensions = array( 'jpeg', 'jpg', 'png', 'gif', 'tif' );
// Video Extensions
$video_extensions = array( 'mp4', 'ogg', 'ogv', 'webm', 'mov','flv' );
// Audio Extensions
$audio_extensions = array( 'mp3', 'ogg', 'wav', 'm4a','m4b','mid','midi' );
// Get Filename.
$filename = isset( $attachments[0]['original'] ) ? $attachments[0]['original'] : '' ;
if ( empty( $filename ) ) {
return;
}
// Get Uploaded File extension
$ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
// Get Attachments Content.
if ( in_array( $ext, $image_extensions ) ) {
youzify_activity()->get_wall_post_images( $attachments, $activity_id );
} elseif( in_array( $ext, $video_extensions ) ) {
youzify_activity()->get_wall_post_video( $attachments, $activity_id );
} elseif ( in_array( $ext, $audio_extensions ) ) {
youzify_activity()->get_wall_post_audio( $attachments, $activity_id );
} else {
youzify_activity()->get_wall_post_file( $attachments, $activity_id );
}
}
/**
* Validate Form Data
*/
function validate( $post, $using_ajax = false ) {
// Get Post Type.
$post_type = sanitize_text_field( $post['post_type'] );
if ( $this->post_type == $post_type ) {
// Validate Text Field.
$text_field = trim( $post['text_field_example'] );
if ( empty( $text_field ) ) {
$this->display_wall_form_message( 'error', __( 'Text field is empty!', 'youzify' ), $using_ajax );
}
// Validate Textarea Field.
$textarea_field = trim( $post['textarea_field_example'] );
if ( empty( $textarea_field ) ) {
$this->display_wall_form_message( 'error', __( 'Textarea field is empty!', 'youzify' ), $using_ajax );
}
}
}
/**
* Save Post Type Data.
*/
function save( $activity_id, $data ) {
// Save Fields
bp_activity_update_meta( $activity_id, 'text_field_example', $data['text_field_example'] );
bp_activity_update_meta( $activity_id, 'textarea_field_example', $data['textarea_field_example'] );
bp_activity_update_meta( $activity_id, 'checkbox_field_example', $data['checkbox_field_example'] );
}
/**
* Post Form - Button Arguments.
*/
function form_button( $post_types ) {
// Button Data.
$post_types[ $this->post_type ] = array(
'show' => 'on',
'name' => $this->post_title,
'uploader' => $this->enable_upload_attachments,
'icon' => $this->form_button_icon,
);
return $post_types;
}
/**
* Add custom post type To the allowed form post types.
*/
function allow_post_type( $post_types ) {
$post_types[] = $this->post_type;
return $post_types;
}
/**
* Add custom post type to appear in the activity stream default feed.
*/
function show_everything_filter( $actions ) {
$actions[] = $this->post_type;
return $actions;
}
/**
* Enable Activity Posts Visibility.
*/
function visibility( $post_types ) {
$post_types[ $this->post_type ] = 'on';
return $post_types;
}
/**
* Register Wall New Custom Post Action.
*/
function register_action() {
// Init Vars
$bp = buddypress();
bp_activity_set_action(
$bp->activity->id,
$this->post_type,
$this->post_title,
'yzc_custom_post_type_action',
$this->post_title,
array( 'activity', 'group', 'member', 'member_groups' )
);
}
/**
* Display Form Poup
*/
function display_wall_form_message( $type, $msg, $ajax = false ) {
if ( $ajax ) {
youzify_die( $msg );
} else {
$this->redirect( $type, $msg );
}
}
/**
* Redirect User.
*/
public function redirect( $action, $msg ) {
// Add Message.
bp_core_add_message( $msg, $action );
// Redirect User.
wp_redirect( wp_get_referer() );
// Exit.
exit;
}
}
new YZC_Activity_Custom_Post_Type();
/**
* Custom Post Type Actions
*/
function yzc_custom_post_type_action( $action, $activity ) {
$action = sprintf( __( '%s posted a new problem', 'youzify' ), bp_core_get_userlink( $activity->user_id ) );
return apply_filters( 'yzc_custom_post_type_action', $action, $activity );
}
@Radzio1615

This comment has been minimized.

Copy link

@Radzio1615 Radzio1615 commented Nov 1, 2020

Hi! I used your snippet and added a YouTube video URL to the main activity content field, but the video is not embedding. How to fix it?

@JENNIS015

This comment has been minimized.

Copy link

@JENNIS015 JENNIS015 commented Nov 19, 2020

Hi! I used your snippet and added a YouTube video URL to the main activity content field, but the video is not embedding. How to fix it?

Can you do it? I have the same problem with the url preview when i posted it. @Radzio1615

@Amoosimeon

This comment has been minimized.

Copy link

@Amoosimeon Amoosimeon commented Mar 9, 2021

Hi, i added a custom post type to wordpress, and this to add to activity stream but only the admin activity gets registered, what am i not doing right?

@KaineLabs

This comment has been minimized.

Copy link
Owner Author

@KaineLabs KaineLabs commented Mar 9, 2021

Hello, Done, I just updated the snippet!

@Amoosimeon

This comment has been minimized.

Copy link

@Amoosimeon Amoosimeon commented Mar 9, 2021

Thank you, if possible help with a snippet that allows custom post types created in wordpress to be shown in activity stream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment