I am using WP/code snippet with my wordpress website (here still on localhost) and I have a scroll bar that I want to show only when we add the text that will contain this scrollbar at the top. I have for this purpose created a input in the Dashboard where I can type the text to show, this text is then inserted into a table in database. Then if this text is inserted I set a condition (if(isset($flag) && $flag==true)) to show the scrollbar but it is unfortunately not working. I thought this might be due to the variables not on the same pages, but I tried using a $_session() variable still did not work. Can somebody help? Below is the code used (NB: if I remove the condition then I can see the scrollbar). I am thinking of adding the flag variable to the same table and again check for its value as a condition for showing the scrollbar. Can this work? and is there another way

add_action('wp_dashboard_setup', 'my_custom_dashboard_widget');
function my_custom_dashboard_widget() {
    global $wp_meta_boxes;
    wp_add_dashboard_widget('custom_input_widget', 'Breaking News', 'custom_dashboard_input');

function custom_dashboard_input() {
    echo '<div>Write your Breaking News</div></br>';
    echo '<form name="form" action="" method="post"><input style="width:300px;" type="text" id="bnews" class="bnews" name="bnews" required          minlength="5" maxlength="150" size="10" /></br>';
    echo'<button type="submit" value="Submit">Submit</button></br>';
    echo' ';
    echo'<input style="border:1px solid grey; background: whitesmoke; color:black; margin-top=-5px;" type="reset" value="Reset"/></form>';
                $conn = mysqli_connect("localhost", "root", "", "newpress");

        if($conn === false){
            die("ERROR: Could not connect. "
                . mysqli_connect_error());

        $bnews = $_REQUEST['bnews'];

        $result = mysqli_query($conn, "DELETE FROM wp_breakingnews WHERE ID=1");

        $sql = "INSERT INTO wp_breakingnews VALUES (1,'$bnews')";

        if(mysqli_query($conn, $sql)){
                alert("Succes de Sauvegarde de votre News")
        } else{
            echo "ERROR: $sql. "
                . mysqli_error($conn);


function my_add_custom_dashboard_styles(){


function add_scroll_bar(){
    global $wpdb;
    $result = $wpdb->get_results("select * from wp_breakingnews");
    foreach($result as $row){
if(isset($flag) && $flag==true){
    <div class="scroll-container">
        <?php echo'<div class="mouvement">'.$bnews.'</div>'?>;

.scroll-container {
  background-color: #7FB3D5;
  overflow: hidden;
  width: 100%;
  height: 30px;
  line-height: 30px;

.mouvement {
  text-align: right;

  animation properties 
  transform: translateX(100%);  
  animation: move 13s linear infinite;

@keyframes move {
  from {
    transform: translateX(-90%);
  to {
    transform: translateX(90%);

1 Answer 1


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>'; ?>;
    $_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.

  • thank you very much actualy I did exactly the same way with session variable and you know why did not work because I used lowercase $session_ instead of $_SESSION....Anyway thanks for your complete answer that gave me the hint (answer will be accepted and again thanks for the xplanation for scope (1 upvote 😀). You saved my time! Enjoy coding!
    – Mehdi
    Feb 24 at 17:09
  • You are welcome
    – Jahid Shah
    Feb 25 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.