Search code examples
phpwordpresswordpress-plugin-creation

do_settings_sections() is not working with WordPress Plugins PHP


I am new to both Wordpress development and PHP and I am currently following this tutorial that I really enjoy: https://www.youtube.com/watch?v=hbJiwm5YL5Q

But it did not take long until my code (even though eyactly the same as in the tutorial to my findings) stopped behaving as expected. The do_settings_sections doesn´t render the field.

What am I doing wrong? Unlike in this question I initialized the function, didn´t I?

My code:

<?php

/*
    Plugin Name: Word Count Statistics
    Description: A plugin to display the word and character count, as well as the aproximate reading time for an article. Where and what to display can be customized on the settings page.
    Version: 1.0
    ...
*/

//using a class allows the usage of function names, that don´t need to be unique
class WordCountAndTimePlugin 
{
   function __construct()
    {
        //array allows referencing the function names in the class
        add_action('admin_menu', array($this, 'adminPage'));
        add_action('admin-init', array($this, 'settings'));
    }
   
   function adminPage() 
    {
        add_options_page(
            'Word Count Settings', //Title of page
            'Word Count', //Display name in menu
            'manage_options', //Access allowed if user may...
            'word-count-settings-page', //Slug name
            array($this, 'displayHTML') //Function to call
        );
    }

    function displayHTML() 
    { 
        ?>
            <div class="wrap">
                <h1>Word Count Settings</h1>
                <form action="options.php" method="POST">
                    <?php
                        do_settings_sections('word-count-settings-page');
                        submit_button();
                    ?>
                </form>
            </div>
        <?php 
    }

    function settings()
    {

        //Creates a form section 
        add_settings_section(
            'wcp_first_section', //Section name
            'some text here', //Subtitle
            null, //Content / Desription
            'word-count-settings-page' //Slug name
        );

        //Creates a setting field
        add_settings_field(
            'wcp_location', //Name of setting
            'Display Location', //HTML label text
            array($this, 'displayLocationHTML'), //Function to display custom HTML
            'word-count-settings-page', //Slug name
            'wcp_first_section' //Section name
        );

        //Registers the setting field to the WordPress settings
        register_setting(
            'wordcountplugin', //Group it belongs to
            'wcp_location', //Name of setting
            array(
                'sanitize_callback' => 'sanitize_text_field', //Validates value
                'default' => '0' //Default value
            ) //Array of options
        );

    }

    function displayLocationHTML()
    {
        ?>
            Hello
        <?php
    }
}

$wordCountAndTimePlugin = new WordCountAndTimePlugin();

?>

Thanks in advance :)


Solution

  • You have a small mistake in the hook`s name. You need to replace:

    add_action('admin-init', array($this, 'settings'));
    

    on:

    add_action('admin_init', array($this, 'settings'));