1

So I have a php statement:

$latest_pollsid that contains a number which increases by 1 every time a specific form is submitted. I am reading this statement into several functions and for one of these functions I would like it to capture the value at the current time and not update. Essentially I want it to read the statement when the function runs, grab the number, and then display that without it changing in the future even when the value of the statement itself changes. Is there a way to do this with PHP?


The Scenario:

I am creating a post in the background when the user creates a new poll which embeds the poll they have just made with a shortcode. The shortcode uses the latest poll generally meaning that if I create a poll now and create a poll tomorrow, tomorrow both posts will show tomorrows poll. Todays post won't show today's poll.

Code used to create the background post:

function programmatically_create_post() {

    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    // Setup the author, slug, and title for the post
    $author_id = 1;
    $slug = 'example-post';
    $title = 'My Example Background Post';

    // If the page doesn't already exist, then create it
    if( null == get_page_by_title( $title ) ) {

        // Set the post ID so that we know the post was created successfully
        $pollq_question = wp_kses_post( trim( $_POST['pollq_question'] ) );
        $post_id = wp_insert_post(
            array(
                'comment_status'    =>  'open',
                'ping_status'       =>  'closed',
                'post_author'       =>  $author_id,
                'post_name'         =>  $slug,
                'post_title'        =>  $pollq_question,
                'post_status'       =>  'publish',
                'post_type'         =>  'post',
                'post_content'      =>  '[poll id="' . $latest_pollid . '"]'
            )
        );

    // Otherwise, we'll stop
    } else {

            // Arbitrarily use -2 to indicate that the page with the title already exists
            $post_id = -2;

    } // end if

} // end programmatically_create_post
add_filter( 'after_setup_theme', 'programmatically_create_post' );

PHP FUNCTION CODE:

function polls_latest_id() {
    global $wpdb;
    $poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY pollq_timestamp DESC LIMIT 1");
    return intval($poll_id);
}

MANAGE POLLS CODE

This page manages to list all of the polls with the correct id's but without knowing PHP very well I am struggling to interpret this:

echo "<tr id=\"poll-$poll_id\" $style>\n";
                    echo '<td><strong>'.number_format_i18n($poll_id).'</strong></td>'."\n";
                    echo '<td>';
                    if($current_poll > 0) {
                        if($current_poll == $poll_id) {
                            echo '<strong>'.__('Displayed:', 'wp-polls').'</strong> ';
                        }
                    } elseif($current_poll == 0) {
                        if($poll_id == $latest_poll) {
                            echo '<strong>'.__('Displayed:', 'wp-polls').'</strong> ';
                        }
                    } else if(in_array($poll_id, $multiple_polls)) {
                            echo '<strong>'.__('Displayed:', 'wp-polls').'</strong> ';
                    }
  • 1
    You can't save data in PHP at all. You have to write to a file (generally a bad idea) or save to the database. Are you hacking somebody else's plugin? – s_ha_dum Dec 22 '15 at 16:39
  • @s_ha_dum the plugin is installed on my site but yes I am trying to modify it so that it shows the latest poll at time of creation and not just in general. Are there any other options to get the latest id at time of creation? – dwinnbrown Dec 22 '15 at 16:41
  • "latest poll at time of creation" -- no idea what that means – s_ha_dum Dec 22 '15 at 16:43
  • Ok so it is a poll plugin which lets users make polls. I am creating a post in the background when the user creates a new poll which embeds the poll they have just made with a shortcode. The shortcode uses the latest poll generally meaning that if I create a poll now and create a poll tomorrow, tomorrow both posts will show tomorrows poll. Todays post won't show today's poll. I know it's not a very good explanation but it's quite hard to put into words :P – dwinnbrown Dec 22 '15 at 16:46
  • I thought this was going to be off topic but given that "I am creating a post in the background when the user creates a new poll which embeds the poll they have just made with a shortcode" it should be ok. Post the code that creates the post mentioned in that sentence. – s_ha_dum Dec 22 '15 at 16:48
1

The first problem I spotted is that you aren't setting $latest_pollid but you are nonetheless trying to use that as the poll id. You should have something like: $latest_pollid = polls_latest_id(); before that line in order to set the value.

There are big problems with what you are doing though. Hooking to after_setup_theme means that your code runs on every page load. You are restricting most of it to the page titled 'My Example Background Post' but I still suspect you are getting a disconnect between the quiz and the post you are creating. I don't know how to solve this unless the quiz plugin has a hook you can use, or perhaps the $_POST data has information that could be relevant.

  • by declaring $latest_pollid = polls_latest_id(); within the function and by altering the post content code to this 'post_content' => '[poll id=' . $latest_pollid . ']' It is now returning a number and working perfectly. – dwinnbrown Dec 23 '15 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.