acf_srf_create_field

Create the HTML interface "star rating" field.

Description

(string) acf_srf_create_field( (array) $value, (array) $field ); 

Returns (string)

$field - HTML interface "star rating" field

Parameters (2)

0. $value (array)
- rating average value and votes count
1. $field (array)
- field options

Usage

  1. if ( !function_exists( 'acf_srf_create_field' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'advanced-custom-fields-star-rating-field/functions.php'; 
  3.  
  4. // - rating average value and votes count 
  5. $value = array(); 
  6.  
  7. // - field options 
  8. $field = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = acf_srf_create_field($value, $field); 
  12.  

Defined (1)

The function is defined in the following location(s).

/functions.php  
  1. function acf_srf_create_field( $value = array(), $field ) { 
  2.  
  3. $image_star_url = plugins_url( '/images/stars.png', __FILE__ ); 
  4. $image_loader_url = plugins_url( '/images/ajax-loader.gif', __FILE__ );  
  5.  
  6. $number_of_stars = ( ! empty($field['number_stars']) ? $field['number_stars']: 0 ); 
  7.  
  8. // Votes result and count 
  9. $avrg = ( ! empty($value['avrg']) ? 0+$value['avrg']: 0 ); 
  10. $votes = ( ! empty($value['votes']) ? 0+$value['votes']: 0 ); 
  11.  
  12. // Rating activity flag. Default TRUE - rating inactive. 
  13. $readOnly_flag = 1; 
  14.  
  15. // If you on the admin page and post-id is not defined - rating inactive. 
  16. if ( ! is_admin() && ! empty($field['post-id']) ) { 
  17.  
  18. if ( ! empty($field['key']) )  
  19.  
  20. $permission_flag = acf_srf_check_permission($field, $field['post-id']); 
  21.  
  22. if ( $permission_flag == 1 ) { 
  23. $readOnly_flag = 0; 
  24.  
  25. }  
  26.  
  27.  
  28. $rand = rand(10, 99999); 
  29.  
  30. $html = '<div class="acf-input-wrap">'; 
  31. // These fields are needed for ACF to save the field value when editing 
  32. // object (post, user etc). 
  33. $html .= ' <input name="' . $field['name'] . '[avrg]" value="' . $avrg . '" id="' . $field['id'] . '-' . $rand . '" type="hidden"/> '; 
  34. $html .= ' <input name="' . $field['name'] . '[votes]" value="' . $votes . '" id="' . $field['id'] . '-' . $rand . '-votes" type="hidden"/> ';  
  35. // These fields are used jQuery script to pass values. 
  36. $html .= ' <div id="rating_' . $field['key'] . '-' . $rand . '">'; 
  37. $html .= ' <input name="vote-id" value="' . $field['key'] . '" type="hidden">'; 
  38. $html .= ' <input name="val" value="' . $avrg . '" type="hidden">'; 
  39. $html .= ' <input name="votes" value="' . $votes . '" type="hidden">'; 
  40.  
  41. if ( ! empty($field['post-id']) ) { 
  42. $html .= ' <input name="post-id" value="' . $field['post-id'] . '" id="post-id" type="hidden"/> '; 
  43.  
  44. $html .= ' </div>'; 
  45. $html .= ' <script type="text/javascript" >'; 
  46. $html .= ' jQuery(function() {'; 
  47. $html .= ' jQuery("#rating_' . $field['key'] . '-' . $rand . '").rating({'; 
  48. $html .= ' fx: "full", '; 
  49. $html .= ' readOnly: ' . $readOnly_flag .', '; 
  50. $html .= ' image: "' . $image_star_url .'", '; 
  51. $html .= ' loader: "' . $image_loader_url .'", '; 
  52. $html .= ' stars: "' . $number_of_stars .'", '; 
  53. // AJAX processing only for the active rating. 
  54. if ( $readOnly_flag == 0 ) { 
  55. $html .= ' url: "srfajax.url", '; 
  56. $html .= ' callback: function(responce) {'; 
  57. $html .= ' this.vote_success.fadeOut(2000);'; 
  58. // alert answer 
  59. // $html .= " alert('***** ***: '+this._data.val);"; 
  60. $html .= ' }'; 
  61.  
  62. $html .= ' });'; 
  63. $html .= ' });'; 
  64. $html .= ' </script>'; 
  65. $html .= '</div>'; 
  66.  
  67. return $html; 
  68.