Support » Plugins » How will i properly sanitize of wordpress plugin input field?

  • Resolved Sumon Hasan

    (@infodesignbd)


    function add_css_setting_register() {
    	register_setting(
    		'add_css_option',
    		'add_css_option', 
    		'add_css_validate_setting' // Sanitize callback
    	);
    }
    add_action( 'admin_init', 'add_css_setting_register' );
    
    /* 
    	Function to generate options page
    */
    function add_css_option_ramwrork() {
    	
    	$options_value    = get_option( 'add_css_option' );
    	$add_css = isset( $options_value['add_css'] ) ? $options_value['add_css'] : '';
    	$add_css = wp_kses( $add_css, array( '\'', '\"', '>', '+' ) );
    	?>
    
    <div class="wrap" id="poststuff">
    		<div id="post-body" class="columns-2">
    		<?php settings_errors(); ?>
    		<form action="options.php" method="post">
    				<div id="post-body-content">
    						<?php settings_fields( 'add_css_option' ); ?>
    					<div class="wp_custom_css_textarea_main">
    						<textarea name="add_css_option[add_css]" id="wp_custom_css_textarea"><?php echo $add_css; ?></textarea>
    					</div>
    				</div>
    
    				<div id="postbox-container-1" class="postbox-container add-css-right">
    					<div>
    						<?php submit_button( __( 'Save CSS', 'add-css' ), 'primary large add-css-save' ); ?>
    
    						<div class="postbox">
    							<h3 class="sideber"><span><?php _e( 'Sumon Hasan', 'simple-css' ); ?></span></h3>
    							<div class="inside">
    								<p><?php printf( __( 'Visit for more free WordPress theme and plugin, %s.', 'add-css' ), '<a href="http://sumonhasan.com/plugins/" target="_blank">Link</a>' ); ?></p>
    							</div>
    						</div>
    					</div>
    				</div>
    			</form>
    		</div>
    	</div>
    	<?php
    }
    
    /**
     * Sanitize and validate form input.
     */
    function add_css_validate_setting( $input ) {
    	$input['add_css'] = wp_kses( $input['add_css'], array( '\'', '\"', '>', '+' ) );
    	return $input;
    }
    
    /* 
    	Add CSS head options css output
    */
    
    function add_css_output() {
    
    	$option = get_option( 'add_css_option' );
    	$add_css_output = isset( $option['add_css'] ) ? $option['add_css'] : '';
    	$add_css_output = wp_kses( $add_css_output, array( '\'', '\"', '>', '+' ) );
    	$add_css_output = str_replace( '>', '>', $add_css_output );;
    
    	if ( $add_css_output ) {
    		$display_css = '<!-- add css plugin style start -->' . "\n";
    		$display_css .= '<style>' . "\n";
    		$display_css .= $add_css_output . "\n";
    		$display_css .= '</style>' . "\n";
    		$display_css .= '<!-- add css plugin style end -->' . "\n";
    		echo $display_css;
    	}
    
    }
    add_action( 'wp_head', 'add_css_output', 20 );
  • The topic ‘How will i properly sanitize of wordpress plugin input field?’ is closed to new replies.