Populate Custom Category Taxonomy with Data in WP Plugin - wordpress

I'm writing a wordpress plugin that allows people in the admin to hide/show content specific to US states. I have a custom category taxonomy called States, and it lists all of the states. Admins can check which states they want the post to appear in. Pages and posts will not show up in the loop if the user's state doesn't match up with the posts' selected states.
Now, my question is, how do I populate the plugin with all of the states' data upon install (or remove it upon uninstall)?

This should work. You'll need to add the rest of the states, and make sure that your taxonomy is actually called "States", but other than that it should be fine:
<?php
$foo_states = array(
'Alabama',
'Alaska',
'Arizona',
'Arkansas'
);
function foo_install() {
global $foo_states;
foreach ( (array) $foo_states as $state ) {
wp_insert_term($state, 'States');
}
}
register_activation_hook(__FILE__, 'foo_install')
function foo_uninstall() {
global $foo_states;
foreach ( (array) $foo_states as $state ) {
wp_delete_term(get_term_by('name', $state, 'States')->term_id, 'States');
}
}
register_deactivation_hook(__FILE__, 'foo_uninstall');
?>

Related

How to set a WooCommerce email template as default for all emails

I’m looking for a way to send all WordPress emails using a custom WooCommerce template so all emails will look the same.
The path to the template would be:
woocommerce/emails/my-custom-woocommerce-template.php
Does it have to all be templatized in a single file? If not, a combination of these entry points can probably get you the standardization you're looking for:
email-header.php lets you customize the start of the email including the header image (if you need to do more than change its URL). It opens the layout tags for the rest of the email content
email-footer.php lets you customize the footer, and closes the layout tags started in the header.
email-styles.php or the woocommerce_email_styles filter let you customize the CSS (see some gotchas in my article here).
Various actions/filters are scattered throughout the emails for customizing individual parts.
You can use the below function. It is working
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
// List of all templates that should be replaced with custom template
$woo_templates = array(
'emails/admin-new-order.php',
'emails/admin-failed-order.php',
'emails/admin-cancelled-order.php',
'emails/customer-completed-order.php',
'emails/customer-new-account.php',
'emails/customer-note.php',
'emails/customer-on-hold-order.php',
'emails/customer-processing-order.php',
'emails/customer-refunded-order.php',
'emails/customer-reset-password.php',
);
//Check whether template is in replacable template array
if( in_array( $template_name, $woo_templates ) ){
// Set your custom template path
$template = your_template_path.'emails/my-custom-woocommerce-template';
}
// Return what we found
return $template;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
add_filter( 'wp_mail', 'your_wp_mail_action' ); // $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
function your_wp_mail_action( $args ) {
global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
$your_prefix_your_email_args = $args;
ob_clean();
get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
$args['message'] = ob_get_clean();
// ... your logic
return $args;
}
To view and update email settings, log into your website dashboard. In the left-hand menu, click on WooCommerce → Settings.
There, you’ll find several options tabs at the top. Click Emails to view the following templates
you can custom all as you want

Show product variations on the product category and shop pages

I'd like to display product variations for some (not all) variable products on the shop and product-category pages based on a custom field I'm planning to add to each variable product. To keep pagination working, I can hook in to the woocommerce_product_query hook in WC_Query::product_query($q) but I can't figure out how to modify the query to give me what I'm after.
The logic I want to apply is:
//Pseudo-code
$loop = $query->get_all_products // i.e. not just for one page
foreach ( $loop as $product) {
if ($product->is_simple) {
display_simple($product)
} else {
$variations = wc_get_products($product->get_children());
foreach ($variations as $product) {
if ($product->custom_show_on_archive_pages) {
display_variation($product)
}
}
}
}
The problem with the above (apart from being pseudo-code) is that it assumes we have an array called $loop but actually we have a query object.
My ideal would be to modify the query so that it generates the results as per the pseudo-code, and then my archive-product.php template can use an if/else to display simple vs variable products.
Something like this in my functions.php:
add_action( 'woocommerce_product_query', 'show_variable_products');
function show_variable_products( $q ) {
$meta_query = $q->get('meta_query');
// Do something
$q->set('meta_query', $meta_query);
}
I'm wondering if I need to actually run multiple queries somehow:
The original query but filtered to parent products
The children of these parent products (i.e. the variations), filtered to those with custom field checked
The original query but filtered to simple products
Merge (2) and (3)
Would this be possible?
Alternatively, is there another solution (that does not involve buying a plugin)?

Show only user-created taxonomy entries

iam implementing an event system on wordpress which allows an restricted user the following:
Create a new organizer (custom-post-type)
Create an event (custom-post-type)
Creating an event the user should choose an organizer for a special event by a list. Therefore i handle the organizer cpt as a taxonomy inside the event cpt.
Now my question is:
How can i only show the organizers, which this specific user has created? I face the problem, that all existing events are shown on every event for every user.
If you need any code or screenshots let me know, thank you very much in advance!
Solved it with Advanced Custom Fields plugin (Relationship Field). It is possible to relate cpts into another cpt. I learned to NOT use a cpt as a taxonomy.
Approach for filtering only user created posts
You can achieve it by adding the following code to your functions.php file
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'edit_others_posts' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

Woocommerce choose grid or list view for each category

I want the possibility to choose between grid and list view for each Woocommerce category. I have found this plugin: https://nl.wordpress.org/plugins/woocommerce-grid-list-toggle/
However, the plugin is meant for the shopper to choose whether to display items in grid view or list view. What I truly want is the ability to assign a view for each category in the back-end.
Example:
Category A is displayed as grid
Category B is displayed as list
Breaking my head over this.
Similar to this question you need to filter template_include. You need to call your custom archive template archive-list-view.php and save it in your theme's woocommerce folder. Obviously, you can name it anything you like, you will just have to adjust the code below to match.
Folder structure:
/theme-folder/functions.php
/theme-folder/woocommerce/archive-list-view.php
On the template_include filter we will check if we are on the term archive for the nussmylch product category. If so, we'll look for and supply the new template. Otherwise, the standard template is used.
EDIT: incorrect WooCommerce function is_product_taxonomy() was used. is_product_category() is needed to check for a specific category.
add_filter( 'template_include', 'so_33615903_custom_category_template', 20 );
function so_33615903_custom_category_template( $template ) {
// check you are on the taxonomy archive for specific category
if ( is_product_category( 'nussmylch' ) ) {
$new_template = locate_template( array( 'woocommerce/archive-list-view.php' ) );
if ( '' != $new_template ) {
$template = $new_template ;
}
}
return $template;
}
Working Example

How do I make my custom WordPress meta box visible to admins only?

I am using my functions.php to add a custom meta box on my posts page in the WordPress Admin Area. However, I need to make it so its only visible to admins, and not editors, contributors, etc.
What would I do to make it visible to admins only?
function your_function() {
global $current_user;
if($current_user->roles[0] == 'administrator') {
add_meta_box(your parameters);
// fill in your parameters
}
}
add_action('admin_init','your_function');
if ( is_user_logged_in() ) {
get_currentuserinfo();
# check if current user is admin
if ( $current_user->wp_user_level >= 10 ) {
# put your admin-only function here
}
}
This snippet works for custom taxonomies. It removes / hides a custom taxonomy meta box for all non-admins, assuming no other role has the update_core capability. Similar, but opposite of the answer by #johannes-pille
function remove_tax_metaboxes() {
if (!current_user_can('update_core')) {
remove_meta_box( 'taxdiv', 'post', 'side' );
}
}
add_action( 'do_meta_boxes', 'remove_tax_metaboxes' );
Note that the third argument of remove_meta_box may differ, see https://codex.wordpress.org/Function_Reference/remove_meta_box

Resources