skip to Main Content

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,
) );

3

Answers


  1. Chosen as BEST ANSWER

    Thank you for the thoughtful answers. Still, it does not work. I've spent lots of time with both answers trying to find the where it trips up.

    (There are, I believe, a couple of extra steps in there, as I Google and experiment to find the answer.)

    When I use the first answer, the return on is always 0, or actually a circle.

    With the second, nothing shows -- none of the words appear.

    In the first answer, if I make

    $mc6397gh_Rand = array(one,two,three);
    

    it works, but is not using my word list returned in the variable.

    In the second, if I do that and also eliminate the [0], it works, again without my needed word list in the variable.

    Nothing so far works when the same list is returned in a variable. In my original, I see the entire list of words stored in the variable.

    Is there a way to make the list in the variable work as an array?


  2. The problem is in the line

    $mc6397gh_Rand = array($mc6397gh_PerList);
    

    When you do something like this the variable $mc6397gh_Rand will be like [0 => $mc6397gh_PerList]. So array_rand only has one key to select from.

    You can do either of the following

    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 = $mc6397gh_PerList;   ## Changed Line
            $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,
            ));
        }
    

    or you can do this

    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[0]);    ## Changed Line
            $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,
            ));
        }
    

    Having said this I am not sure why you need to add an extra variable when you could have simply done $mc6397gh_RandIndex = array_rand($mc6397gh_PerList);

    Login or Signup to reply.
  3. The miracle of Google FINALLY came through. To turn a list contained in a variable into an array, use "eval". See the line isolated below. That works!

        if (get_option('mc6397gh_Type') == 'personalrandom') {
    
        function mc6397gh_replace( $mc6397gh_wp_admin_bar ) {
        $mc6397gh_my_account=$mc6397gh_wp_admin_bar->get_node('my-account');
        $mc6397gh_PerList = get_option(mc6397gh_MyList) ;
    
        eval("$mc6397Create = array($mc6397gh_PerList);");
    
        $mc6397gh_RandIndex = array_rand($mc6397Create);
        $mc6397gh_PerRand = $mc6397Create[$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,
        ) );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search