I am using woocommerce plugin. Idea was to edit checkout page to create my own classes , placeholders etc for fields. With some googling I managed it all. Only thing that I can't change is country dropdown/select. For example having this code
$fields['shipping']['shipping_country'] = array(
'type' => 'select',
'label' => 'Country',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'class' => array(),
'label_class' => array('col-md-6'),
'input_class' => array('col-md-6'),
'return' => false,
'options' => array( 'Germany' => 'Germany',
'Germany' => 'Germany'),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
);
I want to dynamically load all countries from woocommerce. I don't want to add country by country in array, dont think thats the right way also. So is there some object or whatever that will load in this 'options' array all available countries?
You aren't very specific - you haven't shared where this code exists in your system, or which countries you want, but the answer to your question is yes.
Check out these two functions:
WC()->countries->get_shipping_countries();
WC()->countries->get_allowed_countries();
NOTE:
You ALSO probably want to be using the woocommerce_form_field for this, since it does all the heavy lifting for you.
Related
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]^$',
),
I'm using the Kirki plugin to add fields and sections in the WordPress customizer. So far, I can get a field to be added to the customizer, but I'm confused on how to return that data back into my theme. I'm kinda tired so I might be missing something. This is what I have so far:
Kirki::add_config('theme_config_id', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_config_id', [
'type' => 'editor',
'settings' => 'my_setting',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
]);
I was reading on trying to the values out from here using this:
$value = Kirki::get_option( $config_id, $option_id );
But I'm not sure where (or what) the $config_id or $option_id would be?
I have a feeling that I'm missing something and I've read through the docs and I feel that I'm not getting it.
This is the correct code
$value = Kirki::get_option( 'config_id', 'option_id' );
so in your case,
$value = Kirki::get_option( 'theme_custom', 'footer_content' );
After digging through the internet a little bit, I was able to read through some more documentation as well as some other examples and I was able to figure out what I was doing wrong. Overall, I was close, but I ended up cleaning it up and just using the WordPress get_theme_mod() outright in my template file (in this case it was the footer.php file).
Here's what I ended up with:
Kirki::add_config('theme_custom', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod'
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_custom', array(
'type' => 'editor',
'settings' => 'footer_content',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
'priority' => 10
));
and in my footer.php file, I added this:
<?php $value = get_theme_mod('footer_content', ''); ?>
<?php echo($value); ?>
Granted, this is a super basic way of implementing this. I'm going to try and figure out how to get it to refresh the customizer preview before you publish it. But for now, this seems to be working.
I'm using Business Lounge theme by RT themes with Elementor.
Wordpress version is current (5.2.1)
On the team page (Demo: https://businesslounge-demo.rtthemes.com/our-team/) there is a list of cards of team members. I want to change the order of the team members to an option that is not currently selectable.
The team member list is done with a shortcode [staff_box]
In Elementor edit mode I looks like this:
Edit:
The edit form is defined in
wp-content/plugins/businesslounge-extensions/inc/elementor-addons/staff.php
<?php
namespace Elementor;
// ...
class Widget_RT_Staff extends Widget_Base {
// ...
protected function _register_controls() {
// ...
$this->add_control(
'list_orderby',
[
'label' => esc_html_x( 'List Order By', 'Admin Panel','businesslounge' ),
'description' => esc_html_x('Sorts the posts by this parameter', 'Admin Panel','businesslounge' ),
'type' => Controls_Manager::SELECT,
'default' => "date",
"options" => array(
'date' => esc_html_x('Date',"Admin Panel","businesslounge"),
'author' => esc_html_x('Author',"Admin Panel","businesslounge"),
'title' => esc_html_x('Title',"Admin Panel","businesslounge"),
'modified' => esc_html_x('Modified',"Admin Panel","businesslounge"),
'ID' => esc_html_x('ID',"Admin Panel","businesslounge"),
'rand' => esc_html_x('Randomized',"Admin Panel","businesslounge"),
)
]
);
// ...
}
// ...
}
Plugin::instance()->widgets_manager->register_widget_type( new Widget_RT_Staff() );
The edit form is defined in `wp-content/plugins/businesslounge-extensions/inc/editor/staff_box.php`
like so
<?php
vc_map(
array(
'base' => 'staff_box',
'name' => _x( 'Team', 'Admin Panel','businesslounge' ),
'icon' => 'rt_theme rt_team',
'category' => array(_x( 'Content', 'Admin Panel','businesslounge' ), _x( 'Theme Addons', 'Admin Panel','businesslounge' )),
'description' => _x( 'Displays team members', 'Admin Panel','businesslounge' ),
'params' => array(
// ...
array(
'param_name' => 'list_orderby',
'heading' => _x( 'List Order By', 'Admin Panel','businesslounge' ),
"description" => _x("Sorts the posts by this parameter",'Admin Panel','businesslounge'),
'type' => 'dropdown',
"value" => array(
_x('Date','Admin Panel','businesslounge') => 'date',
_x('Author','Admin Panel','businesslounge') => 'author',
_x('Title','Admin Panel','businesslounge') => 'title',
_x('Modified','Admin Panel','businesslounge') => 'modified',
_x('ID','Admin Panel','businesslounge') => 'ID',
_x('Randomized','Admin Panel','businesslounge') => 'rand',
),
'save_always' => true
),
// ...
The output is defined in
wp-content/plugins/businesslounge-extensions/inc/shortcodes/staff_box.php
like so:
<?php
function rt_staff( $atts, $content = null ) {
// ...
//defaults
extract(shortcode_atts(array(
"id" => 'staff-'.rand(100000, 1000000),
"class" => "",
"list_layout" => "1/1",
"list_orderby" => "date",
"list_order" => "DESC",
"ids" => array(),
"box_style" => ""
), $atts));
// ...
//general query
$args=array(
'post_status' => 'publish',
'post_type' => 'staff',
'orderby' => $list_orderby,
'order' => $list_order,
'showposts' => 1000
);
// ...
$theQuery = query_posts($args);
// ...
What I want to do:
Add an option 'post_name' to the select box so that I can sort the team by a different field. I want to add
'Post name' => 'post_name',
How can I do this without changing the original source code?
I already have the child theme of business_lounge theme activated.
Do I need a custom extension for this?
You'd have to modify the original code.
You can't override the function unless the author applied a filter to the value returned, or used an action.
As gael notes in his answer, you can soften the blow of losing changes on updates by copying the original code into your child theme's functions.php then renaming the function - for e.g. to my_rt_staff() before adding in your modifications.
You would however still need to call the my_rt_staff() in the plugin instead of rt_stuff and you would have to make this change whenever the plugin was updated, but you wouldn't lose your code.
(Perhaps you could change the "list_orderby" => "date" in the default shortcode attributes to "list_orderby" => "post_name", as default in your modified my_rt_staff() method so it orders by name as default instead of date)
However, this does not help much in your specific circumstance, as the ideal modification you need to make is to the control itself, on the _register_controls() method in the Widget_RT_Staff class. You can override this by extending Widget_RT_Staff but you would still need to call your new class which results in you modifying the plugin code.
Without seeing how the Widget_RT_Staff class affects the shortocde, I can't be certain this would work, but based on the rt_staff() method, if you use the shortcode as [staff_box orderby="post_name"] you may get your intended result without having to touch any code.
You should be able to modify your plugin without losing update abilities by 'overriding' the function as described here:
https://stackoverflow.com/a/40856197/3623080
Make a copy of the function in your child-theme's functions.php, customize and rename it, use it in place of the original function.
Hope it will help
I'm creating a custom module which enables me to add Countries to a custom table in the database. I'll do later more with it, but as I got stuck at the beginning, I can't go on.
First my piece of code:
function partners_schema()
{
$schema['partners_country'] = array(
'description' => 'TODO: please describe this table!',
'fields' => array(
'id' => array(
'description' => 'auto inc id of country',
'type' => 'serial',
'not null' => true,
),
'name' => array(
'description' => 'name of country',
'type' => 'varchar',
'length' => '255',
'not null' => true,
'translatable' => true,
),
'needDistributor' => array(
'description' => 'is a distributor needed',
'type' => 'int',
'size' => 'tiny',
'not null' => true,
),
),
'primary key' => array('id'),
);
return $schema;
}
The code above generates my Database table. After searching I found that I can add 'translatable' => true to my schema, so Drupal knows that this field is translatable content.
I've added a form, to insert data into that schema, but now I got stuck. What do I have to do, that the user is able to translate the column name?
Thanks for your help.
Have a look at the accepted answer on this post it should help clear a few things up.
Is it possible to declare and manage several custom content types inside one module? I'm creating a site that needs four custom content types and I'd like to manage them from one module instead of creating module for every content type. After some testing, I found out that it seems impossible. Because, unless hook_form and content type share the same name of module, drupal doesn't call hook_form.
Here's how I'd like to do -
function mycontent_node_info(){
return array(
'mycontent1' => array(
'name' => t('....'),
'module' => 'mycontent',
'description' => t('...),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('content body'),
),
'mycontent2' => array(
.......
),
'mycontent3' => array(
......
),
'mycontent4' => array(
......
),
);
}
function mycontent1_form(&$node){
$form['control1'] = array(
'#type' => 'select',
'#options' => array(
'0' => t('selection 1'),
'1' => t('selection 2'),
),
'#attributes' => array('id'=>'control1'),
);
$form['control2'] = array(
'#type' => 'select',
'#options' => array(
'0' => t('1'),
'1' => t('2'),
'2' => t('3'),
'3' => t('4'),
),
'#attributes' => array('id'=>'control2'),
);
return $form;
}
function mycontent2_form(&$node){
....
}
function mycontent3_form(&$node){
....
}
function mycontent4_form(&$node){
....
}
Am I doing something wrong here or is not possible and there's no alternative other than creating module for every content types. I appreciate much your help.
The prefix for all your hooks should be the name of your module, i.e. mycontent_node_info() and mycontent_form(&$node). I think the content type itself can be called whatever you want but by convention anything global that you define in a module should be prefixed with the name of the module to avoid namespace problems. So your content becomes mycontent_type1, mycontent_type2, etc... As for dealing with hook_form, I guess the way to do it is to check the type of the node passed in and act accordingly.
You might try using the Features module (http://drupal.org/project/features) to export your content types. It auto generates the code to make this work, and you can have a look at what is going wrong with your code.