0

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();
}
  • I don't see anything obivous, so I'd personally go look at WP_Hook::apply_filters (class-wp-hook.php) and start to debug there. – janh Nov 3 '17 at 8:51
  • @janh thank you for the tip. Going to look at that right away. – Controvi Nov 3 '17 at 10:33
  • Add a debugging point inside setOptions() or add a die() inside setOptions() to test if the method is really getting called or not. – CodeMascot Nov 3 '17 at 10:41
0

In your class, your function is called 'public function setOption()' not setOptionS . Check your error log for this kind of issues.

Also in your code I don't see the function 'socialShareOptions' actually being called anywhere.

  • I can give you de error of the setOptions. This is something I typed in wrong when I was writing the question. in my code it is correct and it is still not working. ps. i will add this to the answer but php and apache error logs give me no errors what so ever. The other point you are making is you probably not looking at the code well enough. check the first code and scroll all the way to the right. There you can see the function being linked to the sub_menu_page function – Controvi Nov 3 '17 at 10:32
  • 1
    Ah right. So first you hook the menu page with 'admin_menu' link and then you try to hook these options with admin_init. That's not going to work since admin_init is earlier in the list than admin_menu, so it will not fire. You need to separate these two functions basically. – B. Schuiling Nov 3 '17 at 10:59
  • ah ok that sounds logical. I'll be trying that. thanks for taking a look at it again :) – Controvi Nov 3 '17 at 11:04
  • If you want to do it class based it's often better to just init the class without hooks ( aka very early ) and in the construct add all the hook you need, instead of making this kind of hard-to-understand chains. Very useful is this approach - code.tutsplus.com/articles/… – B. Schuiling Nov 3 '17 at 11:23
  • thanks for the link. will definitely read it and see how I can / will use that. – Controvi Nov 3 '17 at 11:30

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.