Extend plugin options page

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

1 Answer
1

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;
}

Leave a Comment