It seems like the issue might be related to the scope of the $flag
variable. Currently, you are setting $flag=true
inside the if(isset($_POST['bnews']))
block, which means it’s a local variable within that block. To make it accessible outside that block, you should declare it outside the if statement.
Additionally, you are trying to use $flag in the add_scroll_bar function, but it’s not being passed as a parameter. One way to resolve this is to use a global variable or a session variable to store the flag value.
Here’s an example using a session variable:
Set the session variable when the form is submitted:
// Add this line at the beginning of your code
session_start();
// Inside your form submission block
if (isset($_POST['bnews'])) {
$_SESSION['flag'] = true;
// Rest of your code
}
Use the session variable in your add_scroll_bar function:
add_action('astra_main_header_bar_top', 'add_scroll_bar');
function add_scroll_bar()
{
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM wp_breakingnews");
foreach ($result as $row) {
$ID = $row->ID;
$bnews = $row->bnews;
}
if (isset($_SESSION['flag']) && $_SESSION['flag'] == true) {
?>
<div class="scroll-container">
<?php echo '<div class="mouvement">' . $bnews . '</div>'; ?>;
</div>
<?php
$_SESSION['flag'] = false; // Reset the flag
}
// Rest of your code
}
This way, the $_SESSION['flag']
variable will persist across different requests, and you can use it to control the visibility of the scrollbar.