Guest User

Untitled

a guest
Aug 1st, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: JJStreet App News Control
  4. * Description: Enables hiding news from app or from web. Custom witten for the best dance studio in the world -- JJStreet!
  5. * Version: 1.0
  6. * Author: Hemles Gangsterz
  7. * Author URI: https://hemles.com
  8. */
  9.  
  10. /* Define the custom box */
  11. add_action( 'add_meta_boxes', 'wpse_61041_add_custom_box' );
  12.  
  13. /* Do something with the data entered */
  14. add_action( 'save_post', 'wpse_61041_save_postdata' );
  15.  
  16. /* Adds a box to the main column on the Post and Page edit screens */
  17. function wpse_61041_add_custom_box() {
  18. add_meta_box(
  19. 'wpse_61041_sectionid',
  20. 'Äppis kuvamine',
  21. 'wpse_61041_inner_custom_box',
  22. 'post',
  23. 'side',
  24. 'high'
  25. );
  26. }
  27.  
  28. /* Prints the box content */
  29. function wpse_61041_inner_custom_box($post)
  30. {
  31. // Use nonce for verification
  32. wp_nonce_field( 'wpse_61041_wpse_61041_field_nonce', 'wpse_61041_noncename' );
  33.  
  34. // Get saved value, if none exists, "default" is selected
  35. $saved = get_post_meta( $post->ID, 'app_news_control', true);
  36. if( !$saved )
  37. $saved = 'both';
  38.  
  39. $fields = array(
  40. 'web' => __('Ainult veebis', 'wpse'),
  41. 'app' => __('Ainult äppis', 'wpse'),
  42. 'both' => __('Mõlemas', 'wpse'),
  43. );
  44.  
  45. foreach($fields as $key => $label)
  46. {
  47. printf(
  48. '<input type="radio" name="app_news_control" value="%1$s" id="app_news_control[%1$s]" %3$s />'.
  49. '<label for="app_news_control[%1$s]"> %2$s ' .
  50. '</label><br>',
  51. esc_attr($key),
  52. esc_html($label),
  53. checked($saved, $key, false)
  54. );
  55. }
  56. }
  57.  
  58. /* When the post is saved, saves our custom data */
  59. function wpse_61041_save_postdata( $post_id )
  60. {
  61. // verify if this is an auto save routine.
  62. // If it is our form has not been submitted, so we dont want to do anything
  63. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  64. return;
  65.  
  66. // verify this came from the our screen and with proper authorization,
  67. // because save_post can be triggered at other times
  68. if ( !wp_verify_nonce( $_POST['wpse_61041_noncename'], 'wpse_61041_wpse_61041_field_nonce' ) )
  69. return;
  70.  
  71. if ( isset($_POST['app_news_control']) && $_POST['app_news_control'] != "" ){
  72. update_post_meta( $post_id, 'app_news_control', $_POST['app_news_control'] );
  73. }
  74. }
  75.  
  76. /* Hides news, that have only app value: */
  77. function my_pre_get_posts($query) {
  78.  
  79. // Check this is main query and other conditionals as needed
  80. if($query->is_main_query()) {
  81. $query->set(
  82. 'meta_query',
  83. array(
  84. 'relation' => 'OR',
  85. array(
  86. 'key' => 'app_news_control',
  87. 'value' => array('both', 'web'),
  88. ),
  89. array(
  90. 'key' => 'app_news_control',
  91. 'compare' => 'NOT EXISTS'
  92. )
  93. )
  94. );
  95. }
  96. }
  97.  
  98. /* This should not be for admin: */
  99. if (!is_admin()) add_action('pre_get_posts' , 'my_pre_get_posts');
Add Comment
Please, Sign In to add comment