Skip to content

Instantly share code, notes, and snippets.

@pramod1988
Created March 29, 2017 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pramod1988/06f8162dbd70b499b8f29eff2f50920e to your computer and use it in GitHub Desktop.
Save pramod1988/06f8162dbd70b499b8f29eff2f50920e to your computer and use it in GitHub Desktop.
Add & save Metabox in wordpress post to enhance functionality
add_action( 'add_meta_boxes', 'add_events_metaboxes' );
// Add the Events Meta Boxes
function add_events_metaboxes() {
add_meta_box('wpt_events_location', 'Event Details', 'wpt_events_location', 'post', 'normal', 'high');
}
function wpt_events_location() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the location data if its already been entered
$url = get_post_meta($post->ID, '_url', true);
$fromdate = get_post_meta($post->ID, '_fromdate', true);
$todate = get_post_meta($post->ID, '_todate', true);
//$address = get_post_meta($post->ID, '_address', true);
//$offer = get_post_meta($post->ID, '_offer', true);
//$status = get_post_meta($post->ID, '_status', true);
$showto = get_post_meta($post->ID, '_showto', true);
if($showto=='yes')
{
$yes = 'selected';
}
else
{
$yes = '';
}
if($showto=='no')
{
$no = 'selected';
}
else
{
$no = '';
}
// Echo out the field
echo '<p>Enter Event Ticket Url</p>';
echo '<input type="text" name="_url" value="' . $url . '" class="widefat" />';
echo '<p>Enter Event From Date (yyyy-mm-dd)</p>';
echo '<input type="text" name="_fromdate" value="' . $fromdate . '" class="widefat" />';
echo '<p>Enter Event To Date (yyyy-mm-dd)</p>';
echo '<input type="text" name="_todate" value="' . $todate . '" class="widefat" />';
echo '<p>Do you want to show to date of event in website</p>';
echo '<select name="_showto" class="widefat">
<option value="">Select Option</option>
<option value="yes" '.$yes.'>Yes</option>
<option value="no" '.$no.'>No</option>
</select>';
//echo '<p>Enter Event Address</p>';
//echo '<input type="text" name="_address" value="' . $address . '" class="widefat" />';
//echo '<p>Add Event Offer</p>';
//echo '<input type="text" name="_offer" value="' . $offer . '" class="widefat" />';
//echo '<p>Add Event Status E.g. (Live Recording,Sold Out,Ticket selling fast)</p>';
//echo '<input type="text" name="_status" value="' . $status . '" class="widefat" />';
}
// Save the Metabox Data
function wpt_save_events_meta($post_id, $post) {
// 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['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$events_meta['_url'] = $_POST['_url'];
$events_meta['_fromdate'] = $_POST['_fromdate'];
$events_meta['_todate'] = $_POST['_todate'];
$events_meta['_showto'] = $_POST['_showto'];
//$events_meta['_address'] = $_POST['_address'];
//$events_meta['_offer'] = $_POST['_offer'];
//$events_meta['_status'] = $_POST['_status'];
// Add values of $events_meta as custom fields
foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
// Remove query string from static files
function remove_cssjs_ver( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
# Show this metabox in template files example below :-
$date = get_post_meta( get_the_ID(), '_fromdate', true );
$url = get_post_meta( get_the_ID(), '_url', true );
$fromdate = get_post_meta( get_the_ID(), '_fromdate', true );
$todate = get_post_meta( get_the_ID(), '_todate', true );
$showto = get_post_meta( get_the_ID(), '_showto', true );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment