Created
October 3, 2022 09:52
-
-
Save ikamal7/17ae43805ec9fea5c4d0fa9e737c7ac2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
namespace WpAuthor\Src\Admin; | |
class Admin_Manager { | |
use \WpAuthor\Traits\Singleton; | |
public function init() | |
{ | |
add_action('init', [ $this, 'register_author_cpt'] ); | |
add_filter( 'archive_template', [ $this, 'get_custom_post_type_template'] ) ; | |
add_filter('single_template', [ $this, 'author_single_template' ] ); | |
// add_action('init', [ $this, 'custom_rewrite_basic' ]); | |
// add_action('init', [ $this, 'custom_rewrite_tag' ]); | |
// add_filter( 'post_type_link', [ $this, 'author_post_type_link' ], 20, 2 ); | |
} | |
public function register_author_cpt() | |
{ | |
$labels = array( | |
'name' => _x( 'Authors','author-info' ), | |
'singular_name' => _x( 'Author', 'author-info' ), | |
'archives' => __( 'Item Archives', 'author-info' ), | |
'attributes' => __( 'Item Attributes', 'author-info' ), | |
'parent_item_colon' => __( 'Parent Item:', 'author-info' ), | |
'all_items' => __( 'All Items', 'author-info' ), | |
'add_new_item' => __( 'Add New Item', 'author-info' ), | |
'add_new' => __( 'Add New', 'author-info' ), | |
'new_item' => __( 'New Item', 'author-info' ), | |
'edit_item' => __( 'Edit Item', 'author-info' ), | |
'update_item' => __( 'Update Item', 'author-info' ), | |
'view_item' => __( 'View Item', 'author-info' ), | |
'view_items' => __( 'View Items', 'author-info' ), | |
'search_items' => __( 'Search Item', 'author-info' ), | |
'not_found' => __( 'Not found', 'author-info' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'author-info' ), | |
'featured_image' => __( 'Featured Image', 'author-info' ), | |
'set_featured_image' => __( 'Set featured image', 'author-info' ), | |
'remove_featured_image' => __( 'Remove featured image', 'author-info' ), | |
'use_featured_image' => __( 'Use as featured image', 'author-info' ), | |
'insert_into_item' => __( 'Insert into item', 'author-info' ), | |
'uploaded_to_this_item' => __( 'Uploaded to this item', 'author-info' ), | |
'items_list' => __( 'Items list', 'author-info' ), | |
'items_list_navigation' => __( 'Items list navigation', 'author-info' ), | |
'filter_items_list' => __( 'Filter items list', 'author-info' ), | |
); | |
$args = array( | |
'label' => __( 'Author', 'author-info' ), | |
'labels' => $labels, | |
'supports' => array( 'title' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'can_export' => true, | |
'has_archive' => 'authors', | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'capability_type' => 'page', | |
'rewrite' =>[ | |
// 'slug' => 'authors/%first_name%-%last_name%', | |
'slug' => 'authors', | |
] | |
); | |
register_post_type( 'author', $args ); | |
flush_rewrite_rules( true ); | |
} | |
public function get_custom_post_type_template( $archive_template ) { | |
global $post; | |
$plugin_root_dir = WPAI_PLUGIN_DIR. '/templates/'; | |
if (is_archive() && get_post_type( $post ) == 'author') { | |
$archive_template = $plugin_root_dir.'/author-archive.php'; | |
} | |
return $archive_template; | |
} | |
public function author_single_template( $single_template ) { | |
global $post; | |
$plugin_root_dir = WPAI_PLUGIN_DIR. '/templates/'; | |
if(is_single() && get_post_type($post) === 'author'){ | |
if(file_exists( $plugin_root_dir.'/author-single.php' )){ | |
$single_template = $plugin_root_dir.'/author-single.php'; | |
} | |
} | |
return $single_template; | |
} | |
// Add rewrite rule | |
function custom_rewrite_basic() { | |
add_rewrite_rule( | |
'^authors/%first_name%-%last_name%/', | |
//'index.php?post_type=movies&movie_id=$matches[1]', | |
'index.php?post_type=author&first_name=$matches[1]&last_name=$matches[2]', | |
'top' | |
); | |
} | |
// Add custom rewrite tag 'movie_id' | |
function custom_rewrite_tag() { | |
add_rewrite_tag('%first_name%', '([^/]+)', 'first_name='); | |
add_rewrite_tag('%last_name%', '([^/]+)', 'last_name='); | |
} | |
// Create custom post type link for movies | |
function author_post_type_link( $link, $post = 0 ){ | |
if ( $post->post_type == 'author' ){ | |
$id = $post->ID; | |
// $post = &get_post($id); | |
$first_name = get_post_meta($post->ID, 'wpai-first-name', true); | |
$last_name = get_post_meta($post->ID, 'wpai-last-name', true); | |
$first_name = str_replace(' ', '-', $first_name); | |
$last_name = str_replace(' ', '-', $last_name); | |
var_dump($link); | |
if( !empty( $first_name ) || !empty($last_name) ){ | |
return $link = str_replace("%first_name%-%last_name%", strtolower($first_name) .'-'.strtolower($last_name), $link ); | |
} | |
} else { | |
return $link; | |
} | |
flush_rewrite_rules(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment