Skip to:

Add Custom Setting to Buddypress Settings

  • P


    I want to modify the Buddypress core files to add a feature which I need to submit to the trac to be used in future Buddypress releases. This feature lets the user set the the throttling time (in seconds) under Buddypress Settings (wp-admin/admin.php?page=bp-settings), a number which would then be used for preventing flooding on the activity page.

    I added the new throttling setting field next to the other fields but when I enter a number, it just doesn’t save it. However, it does save an option under the wp_options table with my option key but with a value of 0.

    Here’s what I added so far:

    in bp-core-admin.php:

    add_settings_field( 'bp-enable-activity-throttling', __( 'Set Activity Throttling Time', 'buddypress' ), 'bp_admin_setting_callback_activity_throttle', 'buddypress', 'bp_activity' );
    register_setting( 'buddypress', 'bp-enable-activity-throttling', 'bp_sanitize_activity_throttling' );

    in bp-core-options.php:

    function bp_sanitize_activity_throttling( $default = 0 ) {
    	return (int) apply_filters( 'bp_sanitize_activity_throttling', (int) bp_get_option( 'bp-enable-activity-throttling', $default ) );

    in bp-core-settings.php:

    function bp_admin_setting_callback_activity_throttle() {
    <input id="bp-enable-activity-throttling" name="bp-enable-activity-throttling" type="text" value="<?php bp_form_option( 'bp-enable-activity-throttling', '' ); ?>" />

    At the bottom of the bp-core-settings.php file, there’s a function called bp_core_admin_settings_save which gets all fields from the Settings page and saves them. In this function I debugged what the $_POST array was receiving from the Settings page and my setting was there with its value! So the code should have saved my new setting with its value in the wp_options table.

    I got frustrated and tried to debug some more, to no avail.

    Does anyone know how to add my setting to the Buddypress core files?

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

  • modemlooper


    You shouldn’t edit those files. You can add settings with a plugin. If you want that as a core feature then post on then submit a patch of working code.

    Reason you ought to write as a plugin for own use is while you should offer the code through trac for consideration for adding to core there is no guarantee it will be accepted so if it’s that necessary for you safer to keep running as a plugin until such time as it’s merged into core.



    It would be a good plugin, from what I can see your method involves throttling for the activity stream correct? Not really projected towards friend request correct? I personally would like to see this feature towards both so hopefully it will come to fruition. Maybe you can create two plugins off this idea?



    The problem is I need to modify the core files and add a plugin at the same time so a user can change the Throttling settings in his WordPress backend.

    I really tried to simple write a plugin and skip the core files modifications, but it’s just not working out. Maybe you can help out here, I will post the 3 core file functions which I modified as well as the plugin code which I wrote. If you can figure out a way for me to not modify the core files and still get Throttling to work, share your code.

    Modified files:

    1- bp-activity\bp-activity-functions.php

    function bp_activity_post_update( $args = '' ) {
    	global $bp;
    	$defaults = array(
    		'content' => false,
    		'user_id' => bp_loggedin_user_id()
    	$r = wp_parse_args( $args, $defaults );
    	extract( $r, EXTR_SKIP );
    	if ( empty( $content ) || !strlen( trim( $content ) ) )
    		return false;
    	if ( bp_is_user_inactive( $user_id ) )
    		return false;
    	<strong>$floodTest = bp_core_check_for_flood ($user_id);
    	if (!$floodTest)
    		return "flood";</strong>
    	// Record this on the user's profile
    	$from_user_link   = bp_core_get_userlink( $user_id );
    	$activity_action  = sprintf( __( '%s posted an update', 'buddypress' ), $from_user_link );
    	$activity_content = $content;
    	$primary_link     = bp_core_get_userlink( $user_id, false, true );
    	// Now write the values
    	$activity_id = bp_activity_add( array(
    		'user_id'      => $user_id,
    		'action'       => apply_filters( 'bp_activity_new_update_action', $activity_action ),
    		'content'      => apply_filters( 'bp_activity_new_update_content', $activity_content ),
    		'primary_link' => apply_filters( 'bp_activity_new_update_primary_link', $primary_link ),
    		'component'    => $bp->activity->id,
    		'type'         => 'activity_update'
    	) );
    	$activity_content = apply_filters( 'bp_activity_latest_update_content', $content );
    	// Add this update to the "latest update" usermeta so it can be fetched anywhere.
    	bp_update_user_meta( bp_loggedin_user_id(), 'bp_latest_update', array( 'id' => $activity_id, 'content' => $content ) );
    	do_action( 'bp_activity_posted_update', $content, $user_id, $activity_id );
    	return $activity_id;

    2- bp-core\bp-core-moderation.php

    function bp_core_check_for_flood( $user_id = 0 )
    	<strong>// Option disabled. No flood checks.
    	if ( !$throttle_time = bp_get_option( 'bt_activity_time' ) )
    		return false;
    	// Bail if no user ID passed
    	if ( !$user_id )
    		return false;
    	$last_posted = get_user_meta( $user_id, '_bp_last_posted', true );
    	if ( !$last_posted )
    		$last_posted = time();
    		add_user_meta( $user_id, '_bp_last_posted', $last_posted);
    		return true;
    		if ( ( time() < ( $last_posted + $throttle_time ) ) && !current_user_can( 'throttle' ) )
    			return false;
    			return true;

    3- bp-themes\bp-default\_inc\ajax.php

    function bp_dtheme_post_update() {
    	// Bail if not a POST action
    	if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
    	// Check the nonce
    	check_admin_referer( 'post_update', '_wpnonce_post_update' );
    	if ( ! is_user_logged_in() )
    		exit( '-1' );
    	if ( empty( $_POST['content'] ) )
    		exit( '-1<div id="message" class="error"><p>' . __( 'Please enter some content to post.', 'buddypress' ) . '</p></div>' );
    	$activity_id = 0;
    	if ( empty( $_POST['object'] ) && bp_is_active( 'activity' ) ) {
    		$activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
    	} elseif ( $_POST['object'] == 'groups' ) {
    		if ( ! empty( $_POST['item_id'] ) && bp_is_active( 'groups' ) )
    			$activity_id = groups_post_update( array( 'content' => $_POST['content'], 'group_id' => $_POST['item_id'] ) );
    	} else {
    		$activity_id = apply_filters( 'bp_activity_custom_update', $_POST['object'], $_POST['item_id'], $_POST['content'] );
    	if ($activity_id == "flood")
    		$bt_activity_throttle_time = bp_get_option ('bt_activity_time');
    		$bt_activity_message = bp_get_option( "bt_activity_message" );
    		$msg = ( $bt_activity_message ) ? $bt_activity_message : "You have to wait to post again";
    		exit( '-1<div id="message" class="error"><p>' . __( $msg, 'buddypress' ) . '</p></div>' );
    	if ( empty( $activity_id ) )
    		exit( '-1<div id="message" class="error"><p>' . __( 'There was a problem posting your update, please try again.', 'buddypress' ) . '</p></div>' );
    	if ( bp_has_activities ( 'include=' . $activity_id ) ) {
    		while ( bp_activities() ) {
    			locate_template( array( 'activity/entry.php' ), true );

    4- The Buddypress Throttling Plugin Code

    add_action( 'admin_menu', 'plugin_menu' );
    function plugin_menu() {
    	// add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);
    	add_options_page( 'Buddypress Throttling | Settings', 'Buddypress Throttling', 'manage_options', 'buddypress_throttling', 'buddypress_throttling_options' );
    function buddypress_throttling_options() {
        //must check that the user has the required capability 
        if (!current_user_can('manage_options')) {
         	WP_die( __('You do not have sufficient permissions to access this page.') );
        // variables for the field and option names 
        $hidden_field_name = 'submit_hidden';
        // See if the user has posted us some information
        // If they did, this hidden field will be set to 'Y'
        if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) 
    		// Activity
    		$bp_activity_time_val =  ( intval($_POST["bt_activity_time"]) <= 0 ) ? 0 : $_POST ["bt_activity_time"];
    		update_option( "bt_activity_time", $bp_activity_time_val );
    		$bp_activity_message_val =  ( isset($_POST["bt_activity_message"]) && $_POST["bt_activity_message"] != "" ) ? $_POST["bt_activity_message"] : "Please wait before posting again";
    		update_option( "bt_activity_message", $bp_activity_message_val );
    		<div class="updated"><p><strong><?php _e('Settings saved.', 'menu-test' ); ?></strong></p></div>
    	// Activity read values
    	$bt_activity_time = get_option( "bt_activity_time" );
    	$bt_activity_time = (intval($bt_activity_time) <= 0) ? 0 : $bt_activity_time;
    	$bt_activity_message = get_option( "bt_activity_message" );
    	$bt_activity_message = ($bt_activity_message) ? $bt_activity_message : "Please wait before posting again";
    	echo '<div class="wrap">';
    		echo "<h2>" . __( 'Buddypress Throttling Settings', 'menu-test' ) . "</h2>";
    		<form name="form1" method="post" action="">
    			<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">	
    			<div class="bt-plugin">
    					.bt-plugin span { display: inline-block; width: 120px; margin-left: 20px }
    					.bt-plugin textarea {width: 400px}
    				<h3><?php _e("On Activity Page: ", 'menu-test' ); ?></h3>
    				<p><span>Throttling Time:</span><input type="text" name="bt_activity_time" value="<?php echo $bt_activity_time; ?>" size="20"> (in seconds)</p>
    				<p><span>Message:</span><textarea name="bt_activity_message" rows="3"><?php echo $bt_activity_message; ?></textarea></p>
    			<p class="submit">
    			<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />

    Test it out locally, just replace the specified functions in these files with the functions I wrote, add the plugin code in a php file and save it under your Plugins directory (and activate it in the backend). The Plugin code will allow you to set the number of seconds for throttling as well as the message the user will see when he’s flooding.

    Put this puzzle pieces together and you got Flooding control (for the activity page for now), which can easily be done for friend requests @bphelp).





    @bphelp Premium, not my cup of tea. I’m tackling the topic so throttling can be a feature in Buddypress itself (core feature), not simply as a plugin.



    I was just informing you of the release of the plugin which works perfectly in my test environment. If you can get your code working and integrated into the core great, if not there is a viable option already.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add Custom Setting to Buddypress Settings’ is closed to new replies.
Skip to toolbar