• Resolved rheajap

    (@rheajap)


    Hello,

    I am not good in coding but I really need something on my wordpress website. Till now I could find all my info on the forums and google and I succeeded for 95% to build my website.

    At this moment I run a petition script ‘LH sigining’ This script builds a list of people who subscribed the petition. I needed an extra text area in that form and I managed to get that text area in the form and store the value in the database.

    To reach this I use the following filter/function:

    function your_form_handler($content, $atts, $post){
    
    if ($post->ID == “154643”){
    
    $content .= ‘<p>
    <textarea name=”opmerkingen” id=”opmerkingen” rows=”10″ required=”required” placeholder=”a placeholder”></textarea>
    </p>’;
    
    }
    
    return $content;
    
    }
    
    add_filter( ‘lh_signing_intermediate_logged_out_form_filter’, “your_form_handler”, 10, 3);
    
    function your_post_handler($user_id){
    
    if ($_POST[‘opmerkingen’]){
    
    add_user_meta( $user_id, ‘opmerkingen’, sanitize_text_field($_POST[‘opmerkingen’]));
    
    }
    
    }
    
    add_filter( ‘lh_signing_http_post_filter’, “your_post_handler”, 10, 1 );

    So If people write the text area with their comment it is stored in my database. I have tested it and the textarea ‘opmerkingen’ is showing up:

    SELECT * FROM 'wp_usermeta' WHERE 'meta_key' LIKE 'opmerkingen' In the mysql database it is showing one record now. Displaying ‘umeta_id’, ‘user0_id’, ‘meta_key’ (opmerkingen) and ‘meta_value’ (the value given by that user.

    What I need/want is a way to show the custom meta_values of Meta_key: opmerkingen on a page in my website one by one.

    I Need it to show random and only if the meta_value exists. I want it to show without user id, user name or whatever extra field. I only want to show the textarea input given by a user.

    The text is about why they did sign the petition. So , on the petition page I want to show something like:

    ” At this moment allready [number / count I allready have] persons have signed this petition because: [HERE I WANT TO DISPLAY THE RANDOM META_VALUE OF THE META_KEY ‘OPMERKINGEN’]

    The counter is working allready. I cannot find out how to get the data from my database and show it one by one randomly refreshing with page change or on a timed intervall.

    I tried to rebuild a lot of $wpdb arguments but without any luck. My skills are to less to solve this. I really hope somebody can give me an advice how to make / write this piece of code so I can finish my project.

    It is possible I am thinking wrong of my approach but I do think I should get the value of text area ‘ opmerkingen’ out of my database.. But how?

    Sorry for any bad english

    Warm regards

    Jap

    • This topic was modified 8 years ago by rheajap. Reason: Forgot code tag
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    While you could set up an AJAX routine that queries for a random meta value at regular intervals, the code is rather involved. An easier way is to get a batch of values all at once and then display them one by one. You don’t want to get all values all at once, but you can get enough random values so that it’s unlikely anyone would notice when the display starts reusing the same values all over again.

    This code would go on your template where you want the output to occur:

    global $wpdb;
    $query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='opmerkingen' ORDER BY RAND() LIMIT 20";
    $reasons = $wpdb->get_results( $query );
    echo "<ul class=\"reason_list\">\n";
    $count = 0;
    foreach ( $reasons as $reason ) {
       $class = 0 == $count?'reason':'reason hide_me';
       echo "<li id=\"reason{$count}\" class=\"$class\">{$reason->meta_value}</li>\n";
       $count++;
    }
    echo "</ul>\n";

    Adjust the LIMIT value to get a different number of results. In your CSS set the .hide_me class to display: none; You can use the other classes to style and size the output as desired.

    You’ll also need JavaScript to rotate through the list, each item has a unique ID which can be used to select an individual item and change its class to simply “reason” without the “hide_me” and also reset the previous item back to the original “reason hide_me”.

    There’s a number of ways to achieve the JavaScript part. You could try to write your own of course. You should be able to find a script that already exists online somewhere, though some adjustments will be needed. If you’ve not worked with JavaScript in the WP environment before, be aware that WP has some unique requirements for using JavaScript, especially if jQuery is involved. Mainly, all but the shortest scripts should be on an external file that is queued for being included using wp_enqueue_script().

    You could use one of the many slider scripts to rotate through the list. These are generally meant for rotating through images as a slideshow, but many work with arbitrary HTML output. You want one that works with your own output. If you are required to specify content through a back end interface, it’s not going to be compatible with our list. The output may need adjustment to work with such scripts.

    There are several WP plugins that might work. You don’t even really need a WP plugin, some generic JavaScript or jQuery slider library will work as long as you correctly queue the needed scripts for WP use. I’ve used FlexSlider from WooThemes with success. This is the generic jQuery version, not the premium WP plugin.

    This is not an endorsement, there are other options available. I’m just saying I’ve used it successfully in the past.

    Thread Starter rheajap

    (@rheajap)

    Wow thank you for this big explanation. I have homework todo. I will do this tomorrow morning. I will get back with the results.

    I do have one question. Do I have to put the code between <?php ?> or just copy it?

    For now, thank you very much..

    Warm regards

    Jap

    • This reply was modified 8 years ago by rheajap.
    Moderator bcworkz

    (@bcworkz)

    It is PHP code after all, so it needs to be between <?php ?> tags somewhere. Whether you need to add them or not depends on your template. If where you insert the code is all HTML, then add the tags. If it’s already PHP, do not add more tags, you cannot nest <?php ?> tags like one would do with HTML.

    Thread Starter rheajap

    (@rheajap)

    THAMK YOU!!!! It is working!!!

    You made me very happy..

    CLICK FOR RESULT

    THank you again!!

    Jap

    Moderator bcworkz

    (@bcworkz)

    You are welcome! I’m happy you’re happy ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show custom meta_value in Newsticker random’ is closed to new replies.