Adding User Profile Fields to Wordpress Buddypress to Include "Favorites" - wordpress

I'm trying to figure out a way for members on my Wordpress (Buddypress) site to pick their "favorite movies, books, etc."
It would be nice if, instead of members simply typing a list of these things, they could select from books already in the system, and add more as the please in the future.
I'm hoping that there is an easy answer to this, such as a plugin that I can use, or, at least, modify. Does anyone know of anything that I can look into?

Your title asks how to add a user profile field. Here is the code to add as many fields as you like. Once you add the field, you can easily place additional inputs or options on the custom tab page for users to enter their own favorites.
function my_test_setup_nav() {
global $bp;
$parent_slug = ‘test’;
$child_slug = ‘test_sub’;
//name, slug, screen, position, default subnav
bp_core_new_nav_item( array(‘name’ => __( ‘Test’ ),’slug’ => $parent_slug,’screen_function’ => ‘my_profile_page_function_to_show_screen’,'position’ => 40,’default_subnav_slug’ => $child_slug ) );
/* Add the subnav items to the profile */
// name, slug, parent_url, parent slug, screen function
bp_core_new_subnav_item( array( ‘name’ => __( ‘Home’ ), ‘slug’ => $child_slug, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen’ ) );
bp_core_new_subnav_item( array( ‘name’ => __( ‘Random Page’ ), ‘slug’ => ‘random’, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen234′ ) );
}
function my_profile_page_function_to_show_screen() {
//add title and content here – last is to call the members plugin.php template
add_action( ‘bp_template_title’, ‘my_profile_page_function_to_show_screen_title’ );
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen_title() {
echo ‘wptaskforce title’;
}
function my_profile_page_function_to_show_screen_content() {
echo ‘wptaskforce content’;
}
//random page content:
function my_profile_page_function_to_show_screen234() {
//add content here – last is to call the members plugin.php template
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen234_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen234_content() {
echo ‘This is a random page.’;
}
add_action( ‘bp_setup_nav’, ‘my_test_setup_nav’ );

Related

How to use Product Archive page as home page?

I would like the page example.com/product-category/tyres to be the home page. When anyone lands on example.com it will show the page example.com/product-category/tyres
Before I attempted to do so, I thought it is a straightforward task but it doesn't appear to be so. There is no option to set up product category page as static post or home page.
Tried the following:
[product_category ids="123"]
add_action( 'customize_register', 'th_customize_register' );
function th_customize_register($wp_customize) {
// Add: Drop Down Pages
$wp_customize->add_setting( 'th_portfolio_page_id', array (
'sanitize_callback' => 'absint',
) );
$wp_customize->add_control('th_portfolio_page_id', array(
'label' => esc_html__('Portfolio Page', 'themeshash'),
'description' => esc_html__( 'You must have to define it if you want to set your portfolio as your homepage.', 'themeshash' ),
'section' => 'title_tagline',
'type' => 'dropdown-pages',
) );
}
add_action( 'customize_register', 'th_customize_register' );
if ( get_option('page_on_front') == get_theme_mod('th_portfolio_page_id') ) {
add_action("pre_get_posts", "th_assign_portfolio_page");
function th_assign_portfolio_page($wp_query){
//Ensure this filter isn't applied to the admin area
if(is_admin()) {
return;
}
if($wp_query->get('page_id') == get_option('page_on_front')):
$wp_query->set('post_type', 'tyres');
$wp_query->set('page_id', ''); //Empty
//Set properties that describe the page to reflect that
//we aren't really displaying a static page
$wp_query->is_page = 0;
$wp_query->is_singular = 0;
$wp_query->is_post_type_archive = 1;
$wp_query->is_archive = 1;
endif;
}
}

Custom button in Wordpress Dashboard

We would like to create a custom button in the wordpress dashboard,
We want this button to hold our pages we create for clients,
Because I want to keep the pages section separate from our "Client" pages
i hope this makes sense
It's really quite straight forward, just takes a bit of getting used to.
You ideally need to follow the docs:
https://codex.wordpress.org/Adding_Administration_Menus
https://developer.wordpress.org/reference/functions/add_menu_page/
This will guide you in how to set up an admin menu.
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
Will be the code you call when creating the admin sidebar menu item, however, there are more functions for create sub menu pages under it etc.
What I think you are looking for though, is a Custom Post Type for your clients...
https://codex.wordpress.org/Post_Types
function create_post_type() {
register_post_type( 'client_posttype',
array(
'labels' => array(
'name' => __( 'Clients' ),
'singular_name' => __( 'Client' )
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'create_post_type' );
Something similar to above will get you started.
If it's all a bit much, you can generate CPT code:
https://generatewp.com/post-type/
Then have a look at the generated code to work out what it is that you would have coded yourself.
Test this :)
class options_page {
function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
function admin_menu() {
add_menu_page( 'Clients', 'Clients Page', 'edit_posts', 'clients_page', 'my_clients', '', 24);
}
function settings_page() {
echo 'This is the page content';
}
}
new options_page;

Add secondary attributes tab to woocommerce

I need to add another tab to product tabs section of woocommerce with the name of features and it's content fills just like attributes section. the source be same but data store in separate meta data and show it in another tab in product page view section.
any Idea how should to do this?
thank you in advance
Here is my code
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
$tabs['featurestab'] = array(
'title' => __( 'Features', 'woocommerce' ),
'priority' => 15,
'callback' => 'features_tab_content');
return $tabs;
}
function features_tab_content() {
echo 'Your product ('.get_the_ID().') features.';
}

Change custom post type slug from Settings > Permalinks page

add_action( 'init', 'register_my_types' );
function register_my_types() {
register_post_type( 'movies',
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'rewrite' => array(
'slug' => 'films'
),
)
);
}
I can use the following to change the slug from my plugin settings page (on save to get 'filme' from a field):
add_filter( 'register_post_type_args', 'movies_register_post_type_args', 10, 2 );
function movies_register_post_type_args( $args, $post_type ) {
if ( 'movies' === $post_type ) {
$args['rewrite']['slug'] = 'filme';
}
return $args;
}
But, I want to be able to modify the "films" slug from Settings > Permalinks page.
How do I add a custom post type slug to Settings > Permalinks page?
Update:
In the end I created a form field in the plugin settings page and I updated the filter like this:
function register_post_type_args( $args, $post_type ) {
if ($this->plugin_name === $post_type ) {
$slug=get_option( $this->plugin_name.'_slug' );
if($args['rewrite']['slug']!=$slug){
$args['rewrite']['slug'] = $slug;
}
}
return $args;
}
But I'm still looking for a way to change the slug from Settings / Permalinks.
You can use plugins like this:
Toolset Types
Custom Post Type UI
You should use WordPress Settings API:
Add a setting field for your slug, attached to the permalinks page with add_settings_field()
Write the callback function to display the <input> fied in the page
Write the function that record the value in the database after the form submission with update_option()
All you have to do then is modify your register_post_type() function. The get_option() function will return the value that has to be affected to slug (don't forget to test its existence).

Add Tax Exempt form on checkout in woocommerce

I am trying to add a form to my checkout page so when a user clicks the 'Tax Exempt' checkbox, a textbox will popup and ask the user what the Tax Exempt Id number is.
I got all of that working great, and I even added the update_totals_on_change class to my form field so it will update the totals.
My next step was to add an action/filter on a method so when the update_totals_on_change executes, I can set the tax to 0 and then it will finish calculating the total.
Does anyone know which functions I can hook on to?
Looking at the checkout.js file in WooCommerce, they set the action to woocommerce_update_order_review for the ajax operation.
I tried following that but soon got lost.
I was thinking I could add some post data by hooking in to woocommerce_checkout_update_order_review
and then hooking in to woocommerce_before_calculate_totals to modify the tax stuff, but I have no idea what I need to modify.
Am I even on the right path?
Alright, I finally figured it out in case anyone is interested.
In my plugin, I made a form after the order notes by hooking in to this function: 'woocommerce_before_order_notes'
add_action('woocommerce_before_order_notes', array(&$this, 'taxexempt_before_order_notes') );
my 'taxexempt_before_order_notes' function contained:
function taxexempt_before_order_notes( $checkout ) {
echo '<div style="clear: both"></div>
<h3>Tax Exempt Details</h3>';
woocommerce_form_field( 'tax_exempt_checkbox', array(
'type' => 'checkbox',
'class' => array('tiri taxexempt'),array( 'form-row-wide', 'address-field' ),
'label' => __('Tax Exempt'),
), $checkout->get_value( 'tax_exempt_checkbox' ));
woocommerce_form_field( 'tax_exempt_name', array(
'type' => 'text',
'class' => array('form-row-first', 'tiri', 'taxexempt', 'textbox', 'hidden'),
'label' => __('Tax Exempt Name'),
), $checkout->get_value( 'tax_exempt_name' ));
woocommerce_form_field( 'tax_exempt_id', array(
'type' => 'text',
'class' => array('form-row-last', 'tiri', 'taxexempt', 'textbox', 'hidden', 'update_totals_on_change'),
'label' => __('Tax Exempt Id'),
), $checkout->get_value( 'tax_exempt_id' ));
}
Then the most important woocommerce function to hook was: 'woocommerce_checkout_update_order_review'
add_action( 'woocommerce_checkout_update_order_review', array(&$this, 'taxexempt_checkout_update_order_review' ));
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($tax_exempt_checkbox) && isset($tax_exempt_id) && $tax_exempt_checkbox == '1' && !empty($tax_exempt_id))
$woocommerce->customer->set_is_vat_exempt(true);
}
I simply parsed out the $post_data that is the serialized form data from the checkout.js file in woocommerce and checked if my part of the form was filled out correctly.
If it was, then I would set the tax exempt for the user.
The accepted solution didn't work for me, but I modified it to use the following:
//=============================================================================
// ADD TAX EXEMPT CHECKMARK
// =============================================================================
add_action( 'woocommerce_after_order_notes', 'qd_tax_exempt');
function qd_tax_exempt( $checkout ) {
echo '<div id="qd-tax-exempt"><h3>'.__('Tax Exempt').'</h3>';
woocommerce_form_field( 'shipping_method_tax_exempt', array(
'type' => 'checkbox',
'class' => array(),
'label' => __('My organization is tax exempt.'),
'required' => false,
), $checkout->get_value( 'shipping_method_tax_exempt' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_review', 'taxexempt_checkout_update_order_review');
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($shipping_method_tax_exempt) && $shipping_method_tax_exempt == '1')
$woocommerce->customer->set_is_vat_exempt(true);
}
The key here is understanding that any field with a name that starts with shipping_method is going to inherit this updating order functionality (which was the part that didn't work for me). I found this answer at http://www.affectivia.com/blog/have-a-checkbox-on-the-checkout-page-which-updates-the-order-totals/
After a long search I found that there is a method for the cart object called remove_taxes() .
So, after setting a user meta for the tax exempt users, this cancels the tax totals.
function remove_tax_for_exempt( $cart ) {
global $current_user;
$ok_taxexp = get_the_author_meta( 'granted_taxexempt', $current_user->ID );
if ($ok_taxexp){ // now 0 the tax if user is tax exempt
$cart->remove_taxes();
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
Because $cart->remove_taxes(); is deprecated. This is what I used instead.
I didn't have a form on the frontend, but have a user roll that is tax exempt. This was my solution.
Also worth noting that set_is_vat_exempt(true) also works in the US to set as tax exempt.
/**
* Set customer as tax exempt if user is a wholesale customer
*/
function remove_tax_for_exempt( $cart ) {
global $woocommerce;
if ( is_user_logged_in() && current_user_can( 'wholesale_customer' ) ) {
$woocommerce->customer->set_is_vat_exempt(true);
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
Since this answer still pops up on google, I thought I'd share that setting the customer as tax exempt only works during checkout, if you need to edit the order on the back-end after it is placed and use the "recalculate" button, the taxes will still appear. Fortunately there is a hook for this as well:
function remove_tax_for_exempt($exempt, $order){
return $exempt || user_can($order->get_user_id(), 'wholesale_customer');
}
add_filter('woocommerce_order_is_vat_exempt', 'remove_tax_for_exempt', 10, 2);

Resources