Add_action not calling callback function

Question

I am currently trying to build a plugin for wordpress to build a settings page.
But for some reason when I use add_action("admin_init", array(class_object, 'function_name')) it will run the add_action function but won’t get to the callback.
It just skips it.

I have tried making a test class with 1 function in it the same way as the main class and that does work.
So I am doing something different somewhere but I don’t know what.

here is the code.
I have split the code over 3 different files and I will post it in order of including the files.

Index file for the plugin

include('admin/options.php');


function social_share_menu_item() {
    add_submenu_page("options-general.php", "Social Share", "Social Share", "manage_options", "social-share", 'socialShareOptions');
}
add_action("admin_menu", "social_share_menu_item");

...

options.php

include_once('settings/SocialOptions.class.php');

function socialShareOptions(){

    $options = array(
        array(
            'type'  => 'checkbox',
            'name'  => 'social-share-facebook',
            'value' => 1,
            'title' => 'Show Facebook '
        ),
        array(
            'type'  => 'checkbox',
            'name'  => 'social-share-twitter',
            'value' => 1,
            'title' => 'Show Twitter'
        ),
        array(
            'type'  => 'checkbox',
            'name'  => 'social-share-instagram',
            'value' => 1,
            'title' => 'Show instagram'
        )
    );

    $socialOptions = new SocialOptions($options);
    $socialOptions->buildSettingsPage();
    add_action("admin_init", array($socialOptions, 'setOptions') );
}

SocialOptions.class.php

class SocialOptions {

    private $_optionsArray;

    function __construct( $options = array() ) {

        $this->_optionsArray = $options;

    }

    public function setOptions() {

        add_settings_section("social_share_config_section", "", null, "social-share");

        foreach ( $this->_optionsArray as $option) {
            add_settings_field("social-share-facebook", $option['title'], array($this, 'option'), "social-share", "social_share_config_section", $option);
        }
        foreach ( $this->_optionsArray as $option) {
            register_setting("social_share_config_section", $option['name']);
        }
    }

    public function option( $fieldData ){
        ?>
        <input type="<?php echo $fieldData['type'] ?> " name="<?php echo $fieldData['name']; ?>" value="<?php echo $fieldData['value'] ?>"<?php checked(1, get_option($fieldData['name']), true)?> /><?php echo $fieldData['title'] ?>
        <?php
    }


    public function buildSettingsPage() {

        $html = '<div class="wrap social-admin-style">

                    <div class="title">
                        <h1>Social Sharing Options</h1>
                    </div>

                    <div class="main-content">

                        <form method="post" action="options.php">

                            ' . settings_fields("social_share_config_section") . ' 

                            ' . do_settings_sections("social-share") . '

                            '. submit_button() . '

                        </form>

                    </div>

                </div>';

        echo $html;

    }


}

As state before when debugging the code I see that the add_action function runs but at some point it just skips tot the end of the function and never enters the setOptions method.

when I create the following it works fine:

options.php

include_once('settings/SocialOptions.class.php');

class Test {
    function testing(){
     return 'nothing';
    }
}

function socialShareOptions(){

    $options = array(
        ...
    );

    $socialOptions = new SocialOptions($options);
    $socialOptions->buildSettingsPage();

    /*NOTE THIS PART*/
    $test = new Test();
    add_action("admin_init", array($test, 'testing') );
}

This will, in debug, run just fine.
That is why I don’t get why the other method isn’t working.

Additional information

tipped by B. Schuiling
In de php error logs I don’t get any kind of error.

Hope you guys can help.
And thanks for taking the time reading this.
IF you need any more info please let me know.

EDIT
Though the original answer wasn’t what the fix was I would like you all to refer to the asnwer by B. Schuiling.
In the comments he explained that the add_action was called to late and therefore wasn’t run.

below is the altered options.php page.(i left the obviouse code out to make it shourter)

ps though this does work I am going to change the code to a singleton way.
This also based on the link that B. Schuiling posted in the comments on his own answer below.

Notice that the options array and the instatiation of the class is now outside the socialShareOptions function.

include_once('settings/SocialOptions.class.php');

$options = array(
    ...
    )
);
$socialOptions = new SocialOptions($options);
add_action("admin_init", array($socialOptions, 'setOptions'));

function socialShareOptions(){
    global $socialOptions;

    $socialOptions->buildSettingsPage();
}
0
, , , Controvi 1 year 2020-07-14T21:10:27-05:00 0 Answers 63 views 0

Leave an answer

Browse
Browse