I have a plugin that's supposed to add some social buttons to a single post page. I use settings API to set some options for the ability to disable and position the buttons left or right, but it doesn't seem to work. Can someone please tell me what I did wrong in my code? I'm kinda new at this so I really have no clue. Thank you.
<?php
/*
Plugin Name: kvn_addThis
Plugin URI: https://github.com/greathug2u/addThis
Version: 1.0
Description: Adds social buttons to single post page
Author: Vy Nguyen
License: GNU General Public License v2 or later
*/
function addthis_enqueue_scripts() {
if ( is_single() && 0 == get_option( 'addthis_disable_button', 0 ) ) {
wp_enqueue_script(
'addthis',
'//s7.addthis.com/js/300/addthis_widget.js#pubid=xa-51f5484b6f2193d4',
array(),
null,
true
);
wp_enqueue_script(
'addthis_button',
'//s7.addthis.com/js/300/addthis_widget.js#pubid=xa-51f5532112489668',
array(),
null,
true
);
}
}
add_action( 'wp_enqueue_scripts', 'addthis_enqueue_scripts' );
function addthis_scripts() {
if ( is_single() && 0 == get_option( 'addthis_disable_button', 0 ) ) {
$script_html = '<script type="text/javascript">';
$script_html .= 'addthis.layers({';
$script_html .= '"theme" : "gray",';
$script_html .= '"share" : {';
if ( 'right' == get_option( 'addthis_position_choice','right' ) ) {
$script_html .= '"position" : "right",';
} else {
$script_html .= '"position" : "left",';
}
$script_html .= '"numPreferredServices" : 5';
$script_html .= '}';
$script_html .= '});';
$script_html .= '</script>';
echo $script_html;
}
}
add_action( 'wp_head', 'addthis_scripts' );
function addthis_add_button() {
if ( is_single() && 0 == get_option( 'addthis_disable_button', 0 ) ) {
$sharebar_html = '<div class="addthis_toolbox addthis_default_style ">';
$sharebar_html .= '<a></a>';
$sharebar_html .= '<a></a>';
$sharebar_html .= '<a></a>';
$sharebar_html .= '<a></a>';
$sharebar_html .= '</div>';
echo $sharebar_html;
}
}
add_action( 'wp_head', 'addthis_add_button');
/*---------------------------------SETTINGS---------------------------------*/
function addthis_add_options_page() {
add_options_page(
__( 'AddThis Options' ),
__( 'AddThis Options' ),
'manage_options',
'addthis_options_page',
'addthis_render_options_page'
);
}
add_action( 'admin_menu', 'addthis_add_options_page' );
function addthis_render_options_page() {
?>
<div class='wrap'>
<?php screen_icon(); ?>
<h2><?php _e( 'AddThis Options' ); ?></h2>
<form action="options.php" method="post">
<?php settings_fields( 'addthis_disable_button' ); ?>
<?php settings_fields( 'addthis_position_choice' ); ?>
<?php do_settings_sections( 'addthis_options_page' ); ?>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes' ); ?>">
</p>
</form>
</div>
<?php
}
function addthis_render_main_settings_text() {
echo "<p>Main settings for the AddThis plugin.</p>";
}
/*----------------------Disable Button----------------------*/
function addthis_add_disable_button_settings() {
register_setting(
'addthis_disable_button',
'addthis_disable_button',
'absint'
);
add_settings_section(
'addthis_main_settings',
__( 'AddThis Controls' ),
'addthis_render_main_settings_text',
'addthis_options_page'
);
add_settings_field(
'addthis_disable_button_field',
__( 'Disable Sharing Buttons' ),
'addthis_render_disable_button_input',
'addthis_options_page',
'addthis_main_settings'
);
}
add_action( 'admin_init', 'addthis_add_disable_button_settings' );
function addthis_render_disable_button_input() {
$current = get_option( 'addthis_disable_button', 0 );
echo '<input name="addthis_disable_button"' . checked( $current, 1, false ) . ' type="checkbox" value="1" />';
}
/*----------------------Position Choice----------------------*/
function addthis_add_position_choice_settings() {
register_setting(
'addthis_position_choice',
'addthis_position_choice',
'absint'
);
add_settings_section(
'addthis_main_settings',
__( 'AddThis Controls' ),
'addthis_render_main_settings_text',
'addthis_options_page'
);
add_settings_field(
'addthis_position_choice_field',
__( 'Sharing Buttons Positioning' ),
'addthis_render_position_choice_input',
'addthis_options_page',
'addthis_main_settings'
);
}
add_action( 'admin_init', 'addthis_add_position_choice_settings' );
function addthis_render_position_choice_input() {
$current = get_option( 'addthis_position_choice', 'right' );
echo '<input type="radio" name="addthis_postion_choice"' . checked( $current, 'left', false ) . ' value="left">Left';
echo '<input type="radio" name="addthis_postion_choice" value="right">Right';
}