Created
September 21, 2015 23:46
-
-
Save johnReeve/ea1e5fcc1940eccf2796 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Here is some functionality specific to the contact page. | |
* | |
*/ | |
class Prep_Contact_Page { | |
private $current_screen; | |
private $textdomain = 'foundationpress'; | |
private $metabox_id = 'contact_page_metabox'; | |
public function __construct($current_screen) { | |
$this->current_screen = $current_screen; | |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); | |
add_action('admin_head', [$this, 'hide_contact_page_meta' ] ); | |
add_action( 'save_post', array( $this, 'save' ) ); | |
} | |
/** | |
* Adds the meta box container. | |
*/ | |
public function add_meta_box( $post_type ) { | |
$post_types = array('page'); //limit meta box to certain post types | |
if ( in_array( $post_type, $post_types )) { | |
add_meta_box( | |
$this->metabox_id | |
,__( 'Contact Page Information', $this->textdomain ) | |
,array( $this, 'render_meta_box_content' ) | |
,$post_type | |
,'advanced' | |
,'high' | |
); | |
} | |
} | |
/** | |
* Render Meta Box content. | |
* | |
* @param WP_Post $post The post object. | |
*/ | |
public function render_meta_box_content( $post ) { | |
// Add an nonce field so we can check for it later. | |
wp_nonce_field( $this->metabox_id, $this->metabox_id . '_nonce' ); | |
// Use get_post_meta to retrieve an existing value from the database. | |
$contact_page_address_box = get_post_meta( $post->ID, '_contact_page_address_box', true ); | |
// Display the form, using the current value. | |
echo '<label for="_contact_page_address_box">'; | |
_e( 'Contact Page: Address', $this->textdomain ); | |
echo '</label><br>'; | |
echo '<textarea id="_contact_page_address_box" name="_contact_page_address_box" style="width:100%;min-height:200px">'; | |
echo esc_html( $contact_page_address_box ); | |
echo '</textarea>'; | |
} | |
/** | |
* Save the meta when the post is saved. | |
* | |
* @param int $post_id The ID of the post being saved. | |
*/ | |
public function save( $post_id ) { | |
/* | |
* We need to verify this came from the our screen and with proper authorization, | |
* because save_post can be triggered at other times. | |
*/ | |
// Check if our nonce is set. | |
if ( ! isset( $_POST[$this->metabox_id . '_nonce' ] ) ) | |
return $post_id; | |
$nonce = $_POST[$this->metabox_id . '_nonce' ]; | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $nonce, $this->metabox_id ) ) | |
return $post_id; | |
// If this is an autosave, our form has not been submitted, | |
// so we don't want to do anything. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return $post_id; | |
// Check the user's permissions. | |
if ( 'page' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) | |
return $post_id; | |
} else { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) | |
return $post_id; | |
} | |
/* OK, its safe for us to save the data now. */ | |
// Sanitize the user input. | |
$contact_page_address_box = balanceTags( $_POST['_contact_page_address_box'] ); | |
// Update the meta field. | |
update_post_meta( $post_id, '_contact_page_address_box', $contact_page_address_box ); | |
} | |
/* | |
* Change Meta Box visibility according to Page Template | |
* | |
* Observation: this example swaps the Featured Image meta box visibility | |
* | |
* Usage: | |
* - adjust $('#postimagediv') to your meta box | |
* - change 'page-portfolio.php' to your template's filename | |
* - remove the console.log outputs | |
*/ | |
public function hide_contact_page_meta() { | |
if('page' != $this->current_screen->id) return; | |
echo <<<HTML | |
<script type="text/javascript"> | |
jQuery(document).ready( function($) { | |
var metabox = $("#{$this->metabox_id}"), | |
trigger_value = "templates/page-contact.php"; | |
/** | |
* Adjust visibility of the meta box at startup | |
*/ | |
if($('#page_template').val() == trigger_value) { | |
// show the meta box | |
metabox.show(); | |
} else { | |
// hide your meta box | |
metabox.hide(); | |
} | |
/** | |
* Live adjustment of the meta box visibility | |
*/ | |
$('#page_template').live('change', function(){ | |
if($(this).val() == trigger_value) { | |
// show the meta box | |
metabox.show(); | |
} else { | |
// hide your meta box | |
metabox.hide(); | |
} | |
}); | |
}); | |
</script> | |
HTML; | |
} | |
} | |
add_action( 'current_screen', function () { | |
global $current_screen; | |
$contactPage = new Prep_Contact_Page($current_screen); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment