So I have this chunk of PHP that excluded a certain element from appearing on all of my WordPress pages, while keeping them on posts/products, but there's a couple of pages where I would still like it to appear.
How do I go about excluding certain Page IDs from this query?
add_action( 'template_redirect', 'avada_check_page' );
function avada_check_page() {
if ( is_singular( 'page' ) ) {
add_action( 'avada_override_current_page_title_bar', 'avada_remove_title_bar' );
}
}
function avada_remove_title_bar() {
}
There's about 100 pages I want to apply this to and only 5 where I want to exclude them from this code.
A simple way would just be to use a condition on that first if statement.
add_action( 'template_redirect', 'avada_check_page' );
function avada_check_page() {
if ( is_singular( 'page' ) && !in_array(get_the_ID(),array(1,2,3,4)) ) {
add_action( 'avada_override_current_page_title_bar', 'avada_remove_title_bar' );
}
}
function avada_remove_title_bar() {
}
Where 1,2,3,4 place the exceptions posts ID's.
Related
I'm trying to remove all default bulk actions from the admin's orders page with the following code:
add_filter( 'bulk_actions-edit-shop_order', 'remove_order_statuses_bulk' );
function remove_order_statuses_bulk ( $bulk_actions ) {
error_log( print_r( $bulk_actions, true ) );
$unwanted_actions = array( "mark_processing", "mark_pending", "mark_on-hold", "mark_completed", "mark_cancelled", "mark_refunded", "mark_failed" );
foreach ( $unwanted_actions as $action ) {
if ( isset( $bulk_actions[$action] ) ) {
unset( $bulk_actions[$action] );
}
}
return $bulk_actions;
}
The error_log shows the array containing just "edit", "trash" and "mark_custom-status" (which is a status that I've created using the same hook). So the array is already empty.
The problem is that the menu with bulk actions in wp-admin/edit.php?post_type=shop_order is still showing the removed entries.
I have no caching plugin at present. What may be causing this?
Your function is being called too early since you don't have a priority set.
Change the priority of your add_filter and it works.
add_filter( 'bulk_actions-edit-shop_order', 'remove_order_statuses_bulk', 40, 1 );
I have a WooCommerce site and in the single product page I have a custom checkbox to agree to terms before the add to cart button, but I am trying to add a true/false field in the dashboard so that this checkbox can be moved to a different position on the page.
My functions look like this:
add_action( 'acf/init', "acf_move_checkbox", 10 );
function acf_move_checkbox() {
$move_cart = get_field('move_cart');
if ($move_cart) {
add_action( 'woocommerce_after_single_product', "acf_product_terms", 10 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', "acf_product_terms", 10 );
}
// More code
}
And nothing is happening, am I along the right lines with this or way off?
While acf/init is similar to the WordPress init action, I wouldn't use it.
Init hooks are performed constantly.. since you want to apply an action on the single product page it's best to use a hook that only applies to that page, and will only run on those kinds of pages.
For example you can use the woocommerce_single_product_summary hook. It might also be useful to test fields by hard coding before retrieving them.
So you get:
function action_woocommerce_single_product_summary() {
// Get field
//$move_cart = get_field( 'move_cart' );
// Set true OR false
$move_cart = false;
// When true
if ( $move_cart ) {
add_action( 'woocommerce_after_single_product', 'my_callback_function', 9 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', 'my_callback_function', 9 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );
function my_callback_function() {
echo '<p style="color: red; font-size: 20px;">Hello World!</p>';
}
If the above step works, you can replace the hard coded field with the desired code/field
I'm using the following snippet to limit the number of products in search results on my website from 10 to only 8.
However, I noticed that it's also limiting the number of products shown via WP Admin Dashboard > Products > All Products to 8 if you use the filter. When you use the filter, it also only shows 8 products even if the filter has more than 8 products.
Is there a way to use this snippet only in search results in the front end and not in the WP Admin area?
function myprefix_search_posts_per_page($query) {
if ( $query->is_search ) {
$query->set( 'posts_per_page', '8' );
}
return $query;
}
add_filter( 'pre_get_posts','myprefix_search_posts_per_page', 20 );
Your function is correct. You will just need to add the is_admin() control to make sure the query is executed only in the frontend.
Also you should add the is_main_query() control to make sure it is the main query.
Finally, the posts_per_page parameter is an integer and not a string.
// change the number of search results per page
add_filter( 'pre_get_posts', 'myprefix_search_posts_per_page', 20, 1 );
function myprefix_search_posts_per_page( $query ) {
// only in the frontend
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_search() ) {
$query->set( 'posts_per_page', 8 );
}
}
}
The code has been tested and works. Add it in your active theme's functions.php.
You can use woocommerce_product_query to change posts_per_page. check the below code.
add_action( 'woocommerce_product_query', 'myprefix_search_posts_per_page', 999 );
function myprefix_search_posts_per_page( $query ) {
if( is_admin() )
return;
if ( $query->is_search() ) {
$query->set( 'posts_per_page', '8' );
}
}
I noticed that post tags are different than WooCommerce product tags.
I need to add post tags to WooCommerce products so i can include some WooCommerce products in the post tag archives.
Is this possible?
I've tried these code snippets but it doesn't add them.
add_filter( 'pre_get_posts', 'add_custom_types' );
function add_custom_types( $query ) {
if ( is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array( 'post', 'products', 'product' ) );
return $query;
}
}
add_filter('request', 'post_type_tags_fix');
function post_type_tags_fix($request) {
if ( isset($request['tag']) && !isset($request['post_type']) )
$request['post_type'] = array( 'products', 'product' );
return $request;
}
I needed to do this as well and have come up with what I think is a good way of doing this. A few steps:
Firstly, you need to add the post_tag taxonomy to the woocommerce product post type. You can do this easily with the following filter:
function custom_wc_add_post_tags( $args ){
$args['taxonomies'] = array('post_tag');
return $args;
}
add_filter( 'woocommerce_register_post_type_product', 'custom_wc_add_post_tags' );
This will add a new 'Tags' item to your admin menu, and allow you to tag products with regular post tags rather that the woocommerce-specific 'product_tag' taxonomy. Secondly, you'll likely want to remove the 'product_tag' taxonomy if you don't plan on using it? Because the above will result in two admin menu items called 'tags' which will get confusing. The following will do that for you:
add_filter('woocommerce_taxonomy_objects_product_tag', '__return_empty_array');
add_filter('woocommerce_taxonomy_args_product_tag', '__return_empty_array');
And to remove the column from the 'Products' table on the backend:
function custom_wc_remove_product_tags_column($columns){
unset( $columns['product_tag'] );
return $columns;
}
add_filter( 'manage_edit-product_columns', 'custom_wc_remove_product_tags_column', 15 );
That will actually add the tags to your products. If you then need these to show up in your archive page, you may still need to modify that page's query to look for posts of type 'product' as well as the standard 'post'. It looks like your function above on the 'pre_get_posts' hook will do that part for you. Hope this helps!
Use below code to get archive page in product tag page. now product tag page use theme archive.php file.
Add Below code in theme functions.php
add_filter( 'template_include', 'woocommerce_product_tag_page_template', 99 );
if ( ! function_exists( 'hcode_post_format_parameter' ) ) {
function woocommerce_product_tag_page_template( $template ) {
if ( is_tax( 'product_tag' ) ) {
get_template_part('archive');
}
return $template;
}
}
I'm building a site with a checkout, the site has 2 products, that need to be sold individually. So to prevent people from adding the first item, leaving the cart, then adding the second item, I wanted to set up the cart to auto-empty. So I decided to test it for the homepage. The initial function I had was this:
function my_empty_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
add_action('init', 'my_empty_cart');
That works perfectly. But the cart is always empty because of this. So I decided to add in an "if" statement:
function my_empty_cart() {
global $woocommerce;
if ( is_front_page() && isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'init', 'my_empty_cart' );
And that doesn't work...at all. I've tried it with "is_home" and "is_page" (with various page titles and ids). I even tried removing the "&& isset" part, just to test. At this point i'm lost, I've never had this much difficulty with a simple php code and i'm pulling out my hair at this point. Is there something simple i'm just not seeing?
this worked for me (dont relies on WordPress conditional):
/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ($_SERVER['REQUEST_URI'] === '/') {
$woocommerce->cart->empty_cart();
}
}
Conditional tags (is_home(), is_front_page(), etc) don't work at the time the 'init' hook fires. You can use 'wp' hook instead. It runs immediately after wp class object is set up (http://codex.wordpress.org/Plugin_API/Action_Reference/wp).
function my_empty_cart() {
global $woocommerce;
if ( is_front_page() && isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'wp', 'my_empty_cart' );