Add new field to a PHP Wordpress Plugin

Hi, I’m customizing a Wordpress free plugin, this is the plugin official page:

the plugin add a HTML code to the header of the pages.

I need to add a second field into the script, I want to add a second textarea input, but I’m not able to do it… the plugin doesn’t dump me the field value. There is made of only one PHP file, I attached the code, it’s full of comments.

Can someone help me, please? Regards

[php]

<?php /* * Plugin Name: Add Code to Head * Plugin URI: http://hbjitney.com/add-code-to-header.html * Description: Adds custom html code (javascript, css, etc.) to each public page's head * Version: 1.13 * Author: HBJitney, LLC * Author URI: http://hbjitney.com/ * License: GPL3 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ if ( !class_exists('AddCodeToHead' ) ) { /** * Wrapper class to isolate us from the global space in order * to prevent method collision */ class AddCodeToHead { /** * Set up all actions, instantiate other */ function __construct() { add_action( 'admin_menu', array( $this, 'add_admin' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'wp_head', array( $this, 'display' ) ); } /** * Add our options to the settings menu */ function add_admin() { add_options_page('Add Code to Head', 'Add Code to Head', 'manage_options', 'acth_plugin', array( $this, 'plugin_options_page' ) ); } /** * Callback for options page - set up page title and instantiate field */ function plugin_options_page() { ?>
	<div class="plugin-options">
	 <h2><span>Add Code to Head</span></h2>
	 <form action="options.php" method="post">
<?php settings_fields( 'acth_options' ); do_settings_sections( 'acth_plugin' ); ?>
	  <input name="Submit" type="submit" value="<?php esc_attr_e( 'Save Changes' ); ?>" />
	 </form>
	</div>
<?php } /* * Define options section (only one) and fields (also only one!) */ function admin_init() { register_setting( 'acth_options', 'acth_options', array( $this, 'options_validate' ) ); add_settings_section( 'acth_section', '', array( $this, 'main_section' ), 'acth_plugin' ); add_settings_field( 'acth_string', 'Code', array( $this, 'text_field'), 'acth_plugin', 'acth_section'); } /* * Static content for options section */ function main_section() { // GNDN } /* * Code for field */ function text_field() { $options = get_option( 'acth_options' ); ?>
	        <textarea id="acth_options" name="acth_options[text_string]" rows="20" cols="90"><?php _e( $options['text_string'] );?></textarea>
<?php } /* * No validation, just remove leading and trailing space */ function options_validate($input) { $newinput['text_string'] = trim( $input['text_string'] ); return $newinput; } /* * Display the code(s) on the public page. * We do an extra check to ensure that the codes don't show up * in the admin tool. */ function display() { if( !is_admin() ) { $options = get_option( 'acth_options' ); _e( $options['text_string'] ); } } } } /* * Sanity - was there a problem setting up the class? If so, bail with error * Otherwise, class is now defined; create a new one it to get the ball rolling. */ if( class_exists( 'AddCodeToHead' ) ) { new AddCodeToHead(); } else { $message = "

Error in plugin

Sorry about that! Plugin add-code-to-head reports that it was unable to start.

Please report this error. Meanwhile, here are some things you can try:

  • Make sure you are running the latest version of the plugin; update the plugin if not.
  • There might be a conflict with other plugins. You can try disabling every other plugin; if the problem goes away, there is a conflict.
  • Try a different theme to see if there's a conflict between the theme and the plugin.
"; wp_die( $message ); } ?>

[/php]

Okay, what have you attempted?

Hi astonecipher, thank you for reply. I add my code modified, the script doesn’t save the new field value

the changes that I made are under the comment // @robyone

Thank you

[php]

<?php /* * Plugin Name: Add Code to Head * Plugin URI: http://hbjitney.com/add-code-to-header.html * Description: Adds custom html code (javascript, css, etc.) to each public page's head * Version: 1.13 * Author: HBJitney, LLC * Author URI: http://hbjitney.com/ * License: GPL3 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ if ( !class_exists('AddCodeToHead' ) ) { /** * Wrapper class to isolate us from the global space in order * to prevent method collision */ class AddCodeToHead { /** * Set up all actions, instantiate other */ function __construct() { add_action( 'admin_menu', array( $this, 'add_admin' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'wp_head', array( $this, 'display' ) ); } /** * Add our options to the settings menu */ function add_admin() { add_options_page('Add Code to Head', 'Add Code to Head', 'manage_options', 'acth_plugin', array( $this, 'plugin_options_page' ) ); } /** * Callback for options page - set up page title and instantiate field */ function plugin_options_page() { ?>
	<div class="plugin-options">
	 <h2><span>Add Code to Head</span></h2>
	 <form action="options.php" method="post">
<?php settings_fields( 'acth_options' ); // @robyone settings_fields( 'acth_options2' ); do_settings_sections( 'acth_plugin' ); ?>
	  <input name="Submit" type="submit" value="<?php esc_attr_e( 'Save Changes' ); ?>" />
	 </form>
	</div>
<?php } /* * Define options section (only one) and fields (also only one!) */ function admin_init() { register_setting( 'acth_options', 'acth_options', array( $this, 'options_validate' ) ); // @robyone register_setting( 'acth_options2', 'acth_options2', array( $this, 'options_validate' ) ); add_settings_section( 'acth_section', '', array( $this, 'main_section' ), 'acth_plugin' ); add_settings_field( 'acth_string', 'Code', array( $this, 'text_field'), 'acth_plugin', 'acth_section'); // @robyone add_settings_field( 'acth_string2', 'Code', array( $this, 'field2'), 'acth_plugin', 'acth_section'); } /* * Static content for options section */ function main_section() { // GNDN } /* * Code for field */ function text_field() { $options = get_option( 'acth_options' ); ?>
	        <textarea id="acth_options" name="acth_options[text_string]" rows="20" cols="90">
	        	<?php _e( $options['text_string'] );?>
	        </textarea>

			<!-- @robyone -->
			<textarea id="acth_options2" name="acth_options[field2]" rows="20" cols="90">
				<?php _e( $options['field2'] );?>
			</textarea>
<?php } /* * No validation, just remove leading and trailing space */ function options_validate($input) { $newinput['text_string'] = trim( $input['text_string'] ); return $newinput; } /* * Display the code(s) on the public page. * We do an extra check to ensure that the codes don't show up * in the admin tool. */ function display() { if( !is_admin() ) { $options = get_option( 'acth_options' ); _e( $options['text_string'] ); // @robyone $options = get_option( 'acth_options2' ); _e( $options['field2'] ); // @robyone } } } } /* * Sanity - was there a problem setting up the class? If so, bail with error * Otherwise, class is now defined; create a new one it to get the ball rolling. */ if( class_exists( 'AddCodeToHead' ) ) { new AddCodeToHead(); } else { $message = "

Error in plugin

Sorry about that! Plugin add-code-to-head reports that it was unable to start.

Please report this error. Meanwhile, here are some things you can try:

  • Make sure you are running the latest version of the plugin; update the plugin if not.
  • There might be a conflict with other plugins. You can try disabling every other plugin; if the problem goes away, there is a conflict.
  • Try a different theme to see if there's a conflict between the theme and the plugin.
"; wp_die( $message ); } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service