Generate random number and add Class on the body of the Wordpress login page

Asked

Viewed 55 times

2

I’m having a problem that at first is simple, but I have no idea what I’m doing to eradicate.

function and simple, I want to add a class inside Body in the Wordpress login page, I can do this, but I also want that when updating the page is generated a random number to complement the class.

Example: when accessing the page and generating a random class, let’s say it is "bg_color_2", when updating the page the value changes to another random number and say "bg_color_4".

The function I’m using is this one, but it’s not working.

function login_classes( $classes) {
    $classes[] = 'bg_color_'; // essa e a class
    $value = rand(1,5); // aqui e gerado meu numero aleatório até 5
    return $classes, $value; / onde deve retornar os valores que fique "bg_color_4"
}
add_filter( 'login_body_class', 'login_classes' );

I can do otherwise, the problem is that I have to edit the wp-login.php of Wordpress and add inside the body class this function:

bg_color_<?php echo rand(1,5); ?>

And of course this is not an alternative, because you should never change the Wordpress files, without taking the risk of losing when updating.

  • Supplementing the question, I was using an application that uses the extension. tpl, to use the Rand function in this file type, just use "{1|Rand:5}"

1 answer

1


You need to concatenate the value and add it to $classes:

function login_classes($classes) {
    $classes[] = 'bg_color_' . rand(1,5);
    return $classes;
}
add_filter( 'login_body_class', 'login_classes' );
  • worked perfectly, can’t believe it was that simple, tried everything, really...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.