Last active
August 29, 2015 14:17
-
-
Save jasperf/6d6fd33585ecbea92141 to your computer and use it in GitHub Desktop.
Portfolio Plugin based on WordPress Custom Post Type, custom Taxonomy and post_type_link filer #wordpress
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 | |
/** | |
* Plugin Name: Imagewize Portfolio Plugin | |
* Plugin URI: https://imagewize.com | |
* Description: A simple portfolio plugin | |
* Version: 1.0.0 | |
* Author: Jasper Frumau | |
* Author URI: https://imagewize.com | |
* Text Domain: Optional. Plugin's text domain for localization. Example: mytextdomain | |
* Domain Path: Optional. Plugin's relative directory path to .mo files. Example: /locale/ | |
* Network: Optional. Whether the plugin can only be activated network wide. Example: true | |
* License: GPL2+ | |
*/ | |
// Register Custom Taxonomy | |
function portfolio_taxonomy() { | |
$labels = array( | |
'name' => _x( 'Labels', 'Taxonomy General Name', 'imagewize_portfolio_plugin' ), | |
'singular_name' => _x( 'Label', 'Taxonomy Singular Name', 'imagewize_portfolio_plugin' ), | |
'menu_name' => __( 'Labels', 'imagewize_portfolio_plugin' ), | |
'all_items' => __( 'All Items', 'imagewize_portfolio_plugin' ), | |
'parent_item' => __( 'Parent Item', 'imagewize_portfolio_plugin' ), | |
'parent_item_colon' => __( 'Parent Item:', 'imagewize_portfolio_plugin' ), | |
'new_item_name' => __( 'New Item Name', 'imagewize_portfolio_plugin' ), | |
'add_new_item' => __( 'Add New Item', 'imagewize_portfolio_plugin' ), | |
'edit_item' => __( 'Edit Item', 'imagewize_portfolio_plugin' ), | |
'update_item' => __( 'Update Item', 'imagewize_portfolio_plugin' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'imagewize_portfolio_plugin' ), | |
'search_items' => __( 'Search Items', 'imagewize_portfolio_plugin' ), | |
'add_or_remove_items' => __( 'Add or remove items', 'imagewize_portfolio_plugin' ), | |
'choose_from_most_used' => __( 'Choose from the most used items', 'imagewize_portfolio_plugin' ), | |
'not_found' => __( 'Not Found', 'imagewize_portfolio_plugin' ), | |
); | |
$rewrite = array( | |
'slug' => 'label', | |
'with_front' => true, | |
'hierarchical' => true, | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
'rewrite' => $rewrite, | |
); | |
register_taxonomy( 'label', array( 'Img_portfolio_cpt' ), $args ); | |
} | |
// Hook into the 'init' action | |
add_action( 'init', 'portfolio_taxonomy', 0 ); | |
if ( ! function_exists('img_portfolio_cpt') ) { | |
// Register Custom Post Type | |
function img_portfolio_cpt() { | |
$labels = array( | |
'name' => _x( 'Portfolio Items', 'Post Type General Name', 'imagewize_portfolio_plugin' ), | |
'singular_name' => _x( 'Portfolio', 'Post Type Singular Name', 'imagewize_portfolio_plugin' ), | |
'menu_name' => __( 'Portfolio', 'imagewize_portfolio_plugin' ), | |
'parent_item_colon' => __( 'Parent Item:', 'imagewize_portfolio_plugin' ), | |
'all_items' => __( 'All Items', 'imagewize_portfolio_plugin' ), | |
'view_item' => __( 'View Item', 'imagewize_portfolio_plugin' ), | |
'add_new_item' => __( 'Add New Item', 'imagewize_portfolio_plugin' ), | |
'add_new' => __( 'Add New', 'imagewize_portfolio_plugin' ), | |
'edit_item' => __( 'Edit Item', 'imagewize_portfolio_plugin' ), | |
'update_item' => __( 'Update Item', 'imagewize_portfolio_plugin' ), | |
'search_items' => __( 'Search Item', 'imagewize_portfolio_plugin' ), | |
'not_found' => __( 'Not found', 'imagewize_portfolio_plugin' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'imagewize_portfolio_plugin' ), | |
); | |
$rewrite = array( | |
'slug' => 'portfolio/%label%', | |
'with_front' => true, | |
'pages' => true, | |
'feeds' => true, | |
); | |
$args = array( | |
'label' => __( 'Img_portfolio_cpt', 'imagewize_portfolio_plugin' ), | |
'description' => __( 'Imagewize Portfolio', 'imagewize_portfolio_plugin' ), | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', ), | |
'taxonomies' => array( 'label' ), | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => 5, | |
'menu_icon' => 'dashicons-portfolio', | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'rewrite' => $rewrite, | |
'capability_type' => 'page', | |
); | |
register_post_type( 'Img_portfolio_cpt', $args ); | |
} | |
// Hook into the 'init' action | |
add_action( 'init', 'img_portfolio_cpt', 0 ); | |
} | |
add_filter('post_type_link', 'portfolio_permalink_structure', 10, 4); | |
function portfolio_permalink_structure($post_link, $post, $leavename, $sample) | |
{ | |
if ( false !== strpos( $post_link, '%label%' ) && !empty($lable_term) ) { | |
$label_term = get_the_terms( $post->ID, 'label' ); | |
$post_link = str_replace( '%label%', array_pop( $label_term )->slug, $post_link ); | |
} | |
return $post_link; | |
} | |
/** | |
* Adds a box to the main column on the Portfolio Page. | |
*/ | |
function myplugin_add_meta_box() { | |
$screens = array( 'Img_portfolio_cpt' ); | |
foreach ( $screens as $screen ) { | |
add_meta_box( | |
'myplugin_sectionid', | |
__( 'My Post Section Title', 'imagewize_portfolio_plugin' ), | |
'myplugin_meta_box_callback', | |
$screen | |
); | |
} | |
} | |
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' ); | |
/** | |
* Prints the box content. | |
* | |
* @param WP_Post $post The object for the current post/page. | |
*/ | |
function myplugin_meta_box_callback( $post ) { | |
// Add an nonce field so we can check for it later. | |
wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' ); | |
/* | |
* Use get_post_meta() to retrieve an existing value | |
* from the database and use the value for the form. | |
*/ | |
$value = get_post_meta( $post->ID, '_my_meta_value_key', true ); | |
echo '<label for="myplugin_new_field">'; | |
_e( 'Description for this field', 'myplugin_textdomain' ); | |
echo '</label> '; | |
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />'; | |
} | |
/** | |
* When the post is saved, saves our custom data. | |
* | |
* @param int $post_id The ID of the post being saved. | |
*/ | |
function myplugin_save_meta_box_data( $post_id ) { | |
/* | |
* We need to verify this came from our screen and with proper authorization, | |
* because the save_post action can be triggered at other times. | |
*/ | |
// Check if our nonce is set. | |
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) { | |
return; | |
} | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) { | |
return; | |
} | |
// 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; | |
} | |
// Check the user's permissions. | |
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) { | |
return; | |
} | |
} else { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return; | |
} | |
} | |
/* OK, it's safe for us to save the data now. */ | |
// Make sure that it is set. | |
if ( ! isset( $_POST['myplugin_new_field'] ) ) { | |
return; | |
} | |
// Sanitize user input. | |
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] ); | |
// Update the meta field in the database. | |
update_post_meta( $post_id, '_my_meta_value_key', $my_data ); | |
} | |
add_action( 'save_post', 'myplugin_save_meta_box_data' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment