0

I've been making a plugin for wordpress that adds two simple text field options. The options are saved successfully in plugin options page but don't work, it doesn't output saved options.

enter image description here

Here's the code:

add_action( 'admin_menu', 'viptips_add_admin_menu' );
add_action( 'admin_init', 'viptips_settings_init' );


function viptips_add_admin_menu(  ) { 
  $icon       = 'dashicons-editor-table';
    add_menu_page( 'VIP Tips', 'VIP Tips', 'manage_options', 'vip_tips', 'viptips_options_page', $icon );

}


function viptips_settings_init(  ) { 

    register_setting( 'pluginPage', 'viptips_settings' );

    add_settings_section(
        'viptips_pluginPage_section', 
        __( 'Main plugin options', 'viptips' ), 
        'viptips_settings_section_callback', 
        'pluginPage'
    );

    add_settings_field( 
        'viptips_category_name', 
        __( 'Category name:', 'viptips' ), 
        'viptips_category_name_render', 
        'pluginPage', 
        'viptips_pluginPage_section' 
    );

    add_settings_field( 
        'viptips_postperpage', 
        __( 'Number of posts:', 'viptips' ), 
        'viptips_postperpage_render', 
        'pluginPage', 
        'viptips_pluginPage_section' 
    );


}


function viptips_category_name_render(  ) { 

    $options = get_option( 'viptips_settings' );
    ?>
  <input type='text' name='viptips_settings[viptips_category_name]' value='<?php echo $options['viptips_category_name']; ?>'>
  <p class="description" id="tagline-description">Name of the category that contains predictions</p>
    <?php

}


function viptips_postperpage_render(  ) { 

    $options = get_option( 'viptips_settings' );
    ?>
  <input type='number' name='viptips_settings[viptips_postperpage]' value='<?php echo $options['viptips_postperpage']; ?>'>
  <p class="description" id="tagline-description">Number of posts to be displayed in table.</p>
    <?php

}


function viptips_settings_section_callback(  ) { 

    echo __( 'Don\'t forget to use the <strong>[tabscat]</strong> shortcode, in the page you want the tips table to be displayed!*', 'viptips' );

}


function viptips_options_page(  ) { 

        ?>
        <form action='options.php' method='post'>

            <h1>VIP Tips</h1>

            <?php
            settings_fields( 'pluginPage' );
            do_settings_sections( 'pluginPage' );
            submit_button();
            ?>

        </form>
        <?php

}

To output saved options, I use:

$category_name = get_option('viptips_category_name');
$postperpage = get_option('viptips_postperpage');


    $args = array(
     'post_type' => 'post',
     'post_status' => 'publish',
     'category_name' => $category_name,
     'post_per_page' => $postperpage
     );

Where did I go wrong?

2

Basically, you're not using the correct option name.

In your viptips_settings_init() function, you use the name viptips_settings as the database option name when registering the setting:

// The syntax is: register_setting( '<settings group name>', '<database option name>' )
register_setting( 'pluginPage', 'viptips_settings' );

So WordPress will use that viptips_settings when saving the option (i.e. update_option( 'viptips_settings', '<value>' )) and therefore you would also use the same name when getting the option from the database, regardless if the option is an array of values or just a single value:

  • The input with an array of values:

    <input type='text' name='viptips_settings[viptips_category_name]' ...>
    <input type='number' name='viptips_settings[viptips_postperpage]' ...>
    
  • The input with a single value:

    <input type='text' name='viptips_settings' ...>
    

So for example without using the $category_name and $postperpage variables, you could do something like (but make sure the array keys/items are actually set):

$options = (array) get_option( 'viptips_settings' );

$args = array(
    'post_type'     => 'post',
    'post_status'   => 'publish',
    'category_name' => $options['viptips_category_name'],
    'post_per_page' => $options['viptips_postperpage']
);

And if you had these in your code:

  • PHP

    // Here we've got two different database option names.
    register_setting( 'pluginPage', 'viptips_category_name' );
    register_setting( 'pluginPage', 'viptips_postperpage' );
    
  • HTML (in the form)

    <!-- And here two different input names; both not array -->
    <input type='text' name='viptips_category_name' ...>
    <input type='number' name='viptips_postperpage' ...>
    

Then your code, or this, would work:

$category_name = get_option( 'viptips_category_name' );
$postperpage = get_option( 'viptips_postperpage' );

So I hope this (revised) answer helps you (more) and others as well. :)

| improve this answer | |
2

You have to use viptips_settings option to get values. It will return array and you can get both value using below code.

$options = get_option('viptips_settings');
$category_name = $options['viptips_category_name'];
$postperpage = $options['viptips_postperpage'];


    $args = array(
     'post_type' => 'post',
     'post_status' => 'publish',
     'category_name' => $category_name,
     'post_per_page' => $postperpage
     );
| improve this answer | |
  • Your solution also works like a charm, but I accepted the answer that came first. Thank you! – Knott Mar 4 at 10:49
  • It's Okay, No problem! Thanks :-) – Chetan Vaghela Mar 4 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.