Last active
December 18, 2015 20:29
-
-
Save imath/5840625 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for all your time!