admin_url( 'admin-ajax.php' ), // generate a nonce with a unique ID "myajax-post-comment-nonce" // so that you can check it later when an AJAX request is sent 'amsaMobileNonce' => wp_create_nonce( 'amsa-mobile-nonce' ) ) ); } static function amsa_sample_metaboxes( $meta_boxes ) { $prefix = '_amsa_'; // Prefix for all fields $meta_boxes[] = array( 'id' => 'show_mobile_slidead', 'title' => 'Show in Mobile SlideAd?', 'pages' => array('post'), // post type 'context' => 'normal', 'priority' => 'low', 'show_names' => true, // Show field names on the left 'fields' => array( array( 'name' => __( 'Show?', 'cmb' ), 'desc' => __( 'Tick if you would like this to be included in the randomised selection for posts in the mobile slidead', 'cmb' ), 'id' => $prefix . 'show_mobile_slidead', 'type' => 'checkbox', ), ), ); return $meta_boxes; } /** * Take care of the settings that can be set */ public function register_admin_settings() { //register our settings register_setting( 'amsa-settings-group', 'amsa_posts_to_show' ); register_setting( 'amsa-settings-group', 'amsa_box_title' ); } /** * Take care of the settings link */ public function add_admin_settings() { // ... add_options_page( 'Mobile SlideAd Options', 'Mobile SlideAd', 'manage_options', 'amsa-settings-group', array( $this, 'amsa_admin_options' ) ); } /** * Take care of the settings link */ public function amsa_admin_options() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); } echo '
'; echo '

Mobile SlideAd

'; echo '
'; settings_fields( 'amsa-settings-group' ); do_settings_sections( 'amsa-settings-group' ); echo ''; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo '
How Many Posts to Display
SlideAd Box Title
'; submit_button(); echo '
'; echo '
'; } /** * Get content for the slidead div */ public function amsa_ajax_action_callback() { $nonce = $_POST['amsaMobileNonce']; // check to see if the submitted nonce matches with the // generated nonce we created earlier if ( ! wp_verify_nonce( $nonce, 'amsa-mobile-nonce' ) ) die ( 'Busted!'); global $wpdb; // this is how you get access to the database $limit = get_option('amsa_posts_to_show'); $myquery = new WP_Query( "meta_key=_amsa_show_mobile_slidead&meta_value=on&orderby=rand&posts_per_page=".(!empty($limit) ? $limit : 1) ); if ($myquery->have_posts()) { echo '
' . get_option('amsa_box_title') . '
'; while ( $myquery->have_posts() ) { $myquery->the_post(); echo '
'; if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. echo ''; the_post_thumbnail( array(100) ); echo ''; } echo ''; echo '
' . get_the_title() . '
'; echo '
'; echo '
'; echo '
'; } } else { echo "false"; } wp_reset_postdata(); wp_die(); // this is required to terminate immediately and return a proper response } } // finally instantiate our plugin class and add it to the set of globals $GLOBALS['wp_amsa'] = new WP_AMSA(); }