Skip to content

Instantly share code, notes, and snippets.

@willybahuaud
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save willybahuaud/c5b58c2f28f11b5da2fc to your computer and use it in GitHub Desktop.

Select an option

Save willybahuaud/c5b58c2f28f11b5da2fc to your computer and use it in GitHub Desktop.
<?php
// Le coeur de la fonction
add_action( 'muplugins_loaded', 'save_visite_google' );
function save_visite_google() {
if ( defined( 'DOING_AJAX' ) ) {
return;
}
if ( strstr( strtolower( $_SERVER['HTTP_USER_AGENT'] ) , 'googlebot' ) ) {
$opt = get_option( 'ep_visites' );
$date = current_time( 'H:m d/m/Y' );
if ( $opt ) {
$opt = explode( PHP_EOL, $opt );
array_unshift( $opt, $date );
} else {
$opt = array();
$opt[] = $date;
}
// On limite à 25 entrées…
$opt = array_slice( $opt, 0, 25 );
$opt = implode( PHP_EOL, $opt );
update_option( 'ep_visites', $opt );
}
}
// Pour voir la fonction
add_action( 'admin_menu', 'ep_create_menu' );
function ep_create_menu() {
add_options_page( 'google_ping', 'Visite Google', 'manage_options', 'ep_settings', 'ep_settings_page' );
register_setting( 'ep', 'ep_visites' );
}
function ep_settings_page() {
add_settings_section( 'ep_settings_page', __( 'Visites', 'ep' ), '__return_false', 'ep' );
add_settings_field( 'ep_visites',
'Les moments où Google à visiter le site',
'ep_setting_callback_function',
'ep',
'ep_settings_page',
array(
'options' => array(),
'name' => 'ep_visites'
) );
?>
<div class="wrap">
<h2>Espace Pro</h2>
<form action="options.php" method="post">
<?php
settings_fields( 'ep' );
do_settings_sections( 'ep' ); ?>
<?php submit_button(); ?>
</form>
<?php
}
function ep_setting_callback_function( $args ) {
extract( $args );
$value_old = get_option( $name );
echo '<textarea name="' . $name . '" class="widefat" rows="10">';
echo esc_textarea( $value_old );
echo '</textarea>';
}
<?php
// La même fonction pour chaque article
add_action( 'wp_head', 'save_each_visite_google' );
function save_each_visite_google() {
if ( defined( 'DOING_AJAX' ) || ! is_singular('post') ) {
return;
}
if ( strstr( strtolower( $_SERVER['HTTP_USER_AGENT'] ) , 'googlebot' ) ) {
global $post;
$opt = get_post_meta( $post->ID, 'ep_visites', true );
$date = current_time( 'H:m d/m/Y' );
if ( $opt ) {
$opt = explode( PHP_EOL, $opt );
array_unshift( $opt, $date );
} else {
$opt = array();
$opt[] = $date;
}
// On limite à 15 entrées…
$opt = array_slice( $opt, 0, 15 );
$opt = implode( PHP_EOL, $opt );
update_post_meta( $post->ID, 'ep_visites', $opt );
}
}
// L'afficher dans une metabox
add_action( 'add_meta_boxes', 'each_post_crawl' );
function each_post_crawl( $post_type ) {
if ( 'post' == $post_type ) {
add_meta_box( 'crawl-meta-box', 'last_crawls', 'crawl-meta-box', $post_type, 'normal', 'default' );
}
}
add_action( 'save_post', 'save_modified_crawl' );
function save_modified_crawl( $post_ID ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_ID;
}
if ( isset( $_POST[ 'ep_visites' ] ) ) {
check_admin_referer( 'crawl-save_' . $_POST['post_ID'], 'crawl-nonce' );
update_post_meta( $_POST['post_ID'], 'ep_visites', esc_textarea( $_POST[ 'ep_visites' ] ) );
}
}
function last_crawls( $post ) {
$crawls = get_post_meta( $post->ID, 'ep_visites', true );
echo '<textarea name="ep_visites" class="widefat" rows="10">';
echo esc_textarea( $crawls );
echo '</textarea>';
wp_nonce_field( 'crawl-save_' . $post->ID, 'crawl-nonce') ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment