I am new in Wordpress plugin development, and I wanted to create simple plugin, which will add 2 fields into General settings in Wordpress administration. I got following code (which works fine), from this answer: Add multiple custom fields to the general settings page
When I copy&paste this code into my theme functions.php
file, it works very nice. But I need it to work only after plugin has been activated (so it is independent of theme). So I moved this code into my plugin's main file, and I just added registration activation and deactivation plugin functions.
function my_general_section() {
add_settings_section(
'my_settings_section', // Section ID
'My Options Title', // Section Title
'my_section_options_callback', // Callback
'general' // What Page? This makes the section show up on the General Settings Page
);
add_settings_field( // Option 1
'option_1', // Option ID
'Option 1', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed (General Settings)
'my_settings_section', // Name of our section
array( // The $args
'option_1' // Should match Option ID
)
);
add_settings_field( // Option 2
'option_2', // Option ID
'Option 2', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed
'my_settings_section', // Name of our section (General Settings)
array( // The $args
'option_2' // Should match Option ID
)
);
register_setting('general','option_1', 'esc_attr');
register_setting('general','option_2', 'esc_attr');
}
function my_section_options_callback() { // Section Callback
echo '<p>A little message on editing info</p>';
}
function my_textbox_callback($args) { // Textbox Callback
$option = get_option($args[0]);
echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />';
}
function orgnizerInfoSettings__activation(){
add_action('admin_init', 'my_general_section');
}
function orgnizerInfoSettings__deactivation(){
remove_action('admin_init', 'my_general_section');
}
register_activation_hook( __FILE__, 'orgnizerInfoSettings__activation' );
register_deactivation_hook( __FILE__, 'orgnizerInfoSettings__deactivation' );
Any ideas what I am doing wrong? Everything seems legit to me.
I am new in Wordpress plugin development, and I wanted to create simple plugin, which will add 2 fields into General settings in Wordpress administration. I got following code (which works fine), from this answer: Add multiple custom fields to the general settings page
When I copy&paste this code into my theme functions.php
file, it works very nice. But I need it to work only after plugin has been activated (so it is independent of theme). So I moved this code into my plugin's main file, and I just added registration activation and deactivation plugin functions.
function my_general_section() {
add_settings_section(
'my_settings_section', // Section ID
'My Options Title', // Section Title
'my_section_options_callback', // Callback
'general' // What Page? This makes the section show up on the General Settings Page
);
add_settings_field( // Option 1
'option_1', // Option ID
'Option 1', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed (General Settings)
'my_settings_section', // Name of our section
array( // The $args
'option_1' // Should match Option ID
)
);
add_settings_field( // Option 2
'option_2', // Option ID
'Option 2', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed
'my_settings_section', // Name of our section (General Settings)
array( // The $args
'option_2' // Should match Option ID
)
);
register_setting('general','option_1', 'esc_attr');
register_setting('general','option_2', 'esc_attr');
}
function my_section_options_callback() { // Section Callback
echo '<p>A little message on editing info</p>';
}
function my_textbox_callback($args) { // Textbox Callback
$option = get_option($args[0]);
echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />';
}
function orgnizerInfoSettings__activation(){
add_action('admin_init', 'my_general_section');
}
function orgnizerInfoSettings__deactivation(){
remove_action('admin_init', 'my_general_section');
}
register_activation_hook( __FILE__, 'orgnizerInfoSettings__activation' );
register_deactivation_hook( __FILE__, 'orgnizerInfoSettings__deactivation' );
Any ideas what I am doing wrong? Everything seems legit to me.
Activation hook is run only after the plugin get activated. So only once and only during that one request. It's meant to allow author of a plugin to perform some actions after the plugin is activated. Actions, that need to be done only once. For example if your plugin uses custom database table - you can use activation hook to create such table.
On the other hand, add_action
has to be done every time, for every request, if you want it to be run during that request.
And if plugin is not active, then its code is not run any more, so its actions won't get executed.
This means that:
So here's your code after fixes:
function my_general_section() {
register_setting('general','option_1', 'esc_attr');
register_setting('general','option_2', 'esc_attr');
add_settings_section(
'my_settings_section', // Section ID
'My Options Title', // Section Title
'my_section_options_callback', // Callback
'general' // What Page? This makes the section show up on the General Settings Page
);
add_settings_field( // Option 1
'option_1', // Option ID
'Option 1', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed (General Settings)
'my_settings_section', // Name of our section
array( // The $args
'option_1' // Should match Option ID
)
);
add_settings_field( // Option 2
'option_2', // Option ID
'Option 2', // Label
'my_textbox_callback', // !important - This is where the args go!
'general', // Page it will be displayed
'my_settings_section', // Name of our section (General Settings)
array( // The $args
'option_2' // Should match Option ID
)
);
}
function my_section_options_callback() { // Section Callback
echo '<p>A little message on editing info</p>';
}
function my_textbox_callback($args) { // Textbox Callback
$option = get_option($args[0]);
echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />';
}
add_action('admin_init', 'my_general_section');