Skip to content

Instantly share code, notes, and snippets.

@apisandipas
Last active December 27, 2015 15:29
Show Gist options
  • Save apisandipas/7348523 to your computer and use it in GitHub Desktop.
Save apisandipas/7348523 to your computer and use it in GitHub Desktop.
WordPress plugin development helper functions..
/**
* Determine if the current user has the relevant permissions
*
* @param $post_id
* @return bool
*/
private function canSaveData( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
     return false;
if ( 'page' == $_POST['post_type'] ) {
     if ( ! current_user_can( 'edit_page', $post_id ) )
         return false;
} else {
     if ( ! current_user_can( 'edit_post', $post_id ) )
         return false;
}
return true;
}
/**
* Register a Custom Post Type
*
* @param $single
* @param $plural
* @param null $supports
*/
private function registerPostType( $single, $supports = null ) {
$args = array(
     'labels' => $this->auto_labels( $single ),
     'public' => true,
     'publicly_queryable' => true,
     'show_ui' => true,
     'show_in_menu' => true,
     'query_var' => true,
     'rewrite' => true,
     'capability_type' => 'post',
     'has_archive' => true,
     'hierarchical' => false,
     'menu_position' => null,
     'supports' => ( $supports ) ? $supports : array( 'title', 'editor', 'page-attributes' )
);
register_post_type( $single, $args );
}
/**
* Generates Labels for Custom Post Types
*
* @param string $singular
* @param string $plural
* @return array
*/
function auto_labels( $singular, $plural = '' ) {
if ( empty( $plural ) )
$plural = $singular . 's';
return array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'all_items' => $plural,
'edit_item' => 'Edit ' . $plural,
'add_new_item' => 'Add New ' . $plural,
'menu_name' => $plural,
'new_item' => 'New ' . $singular,
'view_item' => 'View ' . $plural,
'not_found' => 'No ' . $plural . ' found',
'not_found_in_trash' => 'No ' . $plural . ' found in Trash',
'parent_item_colon' => '',
);
}
/**
* Render a Template File
*
* @param $filePath
* @param null $viewData
* @return string
*/
public function getTemplatePart( $filePath, $viewData = null ) {
( $viewData ) ? extract( $viewData ) : null;
ob_start();
include ( "$filePath" );
$template = ob_get_contents();
ob_end_clean();
return $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment