Add wordpress custom options easily, and use your wordpress custom options in your wordpress themes.
WordPress Custom Options
Using Settings API we will add wordpress custom options.
Copy “Custom Option Function” code and paste it in your functions.php file.
Custom Option Function
function my_custom_option_function(){ add_settings_field( "my_option_id", "My Custom Option Title", "my_custom_option_callback", "general" ); register_setting( 'general', 'my_option_name' ); } add_action( 'admin_init', 'my_custom_option_function' ); function my_custom_option_callback(){ ?> <input name="my_option_name" type="text" value="<?php echo esc_attr( get_option('my_option_name') ); ?>"> <?php }
Note: this “my_option_name” is your option name, you can change it in function code and in usage code.
Now your custom option will be in “General options page”, WP-Admin > Settings > General.
If you want to change place from “General options page”, in “Custom Option Function” code, change this “general” to for example “reading”, now your custom option will be in “Reading options page”, read full list:
- reading (your custom option will be in reading options page).
- writing (your custom option will be in writing options page).
- discussion (your custom option will be in discussion options page).
- media (your custom option will be in media options page).
- permalink (your custom option will be in permalink options page).
- general (your custom option will be in general options page)
Usage
Now open file you want to display your custom option, for example open footer.php, and paste this code:
<?php if( get_option('my_option_name') ){ echo get_option('my_option_name'); } ?>
Now your custom option will be displayed in footer.
Clear? if not clear, leave a comment.