Reputation: 5
i'm creating a plugin which will diplay categories of the website. For each categort, i want to set an URL.
So i made a table which display all the categeories of the website and a field for each.
How to save each value for each category ?
Here is my code :
add_settings_field('name_categories','Categories','categories_cb','plugin-settings','plugin_label_settings');
function categories_cb() {
$settingurl = get_option('url_cat');
?>
<table>
<tr>
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$urlcat = 0;
foreach($categories as $category) {
echo "<tr>";
echo '<td align="center" bgcolor="#AEB5B4">'.$category->name.'';
echo "</td>";
//deuxieme colonne le nb de presence
echo "<td><input type='text' name='".$urlcat."' value='".$settingurl."'></td>";
echo "</tr>";
$urlcat++;
?>
And i want to be able to set an url for each field
Thanks a lot
Upvotes: 0
Views: 754
Reputation: 637
add_settings_field
is only for one input field. You could find clues in the reference
the callback function needs to output the appropriate html input and fill it with the old value, the saving will be done behind the scenes.
For your purpose, I recommend using add_settings_field
for each of the categories separately, The code would be something like this:
$categories = get_categories(array(
'orderby' => 'name',
'order' => 'ASC'
));
foreach ($categories as $category) {
add_settings_field(
'name_category_' . $category->cat_ID,
'Category ' . $category->name,
'categories_cb',
'plugin-settings',
'plugin_label_settings',
array(
'id' => $category->cat_ID
)
);
register_setting('plugin-settings', 'name_category_' . $category->cat_ID);
}
function categories_cb($args)
{
$settingurl = get_option('name_category_' . $args["id"]);
echo "<input type='text' name='name_category_". $args["id"] . "' value='" . $settingurl . "'>";
}
Upvotes: 1