-
-
Save DavidPeralvarez/6435841fde5c940b1e3b35efd8dd9cdf to your computer and use it in GitHub Desktop.
Actualizar opciones
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 | |
/* | |
Plugin Name: Options API | |
Description: Practicando con la API de opciones | |
*/ | |
// Activa el plugin | |
register_activation_hook( __FILE__, 'dp_oa_activate_plugin' ); | |
function dp_oa_activate_plugin(){ | |
// Asignar valores por defecto a las opciones/settings del plugin | |
// Utilizar esta función cuando activemos nuestro plugin | |
add_option( 'dp_oa_age', 18 ); | |
add_option( 'dp_oa_true', 1 ); | |
add_option( 'dp_oa_false', 0 ); | |
// Si existe la opción en la BBDD la actualiza y sino la crea nueva | |
//update_option( 'dp_oa_age', 21 ); | |
// Guardar una array de opciones | |
$options = array( | |
'visits' => 0, | |
'last_ip' => '3.145.4.78', | |
'color' => 'green', | |
'flag' => true | |
); | |
add_option( 'dp_oa_options', $options ); | |
} | |
add_action( 'init', 'dp_oa_visits_counter' ); | |
function dp_oa_visits_counter(){ | |
if( !is_admin() ): | |
$options = get_option( 'dp_oa_options' ); | |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { | |
//check ip from share internet | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { | |
//to check ip is pass from proxy | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} else { | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
$options['visits'] += 1; | |
$options['last_ip'] = $ip; | |
var_dump($options); | |
update_option( 'dp_oa_options', $options ); | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment