How to create social media setting options field in wordpress

Here is the code

copy and paste the code in your functions.php file


<?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',
'Social Media',
'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">
<h1>Social Media</h1>
<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(
'facebook',
'Facebook',
array( $this, 'facebook_callback' ),
'my-setting-admin',
'setting_section_id'
);
add_settings_field(
'twitter',
'Twitter',
array( $this, 'twitter_callback' ),
'my-setting-admin',
'setting_section_id'
);
add_settings_field(
'linkedin',
'Linkedin',
array( $this, 'linkedin_callback' ),
'my-setting-admin',
'setting_section_id'
);
add_settings_field(
'youtube',
'Youtube',
array( $this, 'youtube_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['facebook'] ) )
$new_input['facebook'] = sanitize_text_field( $input['facebook'] );

if( isset( $input['twitter'] ) )
$new_input['twitter'] = sanitize_text_field( $input['twitter'] );

if( isset( $input['linkedin'] ) )
$new_input['linkedin'] = sanitize_text_field( $input['linkedin'] );

if( isset( $input['youtube'] ) )
$new_input['youtube'] = sanitize_text_field( $input['youtube'] );

return $new_input;
}

/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}

public function facebook_callback()
{
printf(
'<input type="text" id="facebook" name="my_option_name[facebook]" value="%s" />',
isset( $this->options['facebook'] ) ? esc_attr( $this->options['facebook']) : ''
);
}
public function twitter_callback()
{
printf(
'<input type="text" id="twitter" name="my_option_name[twitter]" value="%s" />',
isset( $this->options['twitter'] ) ? esc_attr( $this->options['twitter']) : ''
);
}
public function linkedin_callback()
{
printf(
'<input type="text" id="linkedin" name="my_option_name[linkedin]" value="%s" />',
isset( $this->options['linkedin'] ) ? esc_attr( $this->options['linkedin']) : ''
);
}
public function youtube_callback()
{
printf(
'<input type="text" id="youtube" name="my_option_name[youtube]" value="%s" />',
isset( $this->options['youtube'] ) ? esc_attr( $this->options['youtube']) : ''
);
}
}

if( is_admin() )
$my_settings_page = new MySettingsPage();

You have able to navigate the page from under settings menu




Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.