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.
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
You can pass arguments to the callback using the function itself, you are leaving out the
$args
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
and hook into it in the usual way