• 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';
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you having trouble with the Settings API or enqueuing scripts? If the Settings API, please provide more information and code.

    If enqueuing, try specifying a protocol, such as https://s7.addthis.com/... Also replace the hash mark ( # ) after .js with a question mark ( ? ). Doing these two things should at least get the script loaded, not sure if anything else is wrong.

    Thread Starter greathug2u

    (@greathug2u)

    I’m not sure either! I changed it like you suggested and it’s still not working. Thank you so much, though.

    Moderator bcworkz

    (@bcworkz)

    If the settings you added are mis-configured, the option could be stuck on ‘disable’ and be preventing the scripts from being loaded. Either check the value stored in the options table or simply remove the && 0 == get_option( 'addthis_disable_button', 0 ) for the time being until you get the buttons displayed. Check the HTML output and verify the meta link tags were properly placed in the head section.

    Be sure you’ve followed the instructions for using this widget exactly. I’m unfamiliar with this one, but these things often require you to place a <div> or something with a specific ID, name, or class where you want the buttons displayed, or possibly some minor javascript needs to be placed somewhere on the page to trigger the script. Also be sure there are no dependencies like jquery that may need to be specified.

    Other than the basic double checking you work, I’m not sure what to suggest. If you can get the widget working and still need help with the Settings API, I can probably help you. But getting a 3rd party javascript working? I’m kind of lost, sorry. All I know is the script you posted looks OK.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Settings API’ is closed to new replies.