Skip to:
Content
Pages
Categories
Search
Top
Bottom

Inserting activity content via plugin

  • @valuser

    Participant

    Am successfully inserting a post via a plugin into a website on which buddypress is active.

    The post comes in properly and is properly recorded on the home/post page.

    However

    these posts are NOT recorded in the sitewide /users activity streams.

    $post_id = wp_insert_post( array(
    'post_author'	=> $user_id,
    'post_title'	=> $post_title,
    'post_content'	=> $apost_content,
    'post_category'	=> $category_ids,
    'tags_input'	=> $tags,
    'post_status'	=> 'publish'
    ), $wp_error );

    1) any ideas how to remedy same.

    2) I understand that as per

    bp_activity_add()


    and

    Posting Activity from Plugins

    I should be able to insert content into activity directly

    I thought i could insert the same content into Activity directly with something like:

    $activity_id = bp_activity_add( array(	
        'action'            => 'A link from NewsSite', // The activity action - e.g. "Jon Doe posted an update"
        'content'           => $post_content,        // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!"
        'component'         => false,                  // The name/ID of the component e.g. groups, profile, mycomponent
        'type'              => false,                  // The activity type e.g. activity_update, profile_updated
        'primary_link'      => 'false',                 // Optional: The primary URL for this item in RSS feeds (defaults to activity permalink)
        'user_id'           => bp_loggedin_user_id(),  // Optional: The user to record the activity for, can be false if this activity is not for a user.
        'item_id'           => false,                  // Optional: The ID of the specific item being recorded, e.g. a blog_id
        'secondary_item_id' => false,                  // Optional: A second ID used to further filter e.g. a comment_id
        'recorded_time'     => bp_core_current_time(), // The GMT time that this activity was recorded
        'hide_sitewide'     => false,                  // Should this be hidden on the sitewide activity stream?
        'is_spam'           => false                  // Is this activity item to be marked as spam?
      ), );

    but that didn’t happen!

    Any help much appreciated.

    Should be learning this stuff faster, but i’m not !!!

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

    Moderator

    Hi @valuser

    Could be a stupid question, but have you enabled ‘site tracking’?

    @valuser

    Participant

    Thanks.

    Yes.

    All options in:
    Settings-BuddyPress-Components
    and
    Settings-BuddyPress- Settings

    have been ticked.

    testing this on a local install wp 4.1 bp 2.1.1

    Theme – TwoThousand and Fifteen (so so for bp!) – plugins bp and the post submitter plugin (http://pastebin.com/s2cNJtrj)

    Permalinks have been set to (‘Day and name’)

    Posts (which atm are just clickable links) are coming into the site with correct tags and category and do show up on wp back-end posts and on front-end home/posts page exactly like normal posts

    Looking at how the posts are recorded in the database.

    All posts have an ID

    But i note that a “normal” post has a guid ending in /?p=10 or some other number (their ID)
    but
    the inserted post’s guid ends /post-name.

    there’s some little thing………

    Ideally would prefer to use bp_activity_add.

    @henrywright

    Moderator

    Any chance you can post all of your code? Perhaps in Pastebin? The code you’ve provided here looks sound so there must be something else at fault

    @valuser

    Participant

    @henrywright

    Moderator

    Having re-looked over your code for bp_activity_add() above, try setting the activity type to new_blog_post

    For example:

    $activity_id = bp_activity_add( array(	
        'action'            => 'A link from NewsSite', // The activity action - e.g. "Jon Doe posted an update"
        'content'           => $post_content,        // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!"
        'component'         => false,                  // The name/ID of the component e.g. groups, profile, mycomponent
        'type'              => new_blog_post,                  // The activity type e.g. activity_update, profile_updated
        'primary_link'      => 'false',                 // Optional: The primary URL for this item in RSS feeds (defaults to activity permalink)
        'user_id'           => bp_loggedin_user_id(),  // Optional: The user to record the activity for, can be false if this activity is not for a user.
        'item_id'           => false,                  // Optional: The ID of the specific item being recorded, e.g. a blog_id
        'secondary_item_id' => false,                  // Optional: A second ID used to further filter e.g. a comment_id
        'recorded_time'     => bp_core_current_time(), // The GMT time that this activity was recorded
        'hide_sitewide'     => false,                  // Should this be hidden on the sitewide activity stream?
        'is_spam'           => false                  // Is this activity item to be marked as spam?
      ), );

    @valuser

    Participant

    Thanks.

    Thanks.

    Have tried :
    ‘type’ => new_blog_post,
    ‘type’ => ‘new_blog_post’,
    ‘type’ => ‘activity_update’,
    ‘type’ => activity_update,

    white page for all.

    No worries.

    @henrywright

    Moderator

    What are you hooking your bp_activity_add() function to?

    Also it’s weird that the inserted post has a guid ending in /post-name, which is a different format to the rest of your posts. The guid is something you shouldn’t really need to override, it’s best to let WP handle it.

    @valuser

    Participant

    as per http://pastebin.com/Y0AE9KuS

    I’m in a (huge) way over my head here.

    i just changed the plugin as per your code and tried variations.

    All variations, so far, disabled the site.

    Can take instructions, copy and paste, research, etc and do some simple stuff.

    Also Tried this variation

    $inputs = array(
    	   'id' 			=> false,
    	   'action' 			=> 'Jon Doe posted an update',
    	   'content'			=> $apost_content,
    	   'component'                  => false, 
               'type'			=> 'activity_update',
    	   'primary_link'	        => false,
               'user_id'		        => $user_id,
    	   'item_id' 			=> false,
               'secondary_item_id'          => false,
               'recorded_time' 	        => gmdate( "Y-m-d H:i:s" ),
               'hide_sitewide' 	        => false,
    	   'is_spam'        	        => false 
        );
     
        $r = wp_parse_args( $args, $inputs );
        extract( $r );
     
        return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'component' => $component, 'type' => $type,'primary_link' => $primary_link, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide, is_spam' => $is_spam) );

    Same result.

    But I imagine there just must be a “proper” way to programatically insert an activity item.

    @henrywright

    Moderator

    Try adding this to your theme’s functions.php file:

    function valuser_add_new_post_activity( $ID, $post ) {
    
        $user = get_userdata( $post->post_author );
    
        $args = array(
            'user_id' => $post->post_author,
            'action' => $user->user_login . ' published ' . $post->post_title,
            'component' => buddypress()->blogs->id,
            'type' => 'new_blog_post',
            'item_id' => get_current_blog_id(),
            'secondary_item_id' => $post->ID,
            'recorded_time' => bp_core_current_time(),
            'hide_sitewide' => false
        );
        bp_activity_add( $args );
    }
    add_action( 'publish_post', 'valuser_add_new_post_activity', 10, 2 );

    That’s how I’d add new blog posts to the activity stream. I’ve hooked bp_activity_add() to the published_post action which will fire each time a new post is added to the database.

    Please note I haven’t tested.

    @valuser

    Participant

    Thank you

    but

    no

    posts (Title & link) are still being transferred to the blog but not recorded in activity.

    Unusual because these are posts inserted on the bp root blog and posts (& comments) from every other blog turn up in activity here

    @henrywright

    Moderator

    Humm, that’s strange. Can you try posting a new post from the WP admin area so see if it works from there?

    From http://pastebin.com/fMnJh5T0

    Clicking on the ruby brings up http://imgur.com/jE3hUzD and clicking on “Home Place” effects the transfer and so far so good the transfer is inserted as a post in the users pblog

    Can you paste the code which actually creates the post? For example, where does the following come from?

    $post_id = wp_insert_post( array(
    'post_author'	=> $user_id,
    'post_title'	=> $post_title,
    'post_content'	=> $apost_content,
    'post_category'	=> $category_ids,
    'tags_input'	=> $tags,
    'post_status'	=> 'publish'
    ), $wp_error );

    @valuser

    Participant

    Just to first reaffirm.

    normal posts via wp-admin were posted on front-end, homepage etc and appeared in activity

    “Inserted” posts were posted on front-end, homepage etc but did NOT appear in activity.

    With your function -just now- normal posts via wp-admin are posted once on front-end, homepage etc and appeared three times in activity. but inserted posts both before and after adding your function do not appear in activity

    Can you paste the code which actually creates the post?

    its via that code (in your last reply) at the end of the plugin.

    the plugin is at http://pastebin.com/s2cNJtrj

    that actually appears to “post the post”. but the post appears everywhere except in activity

    @henrywright

    Moderator

    Right, I’m with you.

    After this line in the plugin

    echo 'Shared successfully to your Home Place with tags: '.$tags;

    Can you add this?

    do_action( 'newsbaby_after_insert_post', $post_id );

    Then change my code you added to functions.php to:

    function valuser_add_new_post_activity( $post_id ) {
        $post = get_post( $post_id );
        $user = get_userdata( $post->post_author );
    
        $args = array(
            'user_id' => $post->post_author,
            'action' => $user->user_login . ' published ' . $post->post_title,
            'component' => buddypress()->blogs->id,
            'type' => 'new_blog_post',
            'item_id' => get_current_blog_id(),
            'secondary_item_id' => $post->ID,
            'recorded_time' => bp_core_current_time(),
            'hide_sitewide' => false
        );
        bp_activity_add( $args );
    }
    add_action( 'newsbaby_after_insert_post', 'valuser_add_new_post_activity' );

    That should do it 🙂

    @valuser

    Participant

    Have done that but fraid no change re “inserted” post appearing in activity.

    did two new post transfers – they arrived but not in activity.

    However a “normal” post via wp-admin came in just once onto activity.

    You really are more than kind o devote time to this

    @henrywright

    Moderator

    However a “normal” post via wp-admin came in just once onto activity.

    Yeah, that’s expected because you’ve enabled site tracking.

    did two new post transfers – they arrived but not in activity.

    This bit isn’t expected because with my code in place, an activity entry should be created each time the wp_insert_post() function in your plugin executes.

    There must be something going on elsewhere that we’re missing.

    Just out of interest, plugins_loaded seems rather early. Try changing that to wp on line 15. And I’m not sure that a negative priority is necessary? So for example, try changing line 15 to:

    add_action( 'wp', 'newsbaby_blog_addthis', 1 );

    I’m not 100% convinced that’ll solve the issue though.

    @valuser

    Participant

    the post “Why Animals Eat Psychoactive Plants” from Digg came in but its not recorded in activity !!

    @henrywright

    Moderator

    Going back to this, the following hook can’t be firing.

    do_action( 'newsbaby_after_insert_post', $post_id );

    If this fired, then the valuser_add_new_post_activity() function would execute and that would result in an activity item being created.

    @valuser

    Participant

    @henrywright

    Have considered this further at http://pastebin.com/tKkAjhhR

    If and when you have the time perhaps you may take a look.

    No worries- & thanks

    Haven’t got it to appear in activity yet.

    @henrywright

    Moderator

    Hi again @valuser

    Just read through the Pastebin text, and yeah the bookmark tool looks great. Worked perfectly! I’m still puzzled by why the activity item isn’t created when you create a post by clicking Home Place. My hunch is that valuser_add_new_post_activity() isn’t executing due to the newsbaby_after_insert_post hook not firing.

Viewing 19 replies - 1 through 19 (of 19 total)
  • The topic ‘Inserting activity content via plugin’ is closed to new replies.
Skip to toolbar