vendredi 21 mai 2021

WordPress Plugin PHP: random choice from a list in a variable

I'm sure PHP has a way to handle this, and I've struggled to find it or figure it out. I'm sure it's simple and obvious to others:

I'm working on a new function for a WordPress plugin of mine. I want to allow the user to save a list of words (in wp-options) and the plugin will pull one randomly to use.

When this code pulls from a simple list, like this:

$mc6397gh_Rand = array(One,Two,Three,Four);

it will select and use one of those words.

But when I substitute (as I need to) the variable, even with the exact same content, like this:

$mc6397gh_Rand = array($mc6397gh_PerList);

it won't choose one word, it just displays the whole list. I need it to randomly choose one of the words in the list returned from the WordPress option. It needs to see it just as if it were simple list.

Any help greatly appreciated!

function mc6397gh_replace( $mc6397gh_wp_admin_bar ) {
$mc6397gh_my_account=$mc6397gh_wp_admin_bar->get_node('my-account');
$mc6397gh_PerList = get_option(mc6397gh_MyList) ;

    $mc6397gh_Rand = array($mc6397gh_PerList);
    $mc6397gh_RandIndex = array_rand($mc6397gh_Rand);
    $mc6397gh_PerRand = $mc6397gh_Rand[$mc6397gh_RandIndex];

    $mc6397gh_newtitle = str_replace( 'Howdy,', $mc6397gh_PerRand, $mc6397gh_my_account->title );
    $mc6397gh_wp_admin_bar->add_node( array(
    'id' => 'my-account',
    'title' => $mc6397gh_newtitle,
) );



Aucun commentaire:

Enregistrer un commentaire