Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to Modify Content Before Saving if to DB


  • vanleurth
    Participant

    @vanleurth

    Hello BuddyCoders,

    I’m trying to add the location of the post at the end of the activity content in a programatically way.

    I’m not sure why this is not working.

    Please review and let me know if you have a better code to accomplish this.

    // Check the activity content
    function mmAddLocationCategory( $args ) 
    {
    
        echo "here" . $args['type'];
    	exit();
    	
    	//get the category from POST
    	$category = isset($_POST['bpp_ex_custom_dropdown']) ? $_POST['bpp_ex_custom_dropdown']:null;
    	
    	// Get the location from the user's profile	
    	$user_id = bp_loggedin_user_id();
        $bp_location = xprofile_get_field_data('Location', $user_id);
    	
    	
    	// Add the location to all of the user's activity		
    	$mmLocation = "<a href=http://dev.areyoupop.com/activity/?s=%23" . $bp_location . ">#" . $bp_location . "</a>";
    	
    	// Add category to the content
    	if(!is_null($category))
    	{
    		//update the category to activity meta
    		$content = $content . "<br />" . $category . " " . $mmLocation . $args['type'];
    		
    	}
    
    return $content;
    
    }
    add_action('bp_activity_post_update', 'mmAddLocationCategory', 10, 2 );
Viewing 6 replies - 1 through 6 (of 6 total)

  • Henry Wright
    Moderator

    @henrywright

    ~5th line down you have exit();. That will stop execution of the function at that point.

    Ref: http://php.net/manual/en/function.exit.php


    vanleurth
    Participant

    @vanleurth

    Henry,

    Thank you for your prompt reply. I forgot to remove the testing code I was using.
    This is the actual code;

    // Check the activity content
    function mmAddLocationCategory( $args ) 
    {
    
        	//get the category from POST
    	$category = isset($_POST['bpp_ex_custom_dropdown']) ? $_POST['bpp_ex_custom_dropdown']:null;
    	
    	// Get the location from the user's profile	
    	$user_id = bp_loggedin_user_id();
        $bp_location = xprofile_get_field_data('Location', $user_id);
    	
    	
    	// Add the location to all of the user's activity		
    	$mmLocation = "<a href=http://dev.areyoupop.com/activity/?s=%23" . $bp_location . ">#" . $bp_location . "</a>";
    	
    	// Add category to the content
    	if(!is_null($category))
    	{
    		//update the category to activity meta
    		$content = $content . "<br />" . $category . " " . $mmLocation . $args['type'];
    		
    	}
    
    return $content;
    
    }
    add_action('bp_activity_post_update', 'mmAddLocationCategory', 10, 2 );

    Henry Wright
    Moderator

    @henrywright

    Is bp_activity_post_update an actual hook? I can’t seem to find it in BP.


    vanleurth
    Participant

    @vanleurth

    Thank you for the observation.
    I have updated the code and ran it and still doesn’t add the text.

    I’m thinking it might be the return line or perhaps I need to use a different function.

    // Check the activity content
    function mmAddLocationCategory( $args ) 
    {
    
        	//get the category from POST
    	$category = isset($_POST['bpp_ex_custom_dropdown']) ? $_POST['bpp_ex_custom_dropdown']:null;
    	
    	// Get the location from the user's profile	
    	$user_id = bp_loggedin_user_id();
        $bp_location = xprofile_get_field_data('Location', $user_id);
    	
    	
    	// Add the location to all of the user's activity		
    	$mmLocation = "<a href=http://dev.areyoupop.com/activity/?s=%23" . $bp_location . ">#" . $bp_location . "</a>";
    	
    	// Add category to the content
    	if(!is_null($category))
    	{
    		//update the category to activity meta
    		$content = $content . "<br />" . $category . " " . $mmLocation . $args['type'];
    		
    	}
    
    return $content;
    
    }
    add_action('bp_activity_posted_update', 'mmAddLocationCategory', 10, 2 );

    shanebp
    Moderator

    @shanebp

    You’re using that hook incorrectly.
    What do you think $args represents in your function?

    Here is the hook, from bp-activity\bp-activity-functions.php
    do_action( 'bp_activity_posted_update', $r['content'], $r['user_id'], $activity_id );
    Notice that it passes 3 separate arguments.
    And $content is not one of them.
    And there is no scope for a return.
    And that hook runs after the activity has been created, so even if your function was less malformed, it wouldn’t work as you expect.

    The correct hook is called earlier in the same function as the hook you’re using.
    $add_content = apply_filters( 'bp_activity_new_update_content', $activity_content );
    Adjust $activity_content and return it.


    vanleurth
    Participant

    @vanleurth

    Thank you for the help. Here is my code updated and working properly.

    // Check the activity content
    function mmAddLocationCategory($activity_content) 
    {
    
        //get the category from POST
    	$category = isset($_POST['bpp_ex_custom_dropdown']) ? $_POST['bpp_ex_custom_dropdown']:null;
    	
    	// Get the location from the user's profile	
    	$bp_location = xprofile_get_field_data('Location', bp_loggedin_user_id());
    	
    	
    	// Add category to the content
    	if(!is_null($category))
    	{
    		//update the category to activity meta
    		$activity_content = $activity_content . "<br>#" . $category . " #" . $bp_location;
    		
    	}
    	
    	return $activity_content;
    
    }
    add_filter('bp_activity_new_update_content', 'mmAddLocationCategory', 10, 2 );
    

    The only problem I have to solve now is how to insert a <br> in the content. It seems the filter strips all break html code. Instead of getting;

    content 
    #singing #city

    I get;
    content #singing #city

    The <br> that is added to the string in the code, gets removed for some reason.

    Thank you for your help

    V.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to Modify Content Before Saving if to DB’ is closed to new replies.
Skip to toolbar