Created
October 10, 2021 10:33
-
-
Save VivekSaha/132142c30528f29b3b90d5f2cf04e542 to your computer and use it in GitHub Desktop.
Custom Theme Option Using Setting API
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
add_action("admin_menu","mythemeoptions"); | |
function mythemeoptions(){ | |
add_menu_page( | |
"theme-options", //page title | |
"Footer Options", //menu title | |
"manage_options", //capability | |
"theme-options", //menu slug | |
"mycustom_options", //call back function | |
"dashicons-admin-post" //icon | |
); | |
} | |
function mycustom_options(){ | |
?> | |
<div class="form-cust"> | |
<h1>Footer Options</h1> | |
<span><?php settings_errors(); ?></span> | |
<form action="options.php" method="post" enctype="multipart/form-data"> | |
<?php | |
settings_fields("section"); | |
do_settings_sections("theme-options"); | |
submit_button(); | |
?> | |
</form> | |
</div> | |
<?php | |
} | |
//Theme options settiong page | |
function theme_options_setting(){ | |
add_settings_section( | |
"section", //id | |
"All Settings", //title | |
"", //callback | |
"theme-options" //page | |
); | |
add_settings_field( | |
"facebook_url", | |
"Facebook URL", | |
"display_facebook_url", | |
"theme-options", | |
"section" | |
); | |
add_settings_field( | |
"twitter_url", | |
"Twitter URL", | |
"display_twitter_url", | |
"theme-options", | |
"section" | |
); | |
add_settings_field( | |
"linkedin_url", | |
"Linkedin URL", | |
"display_linkedin_url", | |
"theme-options", | |
"section" | |
); | |
add_settings_field( | |
"instagram_url", | |
"Instagram URL", | |
"display_instagram_url", | |
"theme-options", | |
"section" | |
); | |
add_settings_field( | |
"youtube_url", | |
"Youtube URL", | |
"display_youtube_url", | |
"theme-options", | |
"section" | |
); | |
add_settings_field( | |
"copyright_text", | |
"Copyright Text", | |
"display_copyright_text", | |
"theme-options", | |
"section" | |
); | |
add_settings_field( | |
"coming_soon_img", | |
"Coming Soon Image", | |
"display_coming_soon_img", | |
"theme-options", | |
"section" | |
); | |
register_setting("section","facebook_url"); | |
register_setting("section","twitter_url"); | |
register_setting("section","linkedin_url"); | |
register_setting("section","instagram_url"); | |
register_setting("section","youtube_url"); | |
register_setting("section","copyright_text"); | |
register_setting("section", "coming_soon_img", "handle_logo_upload"); | |
} | |
add_action("admin_init","theme_options_setting"); | |
function display_facebook_url(){ | |
?> | |
<input type="text" name="facebook_url" value="<?php echo get_option('facebook_url') ?>" id="facebook_url"> | |
<?php | |
} | |
function display_twitter_url(){ | |
?> | |
<input type="text" name="twitter_url" value="<?php echo get_option('twitter_url') ?>" id="twitter_url"> | |
<?php | |
} | |
function display_linkedin_url(){ | |
?> | |
<input type="text" name="linkedin_url" value="<?php echo get_option('linkedin_url') ?>" id="linkedin_url"> | |
<?php | |
} | |
function display_instagram_url(){ | |
?> | |
<input type="text" name="instagram_url" value="<?php echo get_option('instagram_url') ?>" id="instagram_url"> | |
<?php | |
} | |
function display_youtube_url(){ | |
?> | |
<input type="text" name="youtube_url" value="<?php echo get_option('youtube_url') ?>" id="youtube_url"> | |
<?php | |
} | |
function display_copyright_text(){ | |
?> | |
<textarea name="copyright_text" row="20" cols="50" id="copyright_text"><?php echo get_option('copyright_text') ?></textarea> | |
<?php | |
} | |
function display_coming_soon_img(){ | |
?> | |
<img src="<?php echo get_option('coming_soon_img'); ?>" class="img-responsive"> | |
<input type="file" name="coming_soon_img" id="coming_soon_img"> | |
<?php | |
} | |
function handle_logo_upload($option) | |
{ | |
if(!empty($_FILES["coming_soon_img"]["tmp_name"])) | |
{ | |
$urls = wp_handle_upload($_FILES["coming_soon_img"], array('test_form' => FALSE)); | |
$temp = $urls["url"]; | |
return $temp; | |
} | |
return $option; | |
} | |
value fetch:- <?php echo get_option('coming_soon_img'); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment