DISQUS

Fun with WordPress: Adding settings to admin pages

  • ala_747 · 8 months ago
    That's awesome! Thank you for sharing!!

    But I'm trying to figure out how to pass an argument to the callback function. Actually, I don't know if it could be done :(

    Eg.


    $as = array('A', 'B', 'C', 'D');
    $adb = null;

    function my_new_settings(){
    global $as;
    foreach($as as $a):

    $adb = sanitize_title($a);

    register_setting('media',"{$adb}_1");
    register_setting('media',"{$adb}_2");

    add_settings_field("{$adb}" , "{$a} setting:" ,
    'my_new_callback' , 'media' , 'default');

    endforeach;
    }

    function my_new_callback(){
    global $adb;

    echo "<label for=\"{$adb}_1\">Setting 1</label>
    <input name=\"{$adb}_1\" id=\"{$adb}_1\" value=\"".attribute_escape(get_option("{$adb}_1"))."\" class=\"small-text\" type=\"text\">
    <label for=\"{$adb}_2\">Setting 2</label>
    <input name=\"{$adb}_2\" id=\"{$adb}_2\" value=\"".attribute_escape(get_option("{$adb}_2"))."\" class=\"small-text\" type=\"text\">
    ";
    }

    add_action('admin_init', 'my_new_settings');


    Maybe I'm missing something? Thanks in advance!
  • Andrew Rickmann · 8 months ago
    What I usually do is to make sure the plugin and all its functions are contained within a class and then assigned the values to the class so the callback function can access them by using $this->myVar i.e.

    class myPlugin
    {

    private $myVar = 10;

    function myCallBack(){

    $newVar = $this->myVar;

    }


    }
  • ala_747 · 8 months ago
    Thank you very much for your answer :)

    I've tried to do this but $this->adb value is always returning 'd' (the last item of my $as array).

    I think I'll need to do it the old way (a new admin page) :(

    Again, thanks for your time!