I'm overriding Woocommerce theme. And now I have to override customize panel. There is a few woocommerce default sections in Customize panel to change some Woocommerce data, labels etc. (such as image size, number of post per page, privacy text...). And I want to add my own customize panel fields to make some data in my child theme to be easy to change (they should be placed in the same control panel sections next to Woocommerce default fields).
There is Woocommerce/Includes/Customizer/*customizer files*. So I can simply override that files in Woocommerce folder but when Woocommerce will be updated I'll lose my changes.
So I need to add my customize fields (and I think the best way is write them somewhere in my child theme maybe in functions.php but I didn't manage to do it yet) in the same Woocommerce control panel tabs and sections. (For example in section Product Catalog (that's woocommerce default section) I want to make changeable my own filter label).
Is there any way to do it?
Thanks in advance))
To add your own section with settings under WooCommerce customize panel, just add follows codes snippets in your active theme's functions.php -
add_action( 'customize_register', 'my_custom_customize_register', 99 );
function my_custom_customize_register( $wp_customize ) {
$wp_customize->add_section(
'my_wc_custom_section',
array(
'title' => __( 'My Custom Section', 'text-domain' ),
'priority' => 20,
'panel' => 'woocommerce',
'description' => '',
)
);
$wp_customize->add_setting( 'my_wc_custom_section_settings', array( 'transport' => 'postMessage' ) );
$wp_customize->add_control( 'my_wc_custom_section_settings_control',
array(
'label' => __( 'Custom Text', 'text-domain' ),
'type' => 'text',
'settings' => 'my_wc_custom_section_settings',
'section' => 'my_wc_custom_section',
'priority' => 20,
)
);
}
To display your saved field's value from your custom section settings, just use follows -
echo get_theme_mod( 'my_wc_custom_section_settings' );
Thats it.
Related
I added a custom post type 'Catalog'.
This CPT makes it posible to create A list of items like a catalog.
For posts Wordpress has always a single view page on the frontend and I want remove these and show an 404 error if somebody tried visit the url.
register_post_type( 'catalog',
array(
'labels' => array(
'name' => 'Catalogus',
'add_new' => 'Nieuw item',
'add_new_item' => 'Nieuw item toevoegen',
'new_item' => 'Nieuw item',
),
'public' => false,
'show_in_rest' => false,
'menu_icon' => 'dashicons-store',
'menu_position' => 2
)
);
I set the public on false but this removes the whole CPT on the front and adminside.
Who can help me?
When you register a custom post type, a number of other values will set their defaults based on the value used for public. By setting public to false, the argument to show the UI in the admin panel will also have defaulted to false.
Pass in show_ui to control whether it's displayed in the admin panel:
register_post_type( 'catalog', [
'labels' => [
// labels...
]
'public' => false,
'show_in_rest' => false, // default is false so probably not needed
'show_ui' => true, // show the admin UI for the CPT even when public is false
'menu_icon' => 'dashicons-store',
'menu_position' => 2
] );
https://developer.wordpress.org/reference/functions/register_post_type/#public
For anyone in the community that's using the Custom Post Type's UI (CPT UI) plugin, just set "Publicly Queryable" to false. This will disable the single page for your CPT.
Any (custom) post types is ruled by the Wordpress Template Hierarchy.
As you can see here with the Visual Overview, any post type will be displayed by the following templates:
Custom Post Type
$custom.php → (fallback) single-$posttype-$slug.php → (fallback) single-$posttype.php → (fallback) single.php → (fallback) singular.php
Blog Post
$custom.php → (fallback) single.php → (fallback) singular.php
One way to remove the single custom post type display ability and keeping the query ability is to remove and add the adequate templates.
Following best practices, by removing single.php & singular.php and not including any custom templates, you will effectively remove a custom post type ability to produce a single template which will redirect the request to a 404.php and corresponding fallbacks.
To make sure our blog posts still renders and have a single post type display ability, we need to include a single-post.php which will only display theme's blog posts.
Here is a minimal graphical representation of a theme's folder with no single custom post type ability and a single blog post post ability.
myTheme/
├── style.css/
├── single-post.php/
└── index.php/
In order to simplify things, I have a social icon in my WP theme, that I want to enable users to modify via the WP customizer:
$wp_customize->add_setting(
'tcx_social_twitter',
array(
'default' => 'fa fa-twitter'
)
);
$wp_customize->add_control(
'tcx_social_twitter',
array(
'section' => 'tcx_social',
'label' => 'Social links',
'type' => 'text'
)
);
Then <?php echo get_theme_mod( 'tcx_social_twitter' ); ?> in my theme layout to show the icon to the end user. Everything works great so far.
Now I want to enable future adinistrators of my theme to add as many social icons, as they want. I believe this requires adding settings and controls dynamically, however I faild to find any information on this question in the official WP docs. Any help would be greatly appreciated!
I'd like to add 2 custom fields to the user billing information in WooCommerce, one for capturing their VAT number and one for the Chamber of Commerce number. These fields need to be displayed during checkout, on the my account > billing address page and also on the WP Admin on the user page so the admin of the website/webshop can check for these values.
I prefer not to use a plugin but to use the child theme's functions.php.
Can anyone please help me with this problem? I looked around on the Wordpress Stack exchange but couldn't find a specific and up-to-date solution for my problem. Also I read the Woocommerce documentation but there it's not explained how to show the custom billing fields on the user admin page in the backend.
Thank you very much in advance!
You can achieve this by using Woocommerce filter woocommerce_checkout_fields.
Here is the code example.
add_filter( 'woocommerce_checkout_fields','checkout_extra_fields');
function checkout_extra_fields($fields){
$fields['billing']['vat_number'] = array(
'label' => __('VAT number', 'my-slug'),
'placeholder' => __('VAT number', 'my-slug'),
'required' => false,
'clear' => false,
'type' => 'text',
);
$fields['billing']['commerce_number'] = array(
'label' => __('Commerce number', 'my-slug'),
'placeholder' => __('Commerce number', 'my-slug'),
'required' => false,
'clear' => false,
'type' => 'text'
);
return $fields;
}
Also, You can get the saved values from backend by this code
$extra_fileds_vat_number = get_post_meta( wf_get_order_id($order),'_vat_number',1);
$extra_fileds_commerce_number = get_post_meta( wf_get_order_id($order),'_commerce_number',1);
Altough I didn't want to use a plugin and Nishad's answer is absolutely correct I found a free plugin (after a really long search) that does exactly what I want. It's called Flexible Checkout Fields and you can create custom fields that show up on Checkout, Woocommerce my account page and on the WP admin user profile page. I really love it!
So for anyone who encounters the same problem, you might want to try this one!
I am using a wordpress theme that has it's own framwork which is based upon redux framework I think. I am modifying this theme using a child theme. I want to add to the Theme Options in the backend and I found a function within the files of the parent theme that seems to be exactly what I need:
/*
*
* Custom function for filtering the sections array. Good for child themes to override or add to the sections.
* Simply include this function in the child themes functions.php file.
*
* NOTE: the defined constansts for URLs, and directories will NOT be available at this point in a child theme,
* so you must use get_template_directory_uri() if you want to use any of the built in icons
*
*/
function add_another_section($sections){
//$sections = array();
$sections[] = array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
);
return $sections;
}
//add_filter('redux-opts-sections-twenty_eleven', 'add_another_section');
I have added this function to my child theme's functions.php and uncommented the add_filter. However, this does not seem to work and no new section has been added.
I came across this discussion elsewhere which suggests the name of the function needs to be changed (I was getting the same error mentioned on there). I have done that and it still won't work.
Here is what I have in my child theme functions.php
function add_another_section_bl($sections){
$sections = array();
$sections[] = array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
);
return $sections;
}
add_filter('redux-opts-sections-twenty_eleven', 'add_another_section_bl');
I wasn't sure if the filter name 'redux-opts-sections-twenty_eleven' needs editing as it mentions the twenty eleven theme. I have tried it with different theme names at the end instead of twenty_eleven and that had no effect.
Any help would be greatly appreciated! On a side note I have been able to accomplish adding new options to Theme Options by copying over the whole framwork folder into my child theme and defining the path to the framework in the child theme's functions.php. I just felt that there should be a much slicker, neater way to achieve this and I thought that function sounded perfect.
Many thanks.
Lead dev of Redux Framework here. This solution only works if you're using Redux Framework 3.1+. If you have an older version, install the Redux Framework plugin and it will override the version inside your theme.
First go to the current option panel. Open up a javascript console (use chrome or firefox) and type: redux.args.opt_name. That will echo out a name. Copy that and paste it into this function replacing OPT_NAME with the name that was echo'd out:
function add_another_section_bl($sections){
$sections = array(); // Delete this if you want to keep original sections!
$sections[] = array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
);
return $sections;
}
// In this example OPT_NAME is the returned opt_name.
add_filter("redux/options/OPT_NAME/sections", 'add_another_section_bl');
Good luck!
** UPDATE **
Also with the Redux API you can easily add a new section that way.
Redux::addSection(array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
))
That makes it a wee bit easier using our API I believe we released in Redux 3.2...
If you are using redux options with plugin your filter won't work inside a theme
for that make sure you put code inside a plugin.
Here is code that will work with theme and plugin as well!
function add_social_media_options($sections) {
$sections[] = array(
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => 'el-icon-wrench',
'title' => esc_html__('Social Media Settings', 'textdomain'),
'desc' => esc_html__('These are settings social media link', 'textdomain'),
'fields' => array(
array(
'id' => 'facebook_link',
'type' => 'text',
'url' => true,
'title' => __('Facebook link', 'textdomain'),
'compiler' => 'true',
//'mode' => false, // Can be set to false to allow any media type, or can also be set to any mime type.
'desc' => __('', 'textdomain'),
'default' => ''
),
array(
'id' => 'twitter_link',
'type' => 'text',
'url' => true,
'title' => __('Twitter link', 'textdomain'),
'compiler' => 'true',
//'mode' => false, // Can be set to false to allow any media type, or can also be set to any mime type.
'desc' => __('', 'textdomain'),
'default' => ''
),
)
);
return $sections;
}
add_filter("redux/options/redux_demo/sections", 'add_social_media_options');
How to add a new header menu in wordpress?
You may add a function to wordpress (well anywhere in your theme) using the wp_nav_menu(). This requires that you add the code below to your functions.php file (if you do not have one in your theme directory then create one):
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'menu-1' => __( 'Top menu' ),
'menu-2' => __( 'Bottom menu' )
)
);
}
Change the "Top menu" and "Bottom menu" to what ever you want the names to be. Basically you are just telling wordpress (wp 3.+) to make reservations for these menu in your theme. If you want more you just need to define the names in a new line. Save that.
That done you will need to add the menu in your site template. Usually I define the arguments first before I call the wp_nav_menu() function. Read up on the arguments here http://codex.wordpress.org/Function_Reference/wp_nav_menu
Hope that helps.
You can manage the menus in your WordPress dashboard under "Appearance => Menus". Here is a tutorial: http://en.support.wordpress.com/menus/