Created
September 30, 2014 11:52
-
-
Save imath/2e91ac77f6638dcd3cc8 to your computer and use it in GitHub Desktop.
Example of actions and filters to use to customize the behavior of WP Idea Stream
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 | |
/** | |
* WP Idea Stream Custom. | |
* | |
* Place here the function to customize Version 2.0+ of WP Idea Stream | |
*/ | |
// Exit if accessed directly | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
/** Restore Some 1.2 features *************************************************/ | |
/** | |
* Neutralize Built-in ratings system | |
* | |
* In 1.2, it was possible to disable the built-in rating system | |
* deactivating an option. Since 2.0.0, you'll need to user this filter | |
* | |
* Uncomment line 21 to apply | |
*/ | |
//add_filter( 'wp_idea_stream_is_rating_disabled', '__return_true' ); | |
/** | |
* Add links to share by email or twitter | |
* | |
* In 1.2, it was possible generate 'sharing links' to share an idea | |
* by email or on twitter, this is no more the case since 2.0.0. But | |
* you can use an action to do it. | |
* | |
* The plugin is enqueueing Dashicons style on IdeaStream's territory | |
* | |
* Uncomment line 70 to apply | |
*/ | |
function wpis_custom_sharing_services() { | |
// Only show sharing links on single templates | |
if ( ! wp_idea_stream_is_single_idea() ) { | |
return; | |
} | |
$share = array(); | |
/** Twitter *******************************************************************/ | |
// Change with your twitter account | |
$twitter_account = 'imath'; | |
$twitter_args = array( | |
'original_referer' => urlencode( wp_idea_stream_ideas_get_permalink() ), | |
'source' => 'tweetbutton', | |
'text' => urlencode( wp_idea_stream_ideas_get_title() ), | |
'url' => urlencode( wp_idea_stream_ideas_get_permalink() ), | |
'via' => $twitter_account, | |
); | |
$tweet_url = esc_url( add_query_arg( $twitter_args, 'https://twitter.com/intent/tweet' ), null, 'display' ); | |
$share['twitter'] = '<a href="' . $tweet_url . '" title="Share on Twitter" target="_blank" style="text-decoration:none"><span class="dashicons dashicons-twitter"></span> Share on Twitter</a>'; | |
/** Email *******************************************************************/ | |
$email_args = array( | |
'subject' => rawurlencode( wp_idea_stream_ideas_get_title() ), | |
'body' => wp_idea_stream_ideas_get_permalink(), | |
); | |
$email_url = esc_url( add_query_arg( $email_args, 'mailto:' ), null, 'display' ); | |
$share['email'] = '<a href="' . $email_url . '" title="Share by Email" style="text-decoration:none"><span class="dashicons dashicons-email-alt"></span> Share by Email</a>'; | |
echo join( ' ', $share ); | |
} | |
//add_action( 'wp_idea_stream_after_idea_footer', 'wpis_custom_sharing_services' ); | |
/** Editing ideas *************************************************************/ | |
/** | |
* Allow users to edit their idea at any time | |
* | |
* 2.0.0 is introducing idea edit in front end, but as it's a plugin | |
* about sharing ideas, there is mechanism that only leaves this possibility | |
* for 5 minutes and some other conditions. If you wish to allow users to edit | |
* their ideas at anytime, use this filter | |
* | |
* Uncomment line 84 to apply | |
*/ | |
//add_filter( 'wp_idea_stream_ideas_pre_can_edit', '__return_true' ); | |
/** | |
* Allow group members to edit all group ideas | |
* | |
* Uncomment line 104 & 113 to apply | |
*/ | |
function wpis_custom_allow_all_group_members_to_edit_all_group_ideas( $caps = array(), $cap = '', $user_id = 0, $args = array(), $group = null ) { | |
if ( empty( $group->id ) ) { | |
return $caps; | |
} | |
if ( $cap == 'edit_others_ideas' ) { | |
if ( groups_is_user_member( $user_id, $group->id ) ) { | |
$caps = array( 'exist' ); | |
} | |
} | |
return $caps; | |
} | |
//add_filter( 'wp_idea_stream_buddypress_group_map_meta_caps', 'wpis_custom_allow_all_group_members_to_edit_all_group_ideas', 10, 5 ); | |
function wpis_custom_allow_all_group_members_to_edit_self_ideas( $can = false ) { | |
if ( bp_is_group() ) { | |
$can = true; | |
} | |
return $can; | |
} | |
//add_filter( 'wp_idea_stream_ideas_pre_can_edit', 'wpis_custom_allow_all_group_members_to_edit_self_ideas', 10, 1 ); | |
/** Custom fields *************************************************************/ | |
/** | |
* Register new idea metas | |
* | |
* Different arguments (admin/form/single) are callback | |
* functions to display the meta in the corresponding | |
* contexts. | |
* | |
* Uncomment line 145 to apply | |
*/ | |
function wpis_custom_register_metas() { | |
// A very simple way (for text field only) | |
wp_idea_stream_ideas_register_meta( 'simple_text', array( 'label' => 'Simple text' ) ); | |
// More control on regular text field | |
wp_idea_stream_ideas_register_meta( 'text_field', array( | |
'admin' => 'wpis_custom_text_field_admin_display', | |
'form' => 'wpis_custom_text_field_form_display', | |
'single' => 'wpis_custom_text_field_single_display', | |
) ); | |
/** | |
* Checkboxes | |
* | |
* If the form callback is not set, the admin one will | |
* be used in admin and form context | |
*/ | |
wp_idea_stream_ideas_register_meta( 'checkboxes', array( | |
'admin' => 'wpis_custom_checkboxes_admin_display', | |
'single' => 'wpis_custom_checkboxes_single_display', | |
) ); | |
} | |
//add_action( 'wp_idea_stream_init', 'wpis_custom_register_metas' ); | |
/** | |
* Regular text field admin callback | |
*/ | |
function wpis_custom_text_field_admin_display( $display_meta = '' , $meta_object = null, $context = '' ) { | |
if ( empty( $meta_object->field_name ) ) { | |
return; | |
} | |
?> | |
<p> | |
<strong class="label">Text Field</strong> | |
<input type="text" name="<?php echo esc_attr( $meta_object->field_name );?>" value="<?php echo esc_attr( $meta_object->field_value );?>"/> | |
</p> | |
<?php | |
} | |
/** | |
* Regular text field form callback | |
*/ | |
function wpis_custom_text_field_form_display( $display_meta = '' , $meta_object = null, $context = '' ) { | |
if ( empty( $meta_object->field_name ) ) { | |
return; | |
} | |
?> | |
<p> | |
<label for="wpis_text_field">Text Field</label> | |
<input type="text" id="wpis_text_field" name="<?php echo esc_attr( $meta_object->field_name );?>" value="<?php echo esc_attr( $meta_object->field_value );?>"/> | |
</p> | |
<?php | |
} | |
/** | |
* Regular text field single callback | |
*/ | |
function wpis_custom_text_field_single_display( $display_meta = '' , $meta_object = null, $context = '' ) { | |
if ( empty( $meta_object->field_value ) ) { | |
return; | |
} | |
?> | |
<p> | |
<strong>Text Field</strong> : <?php echo esc_html( $meta_object->field_value );?> | |
</p> | |
<?php | |
} | |
/** | |
* Checkboxes admin/form callback | |
*/ | |
function wpis_custom_checkboxes_admin_display( $display_meta = '' , $meta_object = null, $context = '' ) { | |
if ( empty( $meta_object->field_name ) ) { | |
return; | |
} | |
// In case of checkboxes, use an array as more than 1 choice is possible | |
$field_name = $meta_object->field_name . '[]'; | |
$field_value = array(); | |
if ( ! empty( $meta_object->field_value ) ) { | |
$field_value = $meta_object->field_value; | |
} | |
?> | |
<div class="field-group"> | |
<div class="checkbox"> | |
<label for="cb_option1"> | |
<input type="checkbox" id="cb_option1" name="<?php echo esc_attr( $field_name );?>" value="Option 1" <?php checked( in_array( 'Option 1', $field_value ) )?>> | |
Option 1 | |
</input> | |
</label> | |
</div> | |
<div class="checkbox"> | |
<label for="cb_option2"> | |
<input type="checkbox" id="cb_option2" name="<?php echo esc_attr( $field_name );?>" value="Option 2" <?php checked( in_array( 'Option 2', $field_value ) )?>> | |
Option 2 | |
</input> | |
</label> | |
</div> | |
</div> | |
<?php | |
} | |
/** | |
* Checkboxes single callback | |
*/ | |
function wpis_custom_checkboxes_single_display( $display_meta = '' , $meta_object = null, $context = '' ) { | |
if ( empty( $meta_object->field_value ) ) { | |
return; | |
} | |
$output = join( ', ', $meta_object->field_value ); | |
?> | |
<p> | |
<strong>Checkboxes</strong> : <?php echo esc_html( $output );?> | |
</p> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment