Skip to:
Content
Pages
Categories
Search
Top
Bottom

Trouble with custom activity function


  • synfrag
    Participant

    @synfrag

    Hello,

    I’m trying to modify the activity page for my users to show their partner’s activity. I have the working functionality but in converting a query into a function to reuse, it no longer works.

    Working:

    
    	global $bp, $wpdb;
    	$myid = $bp->loggedin_user->id;
    	$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
    	$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
    	$partner =  '&user_id=' . $partnerid;
    	
    	if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $partner ) ) :

    Not Working (Undefined Variable Error on the activity loop)

    Theme functions file:

    function sw_partner_id() {
    	
    	global $bp, $wpdb;
    	$myid = $bp->loggedin_user->id;
    	$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
    	$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
    	$partner =  '&user_id=' . $partnerid;
    	
    	return $partner;
    	
    }
    add_action('sw_partner_activity', 'sw_partner_id'); 

    Modified Loop Template:

    do_action( 'sw_partner_activity' );
    	
     if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $partner ) ) : ?>

    I’m sure I’m missing something here. I’ve tried firing the action in a number of default hooks as well as just calling the function without adding an action. I’ve tried passing $user_id to the function. Really at a loss for why the $partner variable isn’t getting passed from the function to the loop.

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

  • Henry Wright
    Moderator

    @henrywright

    sw_partner_activity isn’t a BuddyPress hook. Are you using a plugin?


    synfrag
    Participant

    @synfrag

    No, it’s a custom hook. I’ve tried using existing hooks as well. If I change the return to echo $partner it’s showing the result of the query, it just wont pass the variable to the activity loop here

    if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $partner ) ) :


    synfrag
    Participant

    @synfrag

    My mistake on the first post, I had some things mixed up. Here is the full action & function.

     // Get partner ID
     function sw_partner_activity() {
    	 do_action('sw_partner_activity');
     }
     
     
    function sw_partner_id() {
    	
    	global $bp, $wpdb;
    	$myid = $bp->loggedin_user->id;
    	$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
    	$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
    	$partner =  '&user_id=' . $partnerid;
    	
    	return $partner;
    	
    }
    add_action('sw_partner_activity', 'sw_partner_id'); 

    And the proper call:
    <?php sw_partner_activity();?>

    And the error (with echo of query results):
    Error


    Henry Wright
    Moderator

    @henrywright

    I’m wondering why you’re writing a custom query to get user meta? That’s what the get_user_meta() function is for.

    Ref: https://codex.wordpress.org/Function_Reference/get_user_meta


    synfrag
    Participant

    @synfrag

    I actually didn’t know about that, I’m fairly new to WordPress. I’ll certainly update that and the companion queries that go with it once I can get this function sorted out.


    Henry Wright
    Moderator

    @henrywright

    Ah OK, cool. In that case you may also be interested to hear about bp_parse_args(). I think it’s a better way of doing what you want. This approach will also handle the loop pagination for you. For example:

    function my_func( $ret ) {
        // Your custom stuff here. 
        return $ret;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'my_func' );

    See the Using bp_parse_args() to filter BuddyPress template loops article for a complete guide.


    synfrag
    Participant

    @synfrag

    That works to an extent but it filters everything on the page and what I need is essentially two user’s activity streams on the same page. Any advice on how to make that work? It sounds like someone was trying to do something very similar in this post but it was years ago


    Henry Wright
    Moderator

    @henrywright

    You could use a control statement inside my_func()? For ex:

    if ( 'something' === 'something' ) {
        // Do something better than this...
        $ret['blah'] = 'blah';
    }
    

    The exact condition you use will depend on what you’re trying to do.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Trouble with custom activity function’ is closed to new replies.
Skip to toolbar