• Hello !

    I would like to know how can I display randomly a php function, and randomly a custom field choosen by one author on his post ?

    Is there a code to enable that ?

    Thanks !

Viewing 15 replies - 1 through 15 (of 24 total)
  • You can call a random function with code like this:

    // TEST RANDOM SELECTION OF FUNCTION
    $choices = array('test1','test2','test3','test4');
    $choice = $choices[rand(0,sizeof($choices)-1)];
    $choice();
    Thread Starter evo252

    (@evo252)

    Hello ! Thanks man, but it does’nt run..

    Sorry but can u please display the entire code cause I’m a big noob…

    I assumed you would be pasting it inside some existing php. Maybe you just needed to put it inside the php tags?

    <?php // TEST RANDOM SELECTION OF FUNCTION
    $choices = array('test1','test2','test3','test4');
    $choice = $choices[rand(0,sizeof($choices)-1)];
    $choice();
    ?>

    And, you did code your own functions and use their names instead of ‘test1’, ‘test2’, etc, didn’t you?

    Thread Starter evo252

    (@evo252)

    Thanks ! One of my two php functions is :
    <?php kd_template_ad(1); ?>

    And I want a custom field for the other… How can I put them into your code ?

    When I make that , just for test :
    <?php // TEST RANDOM SELECTION OF FUNCTION
    $choices = array(‘kd_template_ad(1);’);
    $choice = $choices[rand(0,sizeof($choices)-1)];
    $choice();
    ?>
    It doesn’t run…

    The items in the $choices array need to be only the function names:

    $choices = array('kd_template_ad');

    Any parameters have to be supplied in the $choice() line.

    I don’t understand what you want with the custom field. Do you want to get the name of the function from a custom field?

    Thread Starter evo252

    (@evo252)

    In fact, I would like to launch this function : <?php kd_template_ad(1); ?> randomly with a code that the author can put in the custom field of his article. Is it possible ? (thousand thanks for help !)

    The aim of this : launch my function kd_template_ad randmoly with a value (javascript code for example) that every author of my blog can put in their custom field !

    Let me see if I understand. You really don’t want a random function, you want to run kd_template_ad with a value supplied from a custom field in the post. But you want to sometimes run it and sometimes not, randomly. Is this correct?

    Thread Starter evo252

    (@evo252)

    It’s nearly that, but not exactly that. Try to explain, but sorry for my very bad english, I’m from France :/

    What I need exactly :

    I want, if there is a co-author on a post via a plugin that I have (co-author plus), aka if(function_exists(‘coauthors_posts_links’)) , launch randomly 2 functions :
    1/ this function : <?php kd_template_ad(1); ?> (which will display a specific banner or javascript code).
    2/ Or another function, which would be a value that one author has indicated in his custom field (his own banner or javascript code).

    But I don’t know how to make that, and what’s the function which launch a custom field…

    Sorry for time that you give me ! MERCI !!

    Well, your English is a LOT better than my French!! I think I understand now, but let me restate:

    IF there is a co-author
       IF there is a custom field AND it names a function
          Call randomly either kd_template_ad(1) or
             the function named by the custom field.
       ELSE
          ? what to do here
       ENDIF
    ELSE
       ? what to do here
    ENDIF

    Now, it is a little more complicated if the custom field function needs to pass parameters, so I need to know how they will be passed. It would be easier if any parameters were in a separate field, but they can probably be parsed from the one value if not.

    Also, please tell me your custom field name.

    Thread Starter evo252

    (@evo252)

    Thanks ! I’m afraid to not understand all your requests (singularly your big last paragraph), but let’s trying :

    – I don’t know the custom name, but we can give an arbitrary name, isn’t it ? So, let give “informapub”.

    – For conditions :

    IF there is a co-author
       IF there is a custom field AND it names a function
          Call randomly either kd_template_ad(1) or
             the function named by the custom field.
       ELSE
          Call kd_template_ad(1)
       ENDIF
    ELSE
       Call kd_template_ad(1)
    ENDIF

    Is it good ?

    Great! It will take a little time to code, and I can only do limited testing, but I will post some sample code later.

    The long paragraph is a little confusing. Suppose the coauthor wants to call the function myfunction(‘myparameter’). You cannot assign that string to a variable and call it. This will not work:

    $thefunc = "myfunction('myparameter')";
    $thefunc();

    It must be done like this:

    $thefunc = "myfunction";
    $theparam = 'myparameter';
    $thefunc($theparam);

    So we need to have the function name separate from its parameters.

    This is not a problem if the coauthor functions do not need parameters, but if they do, we need to define how they will be passed in the custom field. It would be easier if the function name is in one field, and the parameters in another.

    I have assumed for now that the user functions do not require parameters. Add the function below to your functions.php, and call it with do_random_func($post->ID)

    <?php function do_random_func($id=0) {
    /* Randomly run a function supplied in a custom field, or a
    pre-defined function.
    */
    if ($id) :
       if (function_exists('coauthors_posts_links')) :
          $meta_value = get_post_meta($id,'informapub',true);
          if ($meta_value && function_exists($meta_value)) :
             if (5 > rand(0,10)) :
                $meta_value();
             else:
                kd_template_ad(1);
             endif;
          else:
             kd_template_ad(1);
          endif;
       else:
          kd_template_ad(1);
       endif;
    endif;
    }
    ?>
    Thread Starter evo252

    (@evo252)

    Thank you vtxyzzy !! So, if I have understood, I have to put the code on functions.php (theme file), and put <?php do_random_func($post->ID) ?> on my template ?

    When I put your code on functions.php, it makes a bug on the top of the homepage :

    Warning: Cannot modify header information – headers already sent by (output started at /home/html/mydomain.com/wp-content/themes/ProSense/functions.php:17) in /home/html/mydomain.com/wp-content/plugins/sidebar-login/sidebar-login.php on line 314
    TDOMF ERREUR: Les Headers ont déjà été envoyés dans le fichier file /home/html/mydomain.com/wp-content/themes/ProSense/functions.php à la ligne 17 avant que session_start() puisse être appelé. Cela peut être du à …

    Votre theme WordPress insère HTML avant d’appeler le tag de template “get_header”. Ce peut être simplement une ligne vide. Vous pouvez le verifier an activant le thème WordPress Default ou Classic et voir si l’erreur persiste. Vous pouvez également vérifer dans votre thème à quel endroit il appelle “get_header”.

    That is correct. You also need to put the code for your functions in the theme’s functions.php, or another file that gets loaded before the page. This code will call the functions when the page is loaded.

    I am not sure how this will work for the coauthors. How will they get their functions added in?

    Thread Starter evo252

    (@evo252)

    I didn’t undertood your last question ?

    Sorry the code seems to run, the bug message that I’d talk not appears now… I’m showing if all it’s ok !

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Display custom field and php function randomly’ is closed to new replies.