public
Last active

This is a skeleton to create a BP My Home (version 2.0) widget. You can use it in your plugin or in the functions.php file of your active theme.

  • Download Gist
bpmh-custom.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
<?php
/**
* 1- Extending WP_Widget
*
* Make sure to prefix your class with BPMH_
* and to add a classname prefixed by bpmh-
*
*/
class BPMH_Widget_Custom extends WP_Widget {
 
function __construct() {
$widget_ops = array('classname' => 'bpmh-widget_custom', 'description' => __( 'Custom BPMH widget') );
parent::__construct( false, _x( '(BPMH) Custom', 'widget name', 'bp-my-home' ), $widget_ops );
/**
* Hooking bpmh_actions to save user settings
*/
add_action( 'bpmh_actions', array( $this, 'save_user_settings' ) );
}
function widget( $args, $instance ) {
extract( $args );
 
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Example' ) : $instance['title'], $instance, $this->id_base);
 
echo $before_widget;
/**
* 2- Configure link
*
* if you need your widget to have a configuration area,
* this is the way you need to build your configure link
*/
$widget_hash = trailingslashit( base64_encode( get_class( $this ) ) );
$config_link = trailingslashit( bp_my_home_get_user_settings_link() . 'widget' ) . $widget_hash;
if ( $title )
echo $before_title .'<span class="configure" ><a href="'.$config_link.'">'.__('Configure', 'bp-my-home') .'</a></span>' . $title . $after_title;
if( !is_admin() && !is_network_admin() )
$this->user_content( $instance );
else
echo '<p>' . __( 'Content is only available in user&#39;s page', 'bp-my-home' ) . '</p>';
echo $after_widget;
}
 
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
 
return $instance;
}
/**
* Let Admin customize the title of the widget
*
* @param array $instance
* @return string html of the form
*/
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = esc_attr( $instance['title'] );
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<?php
}
/**
* We could have done this in the widget() function
* but as this is never loaded in widget administration
* it avoids to run some un-necessary function in this part
* to only do it in user's home page
*
* @param array $instance
* @uses bp_loggedin_user_id() to get current user's id
* @uses get_user_meta() to get previously stored user meta
* @uses esc_html() to sanitize output
* @return string html private content for the user
*/
function user_content( $instance = array() ) {
$user_id = bp_loggedin_user_id();
$bpmh_example_text = get_user_meta( $user_id, 'bpmh_custom_settings', true );
if( !empty( $bpmh_example_text ) )
echo '<p>'. esc_html( $bpmh_example_text ).'</p>';
}
/**
* 3- Displays the user settings form
*
* On the click of the configure link, BP My Home will call this function
* to display the user settings form for your widget.
*
* @param array $args
* @param array $instance
* @uses bp_loggedin_user_id() to get current user's id
* @uses get_user_meta() to get previously stored user meta
* @uses wp_nonce_field() for security check
* @return string html form for user settings
*/
function user_settings( $args, $instance ) {
extract( $args );
$user_id = bp_loggedin_user_id();
$bpmh_custom_text = get_user_meta( $user_id, 'bpmh_custom_settings', true );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Example' ) : $instance['title'], $instance, $this->id_base);
echo $before_widget;
 
if ( $title)
echo $before_title . sprintf( __('Settings for %s', 'bp-my-home' ), $title ) . $after_title;
?>
<form action="" method="post" class="standard-form">
<label for="custom-id"><?php _e( 'Insert a random text', 'bp-my-home' )?></label>
<input type="text" name="_bpmh_custom[input]" value="<?php echo esc_attr( $bpmh_custom_text );?>" id="custom-id"/>
<?php wp_nonce_field( 'bpmh_custom_options', '_wpnonce_widget_options_' . $this->id_base ); ?>
<p><input type="submit" name="_bpmh_custom[save]" value="Save"></p>
</form>
<?php
echo $after_widget;
}
/**
* 4- Saves the user settings
*
* This function is hooked to bpmh_actions
* it first checks we're on the widget's settings area
* then stores the user's data to finally safely redirect the user to it
*
* @uses bp_my_home_current_is_widget_settings() to check we're on a single widget settings area
* @uses bp_action_variable() to check it's the settings area of the current widget
* @uses check_admin_referer() for security check
* @uses bp_loggedin_user_id() to get current user's id
* @uses get_user_meta() to get previously stored user meta
* @uses update_user_meta() to save the user meta in db
* @uses delete_user_meta() to eventually delete user's data
* @uses bp_my_home_get_user_settings_link() to build the redirection link
* @uses bp_core_add_message() to inform the user data were processed
* @uses bp_core_redirect() to redirect user to the widget settings page and avoid refreshing the posted vars
* @return string html form for user settings
*/
function save_user_settings() {
if( bp_my_home_current_is_widget_settings() && base64_decode( bp_action_variable( 1 ) ) == get_class( $this ) ) {
if( empty( $_POST['_bpmh_custom']['save'] ) )
return;
check_admin_referer( 'bpmh_custom_options', '_wpnonce_widget_options_' . $this->id_base );
$user_id = bp_loggedin_user_id();
 
if( !empty( $_POST['_bpmh_custom']['input'] ) )
update_user_meta( $user_id, 'bpmh_custom_settings', sanitize_text_field( $_POST['_bpmh_custom']['input'] ) );
else
delete_user_meta( $user_id, 'bpmh_custom_settings' );
// let's finally redirect to avoid double posting
$widget_hash = trailingslashit( base64_encode( get_class( $this ) ) );
$redirect = trailingslashit( bp_my_home_get_user_settings_link() . 'widget' ) . $widget_hash;
bp_core_add_message( __( 'Widget settings saved successfully', 'bp-my-home' ) );
bp_core_redirect( $redirect );
}
}
 
}
 
/**
* 5- Finally let's register the widget for BP My Home
*
* @since BP My Home 2.0
*/
function bpmh_custom_register_widget() {
/**
* 5-2 then we hook to bpmh_widgets_init to make sure BP My Home is up and running!
*/
add_action( 'bpmh_widgets_init', create_function('', 'return register_widget("BPMH_Widget_Custom");' ) );
}
 
/**
* 5-1 We first hook to widgets_init
*/
add_action( 'widgets_init', 'bpmh_custom_register_widget' );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.