Last active
September 16, 2017 08:27
-
-
Save thierrypigot/76e5023da99a71b219e7c78cc13e7a73 to your computer and use it in GitHub Desktop.
WordCamp Marseille 2017 - Les contenus personnalisés dans WordPress - Fichier d'exemple
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: Jokes | |
Plugin URI: https://www.wearewp.pro | |
Description: Manage jokes | |
Author: WeAre[WP] | |
Author URI: https://www.wearewp.pro | |
Text Domain: waw-joke | |
Domain Path: /languages/ | |
Version: 1.0 | |
Date: 15-09-2017 | |
*/ | |
class waw_joke { | |
function __construct() { | |
} | |
function init() { | |
add_action( 'init', array( $this, 'load_textdomain'), 0 ); | |
add_action( 'init', array( $this, 'cpt'), 1 ); | |
add_action( 'init', array( $this, 'custom_taxonomy'), 1 ); | |
add_action( 'add_meta_boxes', array( $this, 'init_metabox'), 1 ); | |
add_action( 'save_post', array( $this, 'save_metabox'), 1 ); | |
add_action('wp_enqueue_scripts', array( $this, 'scripts_and_styles' ), 999); | |
register_activation_hook( __FILE__, array( $this, 'rewrite_flush' ) ); | |
add_filter( 'enter_title_here', array( $this, 'change_title_text' ) ); | |
} | |
function load_textdomain() { | |
load_plugin_textdomain( 'waw-joke', false, basename( dirname( __FILE__ ) ) . '/languages' ); | |
} | |
/** | |
* | |
* Register Custom Post Type | |
* Blague | |
* | |
**/ | |
function cpt() { | |
$labels = array( | |
'name' => _x( 'Jokes', 'post type general name', 'waw-joke' ), | |
'singular_name' => _x( 'Joke', 'post type singular name', 'waw-joke' ), | |
'menu_name' => _x( 'Jokes', 'admin menu', 'waw-joke' ), | |
'name_admin_bar' => _x( 'Joke', 'add new post-type on admin bar', 'waw-joke' ), | |
'add_new' => _x( 'Add New', 'post_type', 'waw-joke' ), | |
'add_new_item' => __( 'Add New joke', 'waw-joke' ), | |
'edit_item' => __( 'Edit joke', 'waw-joke' ), | |
'new_item' => __( 'New joke', 'waw-joke' ), | |
'view_item' => __( 'View joke', 'waw-joke' ), | |
'search_items' => __( 'Search Jokes', 'waw-joke' ), | |
'not_found' => __( 'No jokes found.', 'waw-joke' ), | |
'not_found_in_trash' => __( 'No jokes found in Trash.', 'waw-joke' ), | |
'parent_item_colon' => __( 'Joke parent:', 'waw-joke' ), | |
'all_items' => __( 'All Jokes', 'waw-joke' ), | |
); | |
$rewrite = array( | |
'slug' => 'joke', | |
'with_front' => true, | |
'pages' => false, | |
'feeds' => false, | |
); | |
$capabilities = array( | |
'edit_post' => 'edit_joke', | |
'read_post' => 'read_joke', | |
'delete_post' => 'delete_joke', | |
'edit_posts' => 'edit_jokes', | |
'edit_others_posts' => 'edit_others_jokes', | |
'publish_posts' => 'publish_jokes', | |
'read_private_posts' => 'read_private_jokes', | |
); | |
$supports = array( | |
'title', | |
'editor', | |
'excerpt', | |
'author', | |
'thumbnail', | |
'comments', | |
'trackbacks', | |
'revisions', | |
'custom-fields', | |
'page-attributes', | |
'post-formats' | |
); | |
$args = array( | |
'label' => __( 'Joke', 'waw-joke' ), | |
'description' => __( "Hello i'm the joke description", 'waw-joke' ), | |
'labels' => $labels, | |
'supports' => $supports, | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => false, | |
'show_in_admin_bar' => true, | |
'menu_position' => 5, | |
'menu_icon' => 'dashicons-format-chat', | |
'can_export' => true, | |
'has_archive' => 'joke', | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'rewrite' => $rewrite, | |
'capability_type' => 'post', | |
'map_meta_cap' => null, | |
'register_meta_box_cb'=> null, | |
'taxonomies' => array('family'), | |
'query_var' => true, | |
'capabilities' => $capabilities, | |
); | |
register_post_type( 'joke', $args ); | |
} | |
function init_metabox(){ | |
add_meta_box( 'joke-meta', __( 'Found on this website', 'waw-joke' ), array( $this, 'metabox_joke' ), 'joke', 'side' ); | |
} | |
function metabox_joke( $post ){ | |
$url = get_post_meta($post->ID,'_url_crea',true); | |
echo '<label for="url_meta">'. __('Url:','waw-joke') .' </label>'; | |
echo '<input id="url_meta" type="url" name="url_site" value="'. $url .'" placeholder="http://.....">'; | |
} | |
function save_metabox( $post_id ) { | |
if(isset($_POST['url_site'])) { | |
update_post_meta( $post_id, '_url_crea', esc_url( $_POST['url_site'] ) ); | |
} | |
} | |
// Register Custom Family | |
function custom_taxonomy() { | |
$labels = array( | |
'name' => _x( 'Families', 'Family General Name', 'text_domain' ), | |
'singular_name' => _x( 'Family', 'Family Singular Name', 'text_domain' ), | |
'menu_name' => __( 'Family', 'text_domain' ), | |
'all_items' => __( 'All Items', 'text_domain' ), | |
'parent_item' => __( 'Parent Item', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), | |
'new_item_name' => __( 'New Item Name', 'text_domain' ), | |
'add_new_item' => __( 'Add New Item', 'text_domain' ), | |
'edit_item' => __( 'Edit Item', 'text_domain' ), | |
'update_item' => __( 'Update Item', 'text_domain' ), | |
'view_item' => __( 'View Item', 'text_domain' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ), | |
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ), | |
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ), | |
'popular_items' => __( 'Popular Items', 'text_domain' ), | |
'search_items' => __( 'Search Items', 'text_domain' ), | |
'not_found' => __( 'Not Found', 'text_domain' ), | |
'no_terms' => __( 'No items', 'text_domain' ), | |
'items_list' => __( 'Items list', 'text_domain' ), | |
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
); | |
register_taxonomy( 'family', array( 'joke' ), $args ); | |
} | |
function rewrite_flush() { | |
flush_rewrite_rules(); | |
} | |
function change_title_text( $title ) { | |
$screen = get_current_screen(); | |
if ( 'joke' == $screen->post_type ) { | |
$title = __( 'Joke title', 'waw-joke' ); | |
} | |
return $title; | |
} | |
function scripts_and_styles() { | |
wp_enqueue_style( 'slick', plugin_dir_url( __FILE__ ) . 'slick/slick.css', array(), '', 'all' ); | |
} | |
} | |
$waw_joke = new waw_joke(); | |
$waw_joke->init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment