have a simple plugin that inserts text (a Google ad) partway through the content of a single post. I'm trying to create a settings page that will let users change the ad code, and change after which paragraph the ad appears.
I have spent all day reading tutorials and code, and I have a lovely settings page, but I can't figure out how to get it to actually save the data. I'm mostly following the example in Professional WordPress Plugin Development.
Here's what I have:
<?php
// add plugin options
add_option( 'wpa_ad_insert_paragraph', '2' );
add_option( 'wpa_ad_insert_adcode', 'ad code goes here');
// update options
update_option( 'wpa_ad_insert_paragraph', $_POST['wpa_ad_insert_paragraph'] );
update_option( 'wpa_ad_insert_adcode', $_POST['wpa_ad_insert_adcode'] );
// Add a menu for our option page
add_action('admin_menu', 'wpa_ad_insert_add_page');
function wpa_ad_insert_add_page() {
add_options_page( 'Ad Insertion', 'Ad Insertion', 'manage_options', 'wpa_ad_insert', 'wpa_ad_insert_option_page' );
}
// Draw the option page
function wpa_ad_insert_option_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Ad Insertion</h2>
<form action="options.php" method="post">
<?php settings_fields('wpa_ad_insert_options'); ?>
<?php do_settings_sections('wpa_ad_insert'); ?>
<input name="Submit" type="submit" value="Save Changes" />
</form>
</div>
<?php
}
// Register and define the settings
add_action('admin_init', 'wpa_ad_insert_admin_init');
function wpa_ad_insert_admin_init(){
register_setting(
'wpa_ad_insert_options',
'wpa_ad_insert_options',
'wpa_ad_insert_validate_options'
);
add_settings_section(
'wpa_ad_insert_main',
'Ad Insertion Settings',
'wpa_ad_insert_section_text',
'wpa_ad_insert'
);
add_settings_field(
'wpa_ad_insert_paragraph',
'Ad will appear after paragraph number:',
'wpa_ad_insert_paragraph_setting',
'wpa_ad_insert',
'wpa_ad_insert_main'
);
add_settings_field(
'wpa_ad_insert_adcode',
'Ad code:',
'wpa_ad_insert_adcode_setting',
'wpa_ad_insert',
'wpa_ad_insert_main'
);
}
function wpa_ad_insert_paragraph_setting() {
$options = get_option('wpa_ad_insert_paragraph');
$items = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20");
echo "<select id='wpa_ad_insert_paragraph' name='plugin_options[wpa_ad_insert_paragraph]'>";
foreach($items as $item) {
$selected = ($options['wpa_ad_insert_paragraph']==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
echo "</select>";
}
function wpa_ad_insert_adcode_setting() {
$options = get_option('wpa_ad_insert_adcode');
echo "<textarea id='wpa_ad_insert_adcode' name='plugin_options[wpa_ad_insert_adcode]' rows='7' cols='50' type='textarea'>{$options['wpa_ad_insert_adcode']}</textarea>";
}
function wpa_insert_ad($content) {
$paragraphAfter = get_option('wpa_ad_insert_paragraph');
$ad = get_option('wpa_ad_insert_adcode');
if( is_single() && is_main_query() ) {
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++ ) {
if ($i == $paragraphAfter)
echo $content[$i] . "</p>";
}
}
return $content;
}
add_filter('the_content', 'wpa_insert_ad');
?>
This code creates a settings page, but when I save the settings page, nothing happens (well, I get the yellow bar saying my settings have been saved, but the information is definitely not getting saved in the database.
What am I missing here?