How to: required for a custom field?
Redux: v 3.5 WordPress: 4.1.1
Hey guys, I used the extension boilerplate and loader to make a custom field and everything is working great! Just wondering how I can get the "required" option to work. My field is basically a raw field without a table. Just like how the info field works. If anyone could just point me in the right direction, that'd be great.
Thanks!
The code:
File: field_custom_field.php
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;
// Don't duplicate me!
if( !class_exists( 'ReduxFramework_custom_field' ) ) {
/**
* Main ReduxFramework_custom_field class
*
* @since 1.0.0
*/
class ReduxFramework_custom_field extends ReduxFramework {
/**
* Field Constructor.
*
* Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function
*
* @since 1.0.0
* @access public
* @return void
*/
function __construct( $field = array(), $value = '', $parent ) {
$this->parent = $parent;
$this->field = $field;
$this->value = $value;
if ( empty( $this->extension_dir ) ) {
$this->extension_dir = trailingslashit( str_replace( '\\', '/', dirname( __FILE__ ) ) );
$this->extension_url = site_url( str_replace( trailingslashit( str_replace( '\\', '/', ABSPATH ) ), '', $this->extension_dir ) );
}
// Set default args for this field to avoid bad indexes. Change this to anything you use.
$defaults = array(
'class' => '',
'content' => '',
'required' => array()
);
$this->field = wp_parse_args( $this->field, $defaults );
}
/**
* Field Render Function.
*
* Takes the vars and outputs the HTML for the field in the settings
*
* @since 1.0.0
* @access public
* @return void
*/
public function render() {
echo '</td></tr></table><div id = "' . $this->field['id'] . '" class="' . $this->field['class'] . ' redux-field-' . $this->field['type'] . '">';
// HTML output goes here
echo $this->field['content'];
echo '</div><table class="form-table no-border" style="margin-top: 0;"><tbody><tr style="border-bottom:0; display:none;"><th style="padding-top:0;"></th><td style="padding-top:0;">';
}
}
}
You can't, not without hacking the Redux core, which we had to do for raw, info and divide.
Alright, thanks. Custom Javascript will do then.
Instead of hacking around, with Redux 3.5.0 just do this.
In your render DO NOT escape the table. IE, just put your content in there.
Then if you want full width, pass the full_width argument.
All should work even with required. :)
Thanks for the suggestion Dovy.
I ended up just using the custom field, with escaping the table. The reason is that I need pure raw HTML, instead of it being wrapped in a <tr> and <fieldset> which have their own css styles that I couldn't get rid of in a clean way. IE, the border-bottom on the <tr>.
Is there anything wrong with escaping the table? I just followed what the info field does.
Once again, thanks for always responding quickly and being a great help! :)
Nothing wrong. It just breaks required. ;)
Mainly you need the fieldset with the proper Id and classes for it to all work again.
Ok awesome!