Skip to content

Instantly share code, notes, and snippets.

@DavidPeralvarez
Created May 16, 2019 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidPeralvarez/0b3597b46597e7dc8caabc851c6a96d8 to your computer and use it in GitHub Desktop.
Save DavidPeralvarez/0b3597b46597e7dc8caabc851c6a96d8 to your computer and use it in GitHub Desktop.
Recuperar 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(){
$age = get_option( 'dp_oa_age' );
var_dump( $age );
var_dump( get_option( 'dp_oa_true' ) );
var_dump( get_option( 'dp_oa_false' ) );
if( get_option( 'opcion_inventada' ) === false ):
echo "Esta opción no existe";
else:
echo "Esta opción ya existe";
endif;
// if( get_option( 'dp_oa_age', 'not_found' ) == 'not_found' ):
// echo "Esta opción no existe";
// else:
// echo "Esta opción ya existe";
// endif;
$options = get_option( 'dp_oa_options' );
$visits = $options['visits'];
$last_ip = $options['last_ip'];
$color = $options['color'];
$flag = $options['flag'];
var_dump( $flag );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment