Answer the question
In order to leave comments, you need to log in
How to fix a Wordpress plugin so that the settings are saved?
I'm writing a Paid Post plugin. I made a test settings page for it (there is only one there so far). The page is displayed normally, but when you click "save" the setting does not change (the checkmark is always on). Reading the Settings API and learning about other people's plugins didn't help.
Piece of code:
// Admin options
function PPOptionsPage() {
add_settings_section("section_main", __("Main options", 'paid-posts'), '', "paid-posts");
add_settings_field(
"pp_display_excerpt",
__("Display post excerpt on a single page before paywall text", 'paid-posts' ),
"PPCheckbox",
"paid-posts",
"section_main",
array( 'label_for' => 'pp_display_excerpt')
);
register_setting("paid-posts", "pp_display_excerpt");
}
function PPCheckbox($options)
{
echo '<input type="checkbox" class="code" name="'. $options['label_for'] .'" id="'. $options['label_for'] .'" value="1"' . checked(1, get_option($options['label_for']), false) . '>';
}
add_action("admin_init", "PPOptionsPage");
function PPAdminMenu() {
add_options_page(
__('Paid Post options', 'paid-posts'),
__('Paid Post', 'paid-posts'),
'manage_options',
'paid-posts',
'PPAdminOptions'
);
}
function PPAdminOptions() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<div class="wrap">
<h1><?php echo get_admin_page_title(); ?></h1>
<form method="post" action="">
<?php
settings_fields("section_main");
do_settings_sections("paid-posts");
submit_button();
?>
</form>
</div>
<?php
}
add_action( 'admin_menu', 'PPAdminMenu' );
Answer the question
In order to leave comments, you need to log in
The correct answer is:
action="" should be replaced with action="options.php", otherwise the form is simply submitted to the same page and no saving takes place.
settings_fields("section_main") should be replaced with settings_fields("paid-posts") - this should be the same value as the first parameter in register_setting().
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question