Custom-fields in Activity-content

Hi, i need add custom-fields to activity-content.

I can’t do this. In this example I show that the text “this yes” appears in activity and “get_post_meta? function not appears. This function is what I need to show my custom fields.

add_filter( 'bp_blogs_activity_new_post_content', 'record_post_activity_content' );
function record_post_activity_content(){
return get_post_meta($post->ID, 'txt_link', true).'this yes';
}

I think the problem is that i can’t get the post id. I tried with this code and it works:

return get_post_meta(577, 'txt_link', true).'This1';

I tried in many ways but I can't get the post-id to work with bp_blogs_activity_new_post_content filter

Thanks

  • Alexander
    • DEV MAN’s Mascot

    Hi there @ggsalas,

    The reason that $post->ID isn't working inside the function, is because $post doesn't exist in the scope of that function. It doesn't know what $post refers to at all.

    From what I can tell, all you need to do is include those arguments in your funciton definition. So something like this:

    add_filter( 'bp_blogs_activity_new_post_content', 'record_post_activity_content',10,3);
    function record_post_activity_content($activity_content,$post,$post_permalink){
    return get_post_meta($post->ID, 'txt_link', true).'this yes';
    }

    When adding the filter, we specify 10 for the priority, then use 3 to say our function accepts 3 arguments. I got these from where "apply_filters" is called. Then when defining the function, we just include the parameter names. This way the function will have access to the post and other info passed into the filter.

    I haven't tested this, so let me know how it goes.

    Thanks for being a WPMU DEV member. Let us know if we can be of any further assistance.
    Best regards,

  • Alexander
    • DEV MAN’s Mascot

    @ggsalas,

    How is it not working? Do you have access to the $post now? I was more addressing the issue of why you couldn't get the post ID. I wouldn't have expected for it to work exactly like that as it was for for illustration purposes. I more or less took your code and added the parameters to make them accessible.

    One thing I suspect here is that you'll actually need to prepend the original content was well. Otherwise it will be removed. Here's an example:

    function record_post_activity_content($activity_content, $post, $post_permalink ){
           $meta = get_post_meta($post->ID, 'txt_link', true).'this yes';
            return $activity_content . $meta;
    }

    Best regards

  • ggsalas
    • Site Builder, Child of Zeus

    Hi @Alexander_Rohmann,

    I think I can't have access to the $post. The code that you give me to me not work.

    recently in a chat sesion @Jack Kitterhing send to me this code:

    add_filter( 'bp_blogs_activity_new_post_content', 'record_post_activity_content', 1, 3 );
    function record_post_activity_content($activity_content, $post, $post_permalink ){
    if( $post->post_type == 'post' ) {
    $activity_content = 'txt_link';
    }
    return $activity_content;
    }

    But the only result I have is the text "txt_link", not the custom-field.

    I think must be an error somewhere.
    Thanks

  • Alexander
    • DEV MAN’s Mascot

    @ggsalas,

    Again, Jack was probably more or less helping you get access to $post

    If you look at the sample he provided, it's just assigning "txt_link" to $activity_content under the condition that the post is a of a post type of "post" Then that value is returned through the filter.

    When you say my code didn't work, Could you provide some more information? In the second code I posted, I only corrected the function, I didn't paste the filter as well again.

    Here it is all together so you can copy and paste:

    add_filter( 'bp_blogs_activity_new_post_content', 'record_post_activity_content',10,3);
    function record_post_activity_content($activity_content, $post, $post_permalink ){
           $meta = get_post_meta($post->ID, 'txt_link', true).'this yes';
            return $activity_content . $meta;
    }

    Let me know if that works.

  • ggsalas
    • Site Builder, Child of Zeus

    My mistake. In all my tries I only have used the plugin "wp-user-frontend-pro" to add posts. Today I add a post with wordpress and your code work perfectly.

    Now I go to create a support request in the forum plugin. If you can imagine where the problem is, please tell me and I send it to the plugin author.

    Thank you!

  • Alexander
    • DEV MAN’s Mascot

    @ggsalas,

    Ok that makes sense! Good to see it's working at least in that context. It's likely that the plugin you're using doesn't trigger "save_post" Wordpress action. That's the action that the activity content hooks into, which is where the filter is used.

    Thanks for being a WPMU DEV member. Let us know if we can be of any further assistance.
    Best regards,

  • Alexander
    • DEV MAN’s Mascot

    Not that I know of unless you make something completely custom. This is just the nature of BuddyPress. There are ways to insert posts without the save_post action being called. This is likely what your front end plugin is doing. So that's why it's not working.

    But from what I can tell here, what we're already working with is the only place you can filter it. "record_post_activity_content" relies on "save_post"

    If you're up for looking into it, there might be a workaround. You could experiment with the bp_blogs_record_post function (bp-blogs-functions.php line 214) Maybe with another combination of hooks, you could implement it somewhere else.

    Another idea would be to simply test a few other front end plugins:
    http://wordpress.org/plugins/wp-user-frontend/
    http://wordpress.org/plugins/front-end-editor/

    Best regards

  • ggsalas
    • Site Builder, Child of Zeus

    Hi Alexander_Rohmann,

    The developer send to me a solution that works perfect. I share:

    function wpufe_modify_bp_activity( $post_id ) {
        global $wpdb, $user_ID;
    
        //get the last inserted activity
        $activity = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}bp_activity WHERE secondary_item_id = $post_id AND user_id = $user_ID");
    
        // if activity found, update the activity with custom field
        if ( $activity ) {
            $content = $activity->content . '... Custom Field: ' . get_post_meta( $post_id, 'custom_field', true );
    
            $wpdb->update(
                $wpdb->prefix . 'bp_activity',
                array( 'content' => $content ),
                array( 'id' => $activity->id )
            );
        }
    }
    
    add_action( 'wpuf_add_post_after_insert', 'wpufe_modify_bp_activity' );

    http://wedevs.com/support/topic/custom-fields-in-budypress-activity-content/#post-5889

    Thanks again!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.