2

I had recently installed Post Views Counter plugin, which created an options page under the Wordpress Settings menu. Now, I'd like to extend plugin functionalities by adding few more options. It will do by another plugin which has been created by me. The target is to add my own options directly into the page where actually the other options are. I don't want to create any top-level menu, submenu under Settings or anything else. How can I do that?

Thank you Riccardo

2
  • If the plugin use Settings API, I think you can add new settings easily. But we can not know how the plugin is built without study it, which is out of the scope of this site.
    – cybmeta
    Dec 27, 2016 at 8:17
  • Yes, it does. It uses add_action ('admin_menu', 'function'), add_options_page, add_settings_field and so on... Dec 27, 2016 at 8:59

1 Answer 1

2

If a third party plugin (the plugin you want to extend) uses the Settings API, you can add a new setting field if you now the options page and option group defined by the third party plugin. Just use the Settings APT too:

  • First, with add_setting_field() you can add a new field to any settings section defined by the the third plugin.
  • Second, with register_setting() you can register a new setting within the option group defined by the plugin.

A very quick example:

add_action( 'admin_init', 'cyb_add_settings_field_to_plugin' );
function cyb_add_settings_field_to_plugin() {

    add_settings_field(
        'some_id',
        'Some title',
        'cyb_field_callback',
        'plugin-settins-page', // Settings page defined by the third party plugin
        'plugin-settings-section', // Section defined by the third party plugin
        array()
    );

    register_setting(
        'option-group', // Options group defined by third party plugin
        'my-option-name', // Custom option name
        'cyb_sanitize_callback' // Sanitize
    );

}

function cyb_field_callback() {
    $value = get_option( 'my-option-name' );
    ?>
    <input type="text" id="some_id" name="my-option-name" value="<?php echo esc_attr( $value ); ?>" />
    <?php
}

function cyb_sanitize_callback( $inputs ) {
    // Do sanitization of the the inputs
    return $inputs;
}

If you wish, you can add new sections as well:

add_action( 'admin_init', 'cyb_add_settings_field_to_plugin' );
function cyb_add_settings_field_to_plugin() {

    add_settings_section(
        'new-settings-section',
        'Settings Section Title',
        'cyb_print_section_info', // Callback
        'plugin-settins-page' // Settings page defined by the third party plugin
    );  
    add_settings_field(
        'some_id',
        'Some title',
        'cyb_field_callback',
        'plugin-settins-page', // Settings page defined by the third party plugin
        'new-settings-section', // My custom section defined above
        array()
    );
    register_setting(
        'option-group', // Options group defined by third party plugin
        'my-option-name', // Option name
        'cyb_sanitize_callback' // Sanitize
    );
}

function cyb_print_section_info() {
    echo 'Section info';
}

function cyb_field_callback() {
    $value = get_option( 'my-option-name' );
    ?>
    <input type="text" id="some_id" name="my-option-name" value="<?php echo esc_attr( $value ); ?>" />
    <?php
}

function cyb_sanitize_callback( $inputs ) {
    // Do sanitization of the the inputs
    return $inputs;
}
8
  • Ok, I was able to add my custom option field, but for some reasons when I edit my option field and push save button, amendments are saved, whether I change the existing options of the plugin they are not saved. :( Dec 27, 2016 at 17:44
  • It seems you need to register your own setting and associated it with the option group defined by the third party plugin. Answer upadated.
    – cybmeta
    Dec 27, 2016 at 19:36
  • Sorry, but now my own option is not working and edits are not saved, while plugin options fields are. Anyway, In the callback "cyb_field_callback" you missed setting_fields() and do_settings_section() functions. If I add those two and use the plugin options group (post_views_counter_settings_general), everything gets messed in the options page. I don't know how to proceed further... I cannot understand why anyone have experienced such customization needs. Dec 27, 2016 at 20:44
  • I don't understand what you mean, I've tested the code with a plugin of mine which use Settings API and it works. cyb_field_callback() outputs the field HTML; settings_fields() should be in the Settings page callback and should be defined by the third party plugin. Can you post a sample code that it is not working for you?
    – cybmeta
    Dec 27, 2016 at 20:55
  • Edit the question to add additional information. Don't use answers if you are not writting an answer. A quick note after seeing your code: you are not following the instructions on my answer; follow them. You don't need a form at all, this code insert new fields in the form defined by the original plugin. Test it as it is and it should work, change only the specific references to the original plugin settings page and settings section.
    – cybmeta
    Dec 27, 2016 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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