Last active
May 17, 2018 10:27
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
/** | |
* Add new tab in settings nav | |
*/ | |
function buddydev_add_new_settings_subnav_item() { | |
$parent_slug = bp_get_settings_slug(); | |
bp_core_new_subnav_item( array( | |
'name' => __( 'Search Engines', 'buddydev' ), | |
'slug' => 'search-engine', | |
'parent_slug' => $parent_slug, | |
'parent_url' => trailingslashit( bp_loggedin_user_domain() . $parent_slug ), | |
'screen_function' => 'buddydev_search_engine_settings_page', | |
'show_in_admin_bar' => true, | |
) ); | |
} | |
add_action( 'bp_settings_setup_nav', 'buddydev_add_new_settings_subnav_item' ); | |
/** | |
* Callback screen function | |
*/ | |
function buddydev_search_engine_settings_page() { | |
add_action( 'bp_template_content', 'buddydev_render_search_engine_settings_page' ); | |
bp_core_load_template( 'members/single/plugin' ); | |
} | |
/** | |
* Render following user posts | |
*/ | |
function buddydev_render_search_engine_settings_page() { | |
$action = trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() . '/' . 'search-engine' ); | |
$no_index = get_user_meta( get_current_user_id(), '_no_index', true ); | |
$no_follow = get_user_meta( get_current_user_id(), '_no_follow', true ); | |
?> | |
<h6><?php _e( '*Enable or Disable your Profile to get indexed from Search Engines*.', 'buddydev' ) ?></h6> | |
<form action="<?php echo $action?>" method="post" | |
class="standard-form" id="settings-form"> | |
<label for="no-index"><input type="checkbox" id="no-index" | |
name="buddydev-no-index" <?php checked( 'noindex', $no_index )?>> <?php _e( 'No Index', 'buddydev' ); ?></label> | |
<label for="no-follow"><input type="checkbox" id="no-follow" | |
name="buddydev-no-follow" <?php checked( 'nofollow', $no_follow )?>> <?php _e( 'No Follow', 'buddydev' ); ?></label> | |
<?php wp_nonce_field( 'search-engine-settings-save' ); ?> | |
<div class="submit"> | |
<input type="submit" name="search-engine-settings-submit" value="Save Changes" id="submit" class="auto"> | |
</div> | |
</form> | |
<?php | |
} | |
/** | |
* Save settings | |
*/ | |
function buddydev_save_search_engine_settings() { | |
if ( ! bp_is_settings_component() || ! bp_is_current_action( 'search-engine' ) || ! isset( $_POST['search-engine-settings-submit'] ) ) { | |
return; | |
} | |
if ( ! wp_verify_nonce( '_wpnonce', 'search-engine-settings-save' ) ) { | |
bp_core_add_message( __( 'Invalid action', 'buddydev' ), 'error' ); | |
} | |
$no_index = isset( $_POST['buddydev-no-index'] ) ? 'noindex' : ''; | |
$no_follow = isset( $_POST['buddydev-no-follow'] ) ? 'nofollow' : ''; | |
update_user_meta( get_current_user_id(), '_no_index', $no_index ); | |
update_user_meta( get_current_user_id(), '_no_follow', $no_follow ); | |
bp_core_add_message( __( 'Settings saved', 'buddydev' ), 'success' ); | |
} | |
add_action( 'bp_actions', 'buddydev_save_search_engine_settings' ); | |
/** | |
* Inject meta into header | |
*/ | |
function buddydev_inject_search_engine_meta() { | |
if ( ! bp_is_user() ) { | |
return; | |
} | |
$no_index = get_user_meta( bp_displayed_user_id(), '_no_index', true ); | |
$no_follow = get_user_meta( bp_displayed_user_id(), '_no_follow', true ); | |
$meta = array_filter( array( $no_index, $no_follow ) ); | |
$content = join( ',', $meta ); | |
?> | |
<meta name="robots" content="<?php echo $content ?>"> | |
<?php | |
} | |
add_action( 'wp_head', 'buddydev_inject_search_engine_meta' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment