Is it possible to send several arguments when calling array($this, ‘function’)?

I’m creating a WordPress plugin where I want some settings applied. Each setting has very similar functionality so my thought was to use same function when validating and change object so some attributes of the object can be accessible:

//Width and margins section
add_settings_section(
    'pdfcrowd_section_dimension', // ID
    'PDF widths and margins', // Title
    array( $this, 'dimension_section_info' ), // Callback
    'pdfcrowd_options' // Page
); 

$this->field_id = 'pdfcrowd_pdf_width';
$this->field_type = 'number';        
add_settings_field(
    $this->field_id, // ID
    'PDF with in units', // Title 
    array( $this, 'sanitizefield' ), // Callback
    'pdfcrowd_options', // Page
    'pdfcrowd_section_dimension' // Section           
);   

$this->field_id = 'pdfcrowd_pdf_height';
$this->field_type = 'number';        
add_settings_field(
    $this->field_id , // ID
    'PDF page height in units. -1 for a single page PDF.', // Title 
    array( $this, 'sanitizefield' ), // Callback
    'pdfcrowd_options', // Page
    'pdfcrowd_section_dimension' // Section           
    );            

$this->field_id = 'pdfcrowd_pdf_margin_top';
$this->field_type = 'number';        
add_settings_field(
    $this->field_id, // ID
    'Top PDF page margin in units.', // Title 
    array( $this, 'sanitizefield' ), // Callback
    'pdfcrowd_options', // Page
    'pdfcrowd_section_dimension' // Section           
);        

There are a lot more settings, but my thought is to have sanitizefield as a callback function for each setting, and then change field_id and field_type of the object. The problem is that the values of field_id and field_type are those set last in the code.

Read More

So my question is: Is it possible to send field_type and field_id somehow and use the callback function sanitize. Or should I really have to do one callback function for each option setting?

$this->field_id = 'pdfcrowd_pdf_width';
$this->field_type = 'number';        
add_settings_field(
    $this->field_id, // ID
    'PDF with in units', // Title 
    array( $this, 'sanitizefield1' ), // Callback
    'pdfcrowd_options', // Page
    'pdfcrowd_section_dimension' // Section           
);   

$this->field_id = 'pdfcrowd_pdf_height';
$this->field_type = 'number';        
add_settings_field(
    $this->field_id , // ID
    'PDF page height in units. -1 for a single page PDF.', // Title 
    array( $this, 'sanitizefield2' ), // Callback
    'pdfcrowd_options', // Page
    'pdfcrowd_section_dimension' // Section           
    );  

etc

Related posts

1 comment

  1. You can pass arguments to the callback using the function itself, you are leaving out the $args

    add_settings_field(
        $this->field_id , // ID
        'PDF page height in units. -1 for a single page PDF.', // Title 
        array( $this, 'sanitizefield2' ), // Callback
        'pdfcrowd_options', // Page
        'pdfcrowd_section_dimension', // Section
        array( 'key'=> 'variable')
    );  
    

    now this is where i might be not understanding you. You want the data to be accessible outside of the callback?

    You wont be able to change the variables with much success but you could create a custom filter and use that to make changes

    public function sanitizefield2($array){
         $array= apply_filters('hook_name', $array);
    }
    

    and hook into it in the usual way

    add_filter('hook_name', 'function');
    

Comments are closed.