How to make Titan Framework text field required - wordpress

I am using Titan Framework with a custom plugin I wrote. There is a settings page that has a tab and on it there is a text field:
array(
'name' => 'Slug',
'id' => 'my_slug',
'type' => 'text',
'default' => 'my-slug',
'desc' => 'The slug.',
),
This text field allows the user to specify a slug for the plugin's frontend UI. The problem is that a user can clear this field out to nothing and then save settings which results in an error.
I understand that I can trap this condition in the PHP code when this setting option is used. I also understand this could be done using jQuery to enforce a value being specified, but it would seem like there should be a best practice solution.
Can anyone tell me the proper way to ensure this field has a value?

The user can definitely save it as empty and yes you can prevent that with JS. But because there is no "pre save" filter, one way to address this is to to the method you mentioned, add a placeholder attribute to the field to give the illusion of a value when empty (this is just for a better user experience).
array(
'name' => 'Slug',
'id' => 'my_slug',
'type' => 'text',
'default' => 'my-slug',
'desc' => 'The slug.',
'placeholder' => 'post',
),
Then check for empty when getting the value.
$slug = $titan->getOption( 'my_slug' );
if ( empty( $slug ) ) {
$slug = 'post';
}

Related

Woocommerce custom attributes not created, wc_create_attribute still working?

Is there something i am missing?
I try to create a new custom attribute for plugin purpose, but somehow it doesnt work.
The simple function is as follows:
$data = array(
'name' => 'programmas',
'slug' => 'elearning_programmas',
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => 1
);
wc_create_attribute( $data );
However, whatever i do, no new attribute is created in the list with attributes under the product attributes page, neither available on the individual product page.
Any ideas?

Is there custom validation of meta values in Wordpress REST API?

WordPress allows registering meta values in the REST API through register_meta().
One can add a schema to the meta like so:
register_meta(
'user',
'my_meta_key',
array(
'type' => 'array',
'description' => 'This is my array meta key!',
'single' => true,
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
'format' => 'date-time',
),
),
),
)
);
It took me some time to find out about the 'format' => 'date-time' option.
I would like to validate my meta fields myself (for a custom post-type). But it seems like the WP_REST_Posts_Controller uses get_endpoint_args_for_item_schema() to set up the schema for arguments in register_rest_route().
register_rest_route() would allow for a custom validate_callback, but rest_get_endpoint_args_for_schema() hard-codes it to rest_validate_request_arg(), which uses rest_validate_value_from_schema().
And there are the primitives and hard-coded formats hex-color, date-time, email, ip and uuid.
So there are two options I see:
Live with it. Just accept the validating against schema primitives, since the schema is only validated on REST requests.
Extend WP_REST_Posts_Controller just to sneak in my own get_endpoint_args_for_item_schema(), which adds an exception for my meta key.
Do you have another idea, how to solve this issue?
EDIT: There seems to be a pattern property for strings. So I could at least do something like (don't mind the regex itself):
'schema' => array(
'type' => 'string',
'pattern' => '[0-9]/[0-9]^$',
),

How to add a custom Rules "action" in Drupal?

I'm using the Rules module in Drupal 7, and I tried to add a new Rules "Action"
I followed the steps described in How to create an custom rule action using hook_rules_action_info? to create a custom rule action using hook_rules_action_info:
I tried to create a sample file (save_nid.rules.inc) in the folder /rules/module/.
And I also tried to create a module folder in /site/all/module/save_nid/.
My code in save_nid.rules.inc looks like so:
function rules_save_nid_action_info() {
return array(
'save_nid_action' => array(
'label' => t('Custom Action'),
'parameter' => array(
'param1' => array(
'type' => 'int',
'label' => t('Parameter Label'),
),
'param2' => array(
'type' => 'commerce_order',
'label' => t('Parameter Label2'),
),
),
'configurable' => FALSE,
'group' => t('ABC Custom module action'),
'callbacks' => array(
'execute' => 'abc_custom_action',
),
),
}
After I cleared the Drupal cache, I didn't see my "custom rule" in the list.
What am I missing or doing wrong?
In your case, the machine name of your custom module seems to be save_nid.
So if you want to use hook_rules_action_info in your module, your function needs to be named save_nid_rules_action_info, instead of rules_save_nid_action_info.
Not sure if it is the "only" problem why you can't get it to work, but at least it is "a" problem you need to address.
PS: make sure to save that custom coding in your custom module's folder (in /site/all/module/save_nid/).

Creating custom options for "List-item" option in Option Tree plugin for wordpress

I have a problem, I need to change the default options for a “List-item” option in Option tree . Right now the default are “Title/Image/Link/Descriptions” .. I want to remove them and add my own. I have written this code:
array(
'id' => 'academic_success_content',
'label' => 'Academic Success Content',
'desc' => 'Enter the academic success content. It will appear in the home page in list items format',
'std' => '',
'type' => 'list-item',
'section' => 'academic_perfomance',
'settings' => array(
array(
'id' => 'academic_success',
'label' => 'Academic Success',
'type' => 'textarea-simple',
)
)
),
But when I preview the themes options , the default list item "title" is still there and I only want to see the Academic Success textarea. What should I do?
I also suffered from the same situation.
And after that, I use the older version of "list-item" and "gallery".
This has worked well. Ex: v2.4.6
Is the theme options page being included?
You'll need to reference it in your functions.php, something to the effect of:
require_once locate_template('/path-to-your/theme-options.php' );

Drupal 7 - how to allow file extensions for field

After creating multiple fields with the help of the installation profile I am now also trying to figure out how I can add the allowed file extensions in the installation profile. Here is my code so far:
'name' => 'attachment',
'type' => 'file',
'settings' => array(
'allowed_file_extensions' => array('txt pdf'),
),
The allowed_file_extensions was just a test to see if it works but it doesn't it gives an fatal PHP error. (** PHP Fatal error: Unsupported operand types** )
How can I add the file extensions at the field?
PS: I also tried putting the settings in the instance, this gives the same error.
Oke, I was having this problem for the last three days. Now after a few hours after I posted this question I solved the problem.
For those who have the same question or problem here is what solved it for me and what the right way is to add settings. The settings can be different for each instance so the settings go at the instance creation and not the field itself.
Here is an example on how it is supposed to be:
$instance = array(
'field_name' => 'file'
'entity_type' => 'node',
'bundle' => 'article',
'label' => 'Attachment',
'description' => 'Add an attachment here',
'required' => TRUE,
'settings' => array(
'max_filesize' => '512',
'file_extensions' => 'zip txt pdf docx'
),
);
field_create_instance($instance);
The settings field is not required but i was using an foreach because I generate multiple fields and instances at once, if you do this and you have settings at 1 of your generated field instances then you have to add settings at all instances or check if it is there or not.
I hope my experience can help any of you out when having the same problem.

Resources