Ecommerce Shopify WordPress Discussion

hide admin bar according to a post metadata

I know there are some questions are similar to my question but mine is a little different I've added a post metadata to all post types called 'admin_bar_status' then created a function that reads all types into a table with a toggle switch to update that metadata to 0 or 1 I did everything well but I have two problems : when I switch the toggle , it doesn't changed until I pressed once again. <?php $post_types = get_post_types(); $i = 1; ?> <div class="container"> <form id="tableform" method="post"> <table class="table-style" class="right"> <thead> <th>Post Type</th> <th>Status</th> </thead> <?php // Loop through each post type foreach ($post_types as $post_type) { // Arguments for WP_Query (customize as needed) $args = array( 'post_type' => $post_type, 'post_status' => 'any', 'posts_per_page' => -1, // Get all posts ); $the_query = new WP_Query($args); // Loop through each post if ($the_query->have_posts()) { //while ($the_query->have_posts()) { $the_query->the_post(); // Check if the post has the 'admin_bar_status' metadata if (metadata_exists('post', get_the_ID(), 'admin_bar_status')) { // Get the 'admin_bar_status' metadata $admin_bar_status = get_post_meta(get_the_ID(), 'admin_bar_status', true); ?> <tr> <td> <?php echo $post_type; ?> </td> <td align=center> <label class="toggle-switchy" data-size="sm"> <?php if ($admin_bar_status == 1) { ?> <input type="checkbox" id="example2" onchange="submitPostType('<?php echo $post_type; ?>')" checked> <span class="toggle"> <span class="switch"></span> </span> <?php } else { ?> <input type="checkbox" id="example2" onchange="submitPostType('<?php echo $post_type; ?>')"> <span class="toggle"> <span class="switch"></span> </span> <?php } ?> </label> </td> </tr> <?php } } } ?> </table> </form> <script> function submitPostType(postType) { var form = document.getElementById('tableform'); var input = document.createElement('input'); input.type = 'hidden'; = 'postType'; input.value = postType; form.appendChild(input); form.submit(); } </script> </div> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $post_type = $_POST['postType']; _update_post_type_metadata($post_type); } } also, the main problem is: the function that reads that metadata can't hide the dmin bar for the specific page which has 'admin_bar_status'==0 or hide for all types! add_action('template_redirect', 'set_admin_bar'); function set_admin_bar() { // Get all registered post types $post_types = get_post_types(); foreach ($post_types as $post_type) { // Get the admin bar status for the current post type $args = array( 'post_type' => $post_type, 'post_status' => 'any', 'posts_per_page' => -1, // Get all posts ); $posts = get_posts($args); foreach ($posts as $post) { $admin_bar_status = get_post_meta($post->ID, 'admin_bar_status', true); // Hide the admin bar if metadata is set to 0 if ($admin_bar_status === '0') { show_admin_bar(false); } } } } please help me
For showing and hiding the admin_bar you need to use filter add_filter('show_admin_bar', function () { global $post; if (is_single() && "0" == get_post_meta($post->ID, 'admin_bar_status', true) { return false; // hide admin bar } return true; // show admin bar });

February 26, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.