Tool site

 <?php

/*

Plugin Name: All Books Manager

Plugin URI: https://yourwebsite.com/

Description: A plugin to manage and display books

Version: 1.0

Author: Your Name

Author URI: https://yourwebsite.com/

License: GPL2

*/


// Register Custom Post Type

function abm_register_book_post_type() {

    $labels = array(

        'name'                  => 'Books',

        'singular_name'         => 'Book',

        'menu_name'             => 'Books',

        'name_admin_bar'        => 'Book',

        'add_new'               => 'Add New',

        'add_new_item'          => 'Add New Book',

        'new_item'              => 'New Book',

        'edit_item'             => 'Edit Book',

        'view_item'             => 'View Book',

        'all_items'             => 'All Books',

        'search_items'          => 'Search Books',

        'parent_item_colon'     => 'Parent Books:',

        'not_found'             => 'No books found.',

        'not_found_in_trash'    => 'No books found in Trash.',

        'featured_image'        => 'Book Cover',

        'set_featured_image'    => 'Set book cover',

        'remove_featured_image' => 'Remove book cover',

        'use_featured_image'    => 'Use as book cover',

    );


    $args = array(

        'labels'             => $labels,

        'public'            => true,

        'publicly_queryable'=> true,

        'show_ui'           => true,

        'show_in_menu'      => true,

        'query_var'         => true,

        'rewrite'          => array( 'slug' => 'book' ),

        'capability_type'   => 'post',

        'has_archive'      => true,

        'hierarchical'      => false,

        'menu_position'     => 5,

        'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt' ),

        'menu_icon'         => 'dashicons-book',

    );


    register_post_type( 'book', $args );

}

add_action( 'init', 'abm_register_book_post_type' );


// Add Book Details Meta Box

function abm_add_book_meta_box() {

    add_meta_box(

        'book_details',

        'Book Details',

        'abm_book_meta_box_callback',

        'book',

        'normal',

        'high'

    );

}

add_action( 'add_meta_boxes', 'abm_add_book_meta_box' );


// Meta Box Callback

function abm_book_meta_box_callback( $post ) {

    wp_nonce_field( 'abm_save_book_meta', 'abm_book_meta_nonce' );


    $isbn = get_post_meta( $post->ID, '_abm_isbn', true );

    $price = get_post_meta( $post->ID, '_abm_price', true );

    $publish_date = get_post_meta( $post->ID, '_abm_publish_date', true );

    $publisher = get_post_meta( $post->ID, '_abm_publisher', true );


    echo '<label for="abm_isbn">ISBN:</label>';

    echo '<input type="text" id="abm_isbn" name="abm_isbn" value="' . esc_attr( $isbn ) . '" size="25" /><br>';


    echo '<label for="abm_price">Price:</label>';

    echo '<input type="text" id="abm_price" name="abm_price" value="' . esc_attr( $price ) . '" size="25" /><br>';


    echo '<label for="abm_publish_date">Publish Date:</label>';

    echo '<input type="date" id="abm_publish_date" name="abm_publish_date" value="' . esc_attr( $publish_date ) . '" /><br>';


    echo '<label for="abm_publisher">Publisher:</label>';

    echo '<input type="text" id="abm_publisher" name="abm_publisher" value="' . esc_attr( $publisher ) . '" size="25" />';

}


// Save Meta Box Data

function abm_save_book_meta( $post_id ) {

    if ( ! isset( $_POST['abm_book_meta_nonce'] ) || 

         ! wp_verify_nonce( $_POST['abm_book_meta_nonce'], 'abm_save_book_meta' ) ) {

        return;

    }


    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;


    if ( ! current_user_can( 'edit_post', $post_id ) ) return;


    $fields = array(

        'abm_isbn',

        'abm_price',

        'abm_publish_date',

        'abm_publisher'

    );


    foreach ( $fields as $field ) {

        if ( isset( $_POST[$field] ) ) {

            update_post_meta( $post_id, '_' . $field, sanitize_text_field( $_POST[$field] ) );

        }

    }

}

add_action( 'save_post', 'abm_save_book_meta' );


// Books Shortcode

function abm_books_shortcode( $atts ) {

    $args = shortcode_atts( array(

        'number' => -1,

        'order'  => 'DESC',

    ), $atts );


    $query = new WP_Query( array(

        'post_type'      => 'book',

        'posts_per_page' => $args['number'],

        'order'          => $args['order'],

    ) );


    ob_start();

    if ( $query->have_posts() ) {

        echo '<div class="books-list">';

        while ( $query->have_posts() ) {

            $query->the_post();

            ?>

            <div class="book-item">

                <h3><?php the_title(); ?></h3>

                <?php if ( has_post_thumbnail() ) : ?>

                    <div class="book-cover"><?php the_post_thumbnail('medium'); ?></div>

                <?php endif; ?>

                <div class="book-details">

                    <p><strong>ISBN:</strong> <?php echo esc_html( get_post_meta( get_the_ID(), '_abm_isbn', true ) ); ?></p>

                    <p><strong>Price:</strong> $<?php echo esc_html( get_post_meta( get_the_ID(), '_abm_price', true ) ); ?></p>

                    <p><strong>Published:</strong> <?php echo esc_html( get_post_meta( get_the_ID(), '_abm_publish_date', true ) ); ?></p>

                    <p><strong>Publisher:</strong> <?php echo esc_html( get_post_meta( get_the_ID(), '_abm_publisher', true ) ); ?></p>

                </div>

            </div>

            <?php

        }

        echo '</div>';

        wp_reset_postdata();

    } else {

        echo '<p>No books found</p>';

    }

    return ob_get_clean();

}

add_shortcode( 'display_books', 'abm_books_shortcode' );


// Enqueue Styles

function abm_enqueue_styles() {

    wp_enqueue_style( 

        'abm-styles', 

        plugins_url( 'css/style.css', __FILE__ ),

        array(),

        filemtime( plugin_dir_path( __FILE__ ) . 'css/style.css' )

    );

}

add_action( 'wp_enqueue_scripts', 'abm_enqueue_styles' );

Comments

Popular posts from this blog

Tool site