i have one question about WP global variables. I have implemented if function into my header.php file. This is very simple function that would need to check variable value and show the correct data, depending on value.
This is if function:
<?php
if ($spon == 0)
{
echo 'number 0';
}
elseif ($spon == 1)
{
echo 'number 2';
}
else
{
echo 'number 3';
}
?>
This function works fine for my needs, but now i would like to create global variable that i can change the value of it in admin panel. Is that possible and how ?
I would recommend you the Options Framework.
If you still do want to do it by yourself, here is a long description.
https://codex.wordpress.org/Creating_Options_Pages
On this Wordpress-Codex Page is everything you need and you should try it - if you still have any questions, just ask again.
But just as a summary, these are the steps you have to do:
add_options_page()
or add_menu_page()
…On the other part you get your saved option (check if you have already saved it …) simply with:
<?php echo get_option( $option, $default ); ?>
e.g.
<?php echo get_option( 'blogname' ); ?>
<?php echo get_option( 'myOption' ); ?>
It's actually not that hard .. but it's much easier with the Options-Framework.
http://wptheming.com/options-framework-theme/
Here is the ready-to-go-but-understand-it-before-you-use-it
-code as in the Codex:
<?php
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'My Settings',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<h2>My Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'My Custom Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'id_number', // ID
'ID Number', // Title
array( $this, 'id_number_callback' ), // Callback
'my-setting-admin', // Page
'setting_section_id' // Section
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'my-setting-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['id_number'] ) )
$new_input['id_number'] = absint( $input['id_number'] );
if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function id_number_callback()
{
printf(
'<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="my_option_name[title]" value="%s" />',
isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
Have fun, and please understand what you do!