Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: JJStreet App News Control
- * Description: Enables hiding news from app or from web. Custom witten for the best dance studio in the world -- JJStreet!
- * Version: 1.0
- * Author: Hemles Gangsterz
- * Author URI: https://hemles.com
- */
- /* Define the custom box */
- add_action( 'add_meta_boxes', 'wpse_61041_add_custom_box' );
- /* Do something with the data entered */
- add_action( 'save_post', 'wpse_61041_save_postdata' );
- /* Adds a box to the main column on the Post and Page edit screens */
- function wpse_61041_add_custom_box() {
- add_meta_box(
- 'wpse_61041_sectionid',
- 'Äppis kuvamine',
- 'wpse_61041_inner_custom_box',
- 'post',
- 'side',
- 'high'
- );
- }
- /* Prints the box content */
- function wpse_61041_inner_custom_box($post)
- {
- // Use nonce for verification
- wp_nonce_field( 'wpse_61041_wpse_61041_field_nonce', 'wpse_61041_noncename' );
- // Get saved value, if none exists, "default" is selected
- $saved = get_post_meta( $post->ID, 'app_news_control', true);
- if( !$saved )
- $saved = 'both';
- $fields = array(
- 'web' => __('Ainult veebis', 'wpse'),
- 'app' => __('Ainult äppis', 'wpse'),
- 'both' => __('Mõlemas', 'wpse'),
- );
- foreach($fields as $key => $label)
- {
- printf(
- '<input type="radio" name="app_news_control" value="%1$s" id="app_news_control[%1$s]" %3$s />'.
- '<label for="app_news_control[%1$s]"> %2$s ' .
- '</label><br>',
- esc_attr($key),
- esc_html($label),
- checked($saved, $key, false)
- );
- }
- }
- /* When the post is saved, saves our custom data */
- function wpse_61041_save_postdata( $post_id )
- {
- // verify if this is an auto save routine.
- // If it is our form has not been submitted, so we dont want to do anything
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
- return;
- // verify this came from the our screen and with proper authorization,
- // because save_post can be triggered at other times
- if ( !wp_verify_nonce( $_POST['wpse_61041_noncename'], 'wpse_61041_wpse_61041_field_nonce' ) )
- return;
- if ( isset($_POST['app_news_control']) && $_POST['app_news_control'] != "" ){
- update_post_meta( $post_id, 'app_news_control', $_POST['app_news_control'] );
- }
- }
- /* Hides news, that have only app value: */
- function my_pre_get_posts($query) {
- // Check this is main query and other conditionals as needed
- if($query->is_main_query()) {
- $query->set(
- 'meta_query',
- array(
- 'relation' => 'OR',
- array(
- 'key' => 'app_news_control',
- 'value' => array('both', 'web'),
- ),
- array(
- 'key' => 'app_news_control',
- 'compare' => 'NOT EXISTS'
- )
- )
- );
- }
- }
- /* This should not be for admin: */
- if (!is_admin()) add_action('pre_get_posts' , 'my_pre_get_posts');
Add Comment
Please, Sign In to add comment