Skip to:
Content
Pages
Categories
Search
Top
Bottom

I need to get just the content string and just the activity update string


  • Matt McFarland
    Participant

    @matt-mcfarland

    I’m trying to add og tags and inorder to do it I have to grab the content of a status update and the status update title (which would be so and so posted this and that so long ago) and put them in the <head> element.

    To do that, I need to grab the info before entry.php fires, but after bp_Head();

    I’ve scowerd your codex and can’t find any hint of how to do this.

    For wordpress I’d just do something like $post->post_title() and $post_post_content()
    .. super easy!!!

    How can this be done with bp posts? You know bp stuff is terribly lacking in SEO and I also want to add microdata to everything and I dont want to do it with action hooks.

    please any BP devs here??? I’d appreciate the help.

Viewing 10 replies - 1 through 10 (of 10 total)

  • modemlooper
    Moderator

    @modemlooper

    You can only get activity info while in a loop. There isn’t a clean way to do it because bp activity is not a custom post type.


    Matt McFarland
    Participant

    @matt-mcfarland

    Can you tell me how to get the data by ID or via SQL?


    danbp
    Moderator

    @danbp

    hi @matt-mcfarland,

    i’m not a dev but consider this, to get a user ID:

    $user_id = bp_get_member_user_id();

    To answer the sql question, here’s a function that doesn’t exist in buddypress, which let you get the xprofile_group name by it’s ID For inspiration…

    function bpfr_get_xprofile_group_id_by_name( $name = '' ) {
    	global $wpdb;
    	
    	$bp = buddypress();
    	
    	if( empty( $name ) )
    		return false;
    		
    	return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_groups} WHERE name = %s", $name ) );
    }

    For the output, you have to create another function containing at least:

    
    	$user_id = bp_get_member_user_id();
    	$xprofile_group_id = bpfr_get_xprofile_group_id_by_name( 'the groupe name' );
    
    if( !class_exists( 'BP_XProfile_Group' ) )
    		return false;
    		
    	$args = array(
    		'profile_group_id'       => $xprofile_group_id,
    		'user_id'                => $user_id,
    		'fetch_fields'           => true,
    		'fetch_field_data'       => true
    	);

    May this help you !


    Matt McFarland
    Participant

    @matt-mcfarland

    Hi, and thanks for that chouf!


    @modemlooper
    why was bp made this way? seems to be bad engineering, despite it’s awesomeness. trust me when I say, it is a great web application. Just hate it’s architecture, to be blunt it’s highly limiting compared to wp. can’t grab outside loop? wth? Anyway love it though, love it.


    @danbp
    That really does help, I see how I can grab db info for the future.


    modemlooper
    Moderator

    @modemlooper

    It was coded a specific way (to have efficient db access) in the beginning and slowly over its life there have been changes to make it more WordPress in design. Using CPTs just hasn’t been dealt with yet. The main focus in the past have been making it work without WP multisite and adding theme compatibility. There have been some work on component APIs and creating admin UI for accessing the site content. The code base for BP is huge so it takes time to make those types of core changes that won’t break existing sites. Also it takes coding hours. Everyone who contributes to BP are all volunteers so, it is when these people have time to give that things like CPTs may happen.

    Trust me, CPTs for BP content types has been a major discussion in the past and will likely be in the future.


    modemlooper
    Moderator

    @modemlooper

    To add to why $post->post_title() doesn’t work is because BP is using page.php and then filtering the_content. It bypasses the WP system. This was to get it to fit into any WP theme. This tosses SEO and graph tags out the window.


    Henry
    Member

    @henrywright-1

    @matt-mcfarland
    cc @modemlooper

    I recently had to find activity item info outside the loop and came across a nifty little function bp_activity_get_specific() which can be used like so

    $activity = bp_activity_get_specific( array( 'activity_ids' => $activity_id ) );
    $activity = $activity['activities'][0];

    You can then do stuff like echo $activity->action; which would display the text for that particular activity.

    One caveat is you will need to know the ID(s) of the activity item(s) you’re interested in.


    modemlooper
    Moderator

    @modemlooper

    I think issue is needing the id in the <head>


    Matt McFarland
    Participant

    @matt-mcfarland

    @modemlooper
    cc @chouf1
    cc @henrywright-1

    I have figured out how to get this in the header, without hacking bp or running a custom sql query.

    So basically, what I did was create a function that loads the data by using global $bp, and a very powerful $bp function known as bp_activity_get

    Now that I’ve dissected your bp-activity-functions.php file the sky is the limit.

    Here’s a very small sample of this in action (mine also grabs rtmedia etc, now finally facebook will grab any images uploaded and all the content, finally)

    First, you want a function that is going to run in the <head> tag like so:

    add_action( 'wp_head', 'insert_fb_in_head', 5 );

    So it’s going to run in wp_head (yay) and it will be called ‘insert_fb_in_head’, priority lvl 5.

    You may also want to load bp_head() before you run wp_head() in your header.php file. I didn’t test it the other way around, just know this WORKS:

    So here comes the magic:

    
    function insert_fb_in_head() {
    
    	global $post;
    	global $bp;
    	
    	if (bp_is_current_component('activity') ) {
    		$title = "HVAC Hacks";
    		$description = "Here's Why You Choose a Professional";
    		$keywords = "hvac, hvac hacks, hvac hacks and other screw ups, hvac memes, hvac meme";
    		$domain = "HVAC-Hacks.com";
    		$image = $default_image;
    	
    		$activity_id = $bp->current_action;
    		if ($activity_id)  {
    			$get_act = bp_activity_get(array('in'=>$activity_id));
    				$activities = ($get_act['activities']);
    				foreach ($activities as $activity)  {
    					$description = strip_tags($activity->action);
    					$image = html_get_attr($activity->content, "img","src");
    				}
    			//HTML META
    			echo '<meta name="description" content="'.$description.'">';
    			echo '<meta name="keywords" content="'.$keywords.'">';
    			//FACEBOOK
    			echo '<meta property="og:title" content="' . $title . '"/>';
    			echo '<meta property="og:type" content="article"/>';
    			echo '<meta property="og:url" content="http://www.hvac-hacks.com'.$_SERVER['REQUEST_URI'].'"/>';
    			echo '<meta property="og:site_name" content="'.$title.'"/>';	
    			echo '<meta property="og:description" content="'.$description.'"/>';
    			echo '<meta property="og:image" content="' . $image  . '"/>';
    			//TWITTER
    			echo '	<meta name="twitter:card" content="photo">
    					<meta name="twitter:site" content="@hvachacks">
    					<meta name="twitter:creator" content="@hvachacks">
    					<meta name="twitter:title" content="'.$title.'">
    					<meta name="twitter:domain" content="www.hvac-hacks.com">
    					<meta name="twitter:image:src" content="'.$image.'">';
    			return;
    			}		
    	}
    

    Oh and here’s the proof: https://developers.facebook.com/tools/debug/og/object?q=http%3A%2F%2Fwww.hvac-hacks.com%2Fmembers%2Fgene-warren-391%2Factivity%2F4293%2F

    That’s facebook’s debugger tool telling me it grabbed all the necessary info…. yay… win..


    Henry
    Member

    @henrywright-1

    @matt-mcfarland glad to see you got this resolved!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘I need to get just the content string and just the activity update string’ is closed to new replies.
Skip to toolbar