4

I'm using the Wordpress Plugin Boilerplate to make a plugin and I'm having some trouble outputting a form for users to input configuration options for the said plugin.

I have the following methods for a class

public function admin_add_menu()
{
    add_menu_page(
            'Paypal Me Order',
            'Paypal Me',
            'manage_options',
            'paypal_me_order',
             array($this, 'admin_display'),
            'dashicons-fa-cc-paypal',
            20
    );
}

function admin_init()
{
        register_setting( 'paypal_me_order-group', 'paypal_me_order-option', array( $this , 'sanitize') );
        add_settings_section( 'paypal_me_order-section', 'Paypal Me Settings', array( $this, 'admin_section' ), 'paypal_me_order' );
        add_settings_field( 'paypal_me_order-link', 'Paypal Me Link', array( $this, 'admn_link_field' ), 'paypal_me_order', 'paypal_me_order-section' );


}

function admin_display (){
    include plugin_dir_path(__FILE__) . 'partials/paypal_me_order-admin-display.php';
}

public function admin_section($args)
{
    include plugin_dir_path(__FILE__) . 'partials/paypal_me_order-admin-section-display.php';
}

public function admin_link_field( $args) {
    include plugin_dir_path(__FILE__) . 'partials/paypal_me_order-link-field.php';
}

Inside admin_display()

<form action="options.php" method="POST">
        <?php settings_fields('paypal_me_order-group'); ?>
        <?php do_settings_sections('paypal_me_order-section'); ?>
        <?php submit_button(); ?>
      </form>

I get the top level menu and the Page shows up but not the "section" or the "fields"

What am I doing wrong here? Why is it not working?

UPDATE

I am able to get the page to display but I cannot get the input field to print.

  • 1
    Hi Kendall, Your form isn't on here. Where is your actual form code? – rudtek Apr 7 '17 at 3:14
  • @rudtek Did not think that would have been relevant but I updated the question. – Kendall ARNEAUD Apr 7 '17 at 13:14
  • You say your form isn't showing but you don't think the form call is relative? – rudtek Apr 7 '17 at 15:12
7

Try this. Please change the field name with yours

<?php
class MySettingsPage
{
    /**
     * Holds the values to be used in the fields callbacks
     */
    private $options;

    /**
     * Start up
     */
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'page_init' ) );
    }

    /**
     * Add options page
     */
    public function add_plugin_page()
    {
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin', 
            'My Settings', 
            'manage_options', 
            'my-setting-admin', 
            array( $this, 'create_admin_page' )
        );
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        // Set class property
        $this->options = get_option( 'my_option_name' );
        ?>
        <div class="wrap">
            <h1>My Settings</h1>
            <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( 'my_option_group' );
                do_settings_sections( 'my-setting-admin' );
                submit_button();
            ?>
            </form>
        </div>
        <?php
    }

    /**
     * Register and add settings
     */
    public function page_init()
    {        
        register_setting(
            'my_option_group', // Option group
            'my_option_name', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'My Custom Settings', // Title
            array( $this, 'print_section_info' ), // Callback
            'my-setting-admin' // Page
        );  

        add_settings_field(
            'id_number', // ID
            'ID Number', // Title 
            array( $this, 'id_number_callback' ), // Callback
            'my-setting-admin', // Page
            'setting_section_id' // Section           
        );      

        add_settings_field(
            'title', 
            'Title', 
            array( $this, 'title_callback' ), 
            'my-setting-admin', 
            'setting_section_id'
        );      
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();
        if( isset( $input['id_number'] ) )
            $new_input['id_number'] = absint( $input['id_number'] );

        if( isset( $input['title'] ) )
            $new_input['title'] = sanitize_text_field( $input['title'] );

        return $new_input;
    }

    /** 
     * Print the Section text
     */
    public function print_section_info()
    {
        print 'Enter your settings below:';
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function id_number_callback()
    {
        printf(
            '<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
            isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
        );
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function title_callback()
    {
        printf(
            '<input type="text" id="title" name="my_option_name[title]" value="%s" />',
            isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
        );
    }
}

if( is_admin() )
    $my_settings_page = new MySettingsPage();
  • thanks for your example. But I would like to try and understand it as well. I do not want the menu to be "under settings" but rather at the top. On another note...what is different from your approach to yours that makes your code work? – Kendall ARNEAUD Apr 7 '17 at 13:36
  • the class is just unneeded and not useful bloat. namesoaces were introduced ages ago in PHP in 5.3, there is no reason not to use them instead of repeating this very non OOP pattern that adds only bloat to the code. – Mark Kaplun Apr 7 '17 at 14:34
  • @MarkKaplun really have no idea what you said there or meant. – Kendall ARNEAUD Apr 7 '17 at 15:18
  • @Kendall you should not use classes if you are not going to do OOP. It just adds clutter and makes removing filters harder. – Mark Kaplun Apr 7 '17 at 16:12
  • I really like the way this is done, even if it feels a little unnecessarily convoluted. For a simple example, sure it does, but when you've got hundreds of options and multiple pages, this will really help organize things. – aalaap Jun 28 '18 at 10:03
0

Turns out it was a typo in my functions. I can now see the settings page perfectly!

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.