ACF block background colour in admin panel - wordpress

I have a bunch of ACF blocks (within an accordion), have added a status field for each block (active or disabled). Basically I want to be able to hide a block on front-end without needing to delete it in admin side, I can do that bit.
Struggling with back-end, if a block status is set to 'disabled', I would like for it be obvious in back-end, so want a different background to other blocks. Below is the code so far. It does not seem understand the if statement, it just adds red bg on all blocks regardless of the status. If I echo out the status, that seems to be assigned to the right block, but if statment doesn't seem to recognise that for some reason.
Just to be clear I need to target blocks which are in the admin panel when editing a page or creating. I have multiple ACF blocks each inside ACF accordion for making it a bit tidy.
Any help would be much appreciated. Thanks.
function my_acf_admin_head() {
global $post;
$post_blocks = parse_blocks( get_the_content( '', false, $post->id ) );
foreach ($post_blocks as $block) {
if ( isset( $block['attrs']['data']['status'] ) ) {
if($block['attrs']['data']['status'] == 'disabled') {
?>
<style>.acf-accordion { background-color: red } </style>
<?php
} else {
?><style>.acf-accordion { background-color: orange } </style><?php
}
}
}
}
add_action('acf/input/admin_head', 'my_acf_admin_head');

function status_acf_blocks( $block ) {
$bg_color = false;
if ( is_admin() ) {
if ( $block == 'disabled' ) {
$bg_color = '<style>.acf-block-wrapper { background-color: red; }</style>';
} else {
$bg_color = '<style>.acf-block-wrapper { background-color: orange; }</style>';
}
}
return $bg_color;
}
Acf block file:
...
<?php
$status = get_field( 'slug' );
echo status_acf_blocks( $status );
?>
<div class="acf-block-wrapper">
</div>
...

Related

Add custom CSS class to "add to cart" button on WooCommerce shop and archives pages based on certain condition

I am trying to disable "add to cart" button on WooCommerce shop and archives pages if product's quantity stock is zero or less.
I don't want to hide it so I came up with the code below.
My issue is that code doesn't add style to element, it replaces the whole button code inside html tree so button is not displayed at all.
Any ideas on how to overcome the issue?
add_action( 'woocommerce_loop_add_to_cart_link', 'remove_price_from_variable_products', 9 );
function remove_price_from_variable_products() {
global $product;
if( $product->get_stock_quantity() <= 0 ) {
?>
<style>
.add_to_cart_button {
cursor: not-allowed !important;
}
</style>
<?php
add_action( 'woocommerce_after_shop_loop_item', 'custom_content_addtocart_button', 100 );
}
}
function custom_content_addtocart_button() {
echo '<br/><div class="button-notice">Contact Us for more information</div>';
}
To add/edit/remove CSS classes from the existing add to cart button on WooCommerce shop and archives pages you can use the woocommerce_loop_add_to_cart_args filter hook
So you get:
function action_woocommerce_loop_add_to_cart_args( $wp_parse_args, $product ) {
// Initialize
$custom_class = '';
// Certain condition, can be anything, in this specific case for the stock quantity. ADAPT TO YOUR NEEDS
if ( $product->get_stock_quantity() <= 0 ) {
$custom_class = 'button-not-allowed';
}
// NOT empty (from here on, no need to make any changes to my answer)
if ( ! empty ( $custom_class ) ) {
// Class
$wp_parse_args['class'] = implode(
' ',
array_filter(
array(
'button' . ' ' . $custom_class,
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
)
)
);
}
return $wp_parse_args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'action_woocommerce_loop_add_to_cart_args', 10, 2 );
Note: the $product->get_stock_quantity() function is best used in combination with $product->managing_stock(), but this depends on your needs
Then apply the following CSS
.button-not-allowed {
cursor: not-allowed !important;
}

If is ACF block template

I'm using ACF to build blocks.
Here's a simple function I've written via functions.php:
<?php
function my_test_function() {
if ( is_page_template( 'template-parts/blocks/hero/hero.php' ) ) {
echo '<p>I appear via the hero.php template.</p>';
}
if ( is_page_template( 'template-parts/blocks/video/video.php' ) ) {
echo '<p>I appear via the video.php template.</p>';
}
}
?>
Here's how I call the function via hero.php and video.php
<?php
my_test_function();
?>
When I test this, nothing appears. I'm guessing the is_page_template function doesn't work in this instance as it's technically not a page template.
Is there another function that might work with ACF block templates?
You could use parse_block() : https://developer.wordpress.org/reference/functions/parse_blocks/ then loop through blocks to check if blockName exist for this page :
function check_if_block_exist($block_handle) {
$post = get_post();
if(has_blocks($post->post_content)) {
$blocks = parse_blocks($post->post_content);
foreach( $blocks as $block ) {
if($block['blockName'] === $block_handle) {
return true;
}
}
return false;
}
}

Append something to wordpress nav menu using wp_nav_menu_objects

I want to append something to a wordpress navigation menu. My code here works, however, right now it puts my content inside the "a" tag of each item. I want it OUTSIDE of the "a" tag - right before the closing "li" tag of each item.
I have searched high and low but the wordpress documentation is limited regarding this...
add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2);
function my_wp_nav_menu_objects( $items, $args ) {
if( $args->theme_location == 'main-menu' ) {
// loop
foreach( $items as &$item ) {
// vars
$mytext = "content";
// append
if( $mytext ) {
$item->title .= $mytext;
}
}
}
// return
return $items;
}

Wordpress Taxonomies

I'm about ready to cry!
I've done a lot of Google searches but I can't quite get this piece of coding working the way I want it to.
In Wordpress I have the following taxonomy:
Active
- Open
- In-Progress
- Awaiting Parts
- Pending / On-Hold
- Awaiting Pick-up
Closed
https://dl.dropboxusercontent.com/u/30177707/wo-tax.png
What I would like is for the child to be displayed for the specific post and if there are no children I would like to display just the parent.
Heres a screenshot I've edited so it gives a better picture of what I'm asking.
https://dl.dropboxusercontent.com/u/30177707/stackoverflow.png
This is the code I've been playing with:
$terms = wp_get_post_terms($post->ID, 'pctracker_workorderstatus');
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo $term->name .'<br>';
}
}
At present its displaying the parent and child for the post.
Would be very grateful for some help or direction!
Thanks,
Jase
You'll have to edit column content like this :
This is an example code you could adapt to your needs, basically I look at all terms to get which ones are parents and childs. Then depending of the results I display parents or childs. In your case, there will always be 1 parent and/or 1 child. But the code should work. (not tested)
function MYCUSTOMPOSTTYPE_custom_columns( $column_name, $id ) {
switch ( $column_name ) {
case 'status':
$terms = wp_get_post_terms($id, 'pctracker_workorderstatus');
$count = count($terms);
if ( $count > 0 ) {
$parents = array();
$childs = array();
foreach ( $terms as $term ) {
if(!empty($term->parent)) {
$childs[] = $term;
} else {
$parents[] = $term;
}
}
//display parent if there no child
if(empty($childs)) {
foreach($parents as $p) {
echo $p->name;
}
} elseif(!empty($parents) && !empty($childs)) {
//don't display parent
foreach($childs as $p) {
echo $p->name;
}
}
}
break;
default:
break;
} // end switch
}
add_action( 'manage_MYCUSTOMPOSTTYPE_posts_custom_column', 'MYCUSTOMPOSTTYPE_custom_columns', 10, 2 );

Wordpress Hide Widget If $_Session Is Active?

I was wondering if there is an easy solution to add some php "if" code when my website tries to show the widgets and if it has $_SESSION I set on homepage (based on the source of which they came) not to show one of them?
I think maybe this will answer your question.
You finally could have something like this:
add_filter( 'sidebars_widgets', 'hidemywidget' );
function hidemywidget($all_widgets) {
if( $_SESSION['%your key%'] == '%your value%' ) {
foreach ( $all_widgets['primary-widget-area'] as $i => $inst ) {
$pos = strpos( $inst, '%the widget you want to hide%');
if( $pos !== false ) {
unset( $all_widgets['primary-widget-area'][$i] );
}
}
}
return $all_widgets;
}

Resources