<?php

/*
* Creating a function to create our CPT
*/
 
function custom_post_type_weblink() {
 
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Web Liinks', 'Post Type General Name'),
        'singular_name'       => _x( 'Web Link', 'Post Type Singular Name'),
        'menu_name'           => __( 'Web Links'),
        'parent_item_colon'   => __( 'Parent Web Link'),
        'all_items'           => __( 'All Web Links'),
        'view_item'           => __( 'View Web Link'),
        'add_new_item'        => __( 'Add New Web Link'),
        'add_new'             => __( 'Add New'),
        'edit_item'           => __( 'Edit Web Link'),
        'update_item'         => __( 'Update Web Link'),
        'search_items'        => __( 'Search Web Link'),
        'not_found'           => __( 'Not Found'),
        'not_found_in_trash'  => __( 'Not found in Trash'),
    );
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'Web Liinks', 'twentytwenty' ),
        'description'         => __( 'Web Liinks for directory', 'twentytwenty' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'excerpt', 'author', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'weblink', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type_weblink', 0 );



// Let us create Taxonomy for Custom Post Type
add_action( 'init', 'create_weblink_custom_taxonomy', 0 );
 
//create a custom taxonomy name it "type" for your posts
function create_weblink_custom_taxonomy() {
 
  $labels = array(
    'name' => _x( 'Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Tags' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => __( 'Parent Tag'),
    'parent_item_colon' => __( 'Parent Tag:' ),
    'edit_item' => __( 'Edit Tag' ), 
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
    'menu_name' => __( 'Tags' ),
  ); 	
 
  register_taxonomy('tag_weblink',array('weblink'), array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tag_weblink' ),
  ));
  
  
  $labels = array(
    'name' => _x( 'Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Categories' ),
    'all_items' => __( 'All Categories' ),
    'parent_item' => __( 'Parent Category'),
    'parent_item_colon' => __( 'Parent Category:' ),
    'edit_item' => __( 'Edit Category' ), 
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Category Name' ),
    'menu_name' => __( 'Categories' ),
  ); 	
 
  register_taxonomy('category_weblink',array('weblink'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'category_weblink' ),
  ));
  
}



function add_weblink_meta_box() {
add_meta_box(
    'weblink_meta_box_for_url', // $id
    'WebLink Details', // $title
    'show_weblink_meta_box_for_url', // $callback
 'weblink', // $screen
    'normal', // $context
    'high' // $priority

);
}

function show_weblink_meta_box_for_url()
{
	global $post;  
    $weblink_url = get_post_meta( $post->ID, 'weblink_url', true ); 
	$weblink_desc = get_post_meta( $post->ID, 'weblink_desc', true );
?>
	
	<input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
	<!-- All fields will go here -->
	<span>Site Url</span></br>
	<input type="text" name="weblink_url" value="<?php echo $weblink_url; ?>"></br>
	<span>Site Description</span></br>
	<textarea name="weblink_desc" rows="1" cols="25"><?php echo $weblink_desc; ?></textarea>

<?php	
}
add_action( 'add_meta_boxes', 'add_weblink_meta_box' );



function save_weblink_fields( $post_id ) {   
	// verify nonce
	if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
		return $post_id; 
	}
	// check autosave
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return $post_id;
	}
	// check permissions
	if ( 'weblink' === $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_id ) ) {
			return $post_id;
		} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
			return $post_id;
		}  
	}

	$old = get_post_meta( $post_id, 'your_fields', true );
	$url = $_POST['weblink_url'];
	$desc =  $_POST['weblink_desc'];
	update_post_meta( $post_id, 'weblink_url', $url ); 
	update_post_meta( $post_id, 'weblink_desc', $desc ); 
}
add_action( 'save_post', 'save_weblink_fields' );



?>