• Resolved cyberlp23

    (@cyberlp23)


    Hello,

    I’ve bought the Pro version of TotalContest.

    I’m trying to retrieve the average rate variable for each submission in a contest in order to display something in PHP in my WordPress page.

    How would I be able to do that?

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter cyberlp23

    (@cyberlp23)

    And another PHP question: is there a way to input PHP variables into the TotalContest fields?
    I’ve tried with Custom block but it does not seem to work.

    Plugin Author TotalSuite

    (@totalsuite)

    Hi @cyberlp23

    You can use the metadata attached to the submission:

    get_post_meta(SUBMISSION_ID, '_tc_rate', true);

    As for the second question, could you please elaborate on the use case?

    I hope this answers your questions! If you need any more clarification, please don’t hesitate to let us know.

    Regards,

    Thread Starter cyberlp23

    (@cyberlp23)

    Thank you for your answer, I will try the get_post_meta. Also, the idea with retrieving the average_rate is to display a leaderboard. It says in your faq that it is possible to do it, but how ? 🙂

    As for the second question:
    – I have a PHP variable $team_id which is defined in my custom WP template where I use TotalSuite (it’s stored in a PHP session + cookie when user logs in). For example, $team_id = 37.
    – Is there a way to display that value on their submissions?

    • This reply was modified 1 year, 5 months ago by cyberlp23.
    Thread Starter cyberlp23

    (@cyberlp23)

    The first question is solved, but not the second.

    Any way of using PHP variables in the TotalContest custom fields?

    Plugin Author TotalSuite

    (@totalsuite)

    Hi,

    You can use JS to populate the value after adding the team_id field to the participation form (via the contest editor). Afterwards, you can use that field in the submission view blocks to display its value.

    Sample:

    add_action( 'wp_enqueue_scripts', function () {
    $teamId = 0; // Retrieve it from session or other source
    wp_add_inline_script( 'totalcontest-frontend', sprint('document.querySelector("#team-id-field").value = "%s"', $teamId); );
    } );

    I hope it helps! If you need any more help, please don’t hesitate to let me know.

    Regards,

    Thread Starter cyberlp23

    (@cyberlp23)

    Thanks. I tweaked your script a little bit to make it work.

    add_action( 'wp_enqueue_scripts', function () {

      $handle = 'totalcontest-frontend';

      $script_url = plugins_url( 'totalcontest/assets/dist/scripts/frontend.js', __FILE__ );

      wp_enqueue_script( $handle, $script_url, array(), null, true );

      if(isset($_SESSION['team_id'])) { $teamId = $_SESSION['team_id']; } else { $teamId = 0; }

      $inline_script = sprintf(

          'document.addEventListener("DOMContentLoaded", function() {

              var teamIdField = document.querySelector("#id_equipe-field");

              if (teamIdField) {

                  teamIdField.value = "%d";

              }

          });',

          $teamId,

          $teamId

      );

      wp_add_inline_script( $handle, $inline_script );

    }, 20 );


    Now, is there a way to display this as a pre-filled, non-modifiable input field in the Participate page? (that I still will be able to display on the Submission page)

    • This reply was modified 1 year, 5 months ago by cyberlp23.
    Thread Starter cyberlp23

    (@cyberlp23)

    I found the way ^^

    add_action( 'wp_enqueue_scripts', function () {

      $handle = 'totalcontest-frontend';

      $script_url = plugins_url( 'totalcontest/assets/dist/scripts/frontend.js', __FILE__ );

      wp_enqueue_script( $handle, $script_url, array(), null, true );

      $teamId = isset($_SESSION['team_id']) ? $_SESSION['team_id'] : 0;

      $inline_script = sprintf(

          'document.addEventListener("DOMContentLoaded", function() {

              var teamIdField = document.querySelector("#id_equipe-field");

              if (teamIdField) {

                  teamIdField.value = "%d";

                  teamIdField.setAttribute("readonly", "readonly");

              }

          });',

          $teamId

      );

      wp_add_inline_script( $handle, $inline_script );

    }, 20 );



    Plugin Author TotalSuite

    (@totalsuite)

    Hi @cyberlp23

    That’s great! Thanks for sharing the snippet.

    Regards,

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    I’ve bought the Pro version of TotalContest.

    Closing this topic as @totalsuite knows they must not support customers on this site. This site is only for users of the opensource plugin and not someone who paid the developer.

    For pro or commercial product support please contact the developer directly on their site. This includes any pre-sales topics as well.

    As the developer is aware, commercial products are not supported in these forums. I am sure they will have no problem supporting you there.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Use TotalContest variable in PHP’ is closed to new replies.