Skip to content

Instantly share code, notes, and snippets.

@DavidPeralvarez DavidPeralvarez/options-api.php Secret
Created May 16, 2019

Embed
What would you like to do?
Actualizar opciones
<?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
You can’t perform that action at this time.