How to Grab Anime info using Jikan API and fill the value in Metabox

Question

I want to create an Anime website. But I find it less effective if I have to manually copy & paste Anime information from MyAnimeList. So I want to create a system where users must enter the Anime ID from MyAnimeList and after that press the generate button, then automatically the desired Anime information will be filled in Metabox. Oh, my post_type is anime.
Here’s the endpoint:

https://api.jikan.moe/v3/anime/{id}

And this is my Metabox:

<?php
/* Metabox Tweak */
if ( ! function_exists( 'rwmb_meta' ) ) {
    function rwmb_meta( $key, $args = '', $post_id = null ) {
        return false;
    }
}

/* Anime Metabox */
function mal_metabox( $meta_boxes ) {
    $prefix = 'mal_';
    
    $meta_boxes[] = [
        'id' => 'anime_info',
        'title' => esc_html__( 'Anime Details' ),
        'post_types' => [ 'anime' ],
        'context' => 'advanced',
        'priority' => 'high',
        'autosave' => true,
        'fields' => [
            [
                'id' => $prefix . 'title_japanese',
                'type' => 'text',
                'name' => esc_html__( 'Japanese' ),
                'placeholder' => esc_html__( 'Japanese Title' ),
            ],
            [
                'id' => $prefix . 'title_english',
                'type' => 'text',
                'name' => esc_html__( 'English' ),
                'placeholder' => esc_html__( 'English Title' ),
            ],
            [
                'id' => $prefix . 'title_synonyms',
                'type' => 'text',
                'name' => esc_html__( 'Synonmys' ),
                'placeholder' => esc_html__( 'Synonyms Title' ),
            ],
            [
                'id' => $prefix . 'status',
                'type' => 'text',
                'name' => esc_html__( 'Status' ),
                'placeholder' => esc_html__( 'Anime Status' ),
            ],
            [
                'id' => $prefix . 'types',
                'type' => 'text',
                'name' => esc_html__( 'Type' ),
                'placeholder' => esc_html__( 'Anime Type' ),
            ],
            [
                'id' => $prefix . 'source',
                'type' => 'text',
                'name' => esc_html__( 'Source' ),
                'placeholder' => esc_html__( 'Anime Source' ),
            ],
            [
                'id' => $prefix . 'duration',
                'type' => 'text',
                'name' => esc_html__( 'Duration' ),
                'placeholder' => esc_html__( 'Anime Duration' ),
            ],
            [
                'id' => $prefix . 'episodes',
                'type' => 'text',
                'name' => esc_html__( 'Total Episodes' ),
                'placeholder' => esc_html__( 'Total Episodes' ),
            ],
            [
                'id' => $prefix . 'seasons',
                'type' => 'text',
                'name' => esc_html__( 'Seasons' ),
                'placeholder' => esc_html__( 'Seasons' ),
            ],
            [
                'id' => $prefix . 'studios',
                'type' => 'text',
                'name' => esc_html__( 'Studios' ),
                'placeholder' => esc_html__( 'Studios' ),
            ],
            [
                'id' => $prefix . 'producers',
                'type' => 'text',
                'name' => esc_html__( 'Producers' ),
                'placeholder' => esc_html__( 'Producers' ),
            ],
            [
                'id' => $prefix . 'releases',
                'type' => 'text',
                'name' => esc_html__( 'Releases' ),
                'placeholder' => esc_html__( 'Release Date' ),
            ],
            [
                'id' => $prefix . 'genres',
                'type' => 'text',
                'name' => esc_html__( 'Genres' ),
                'placeholder' => esc_html__( 'Genres' ),
            ],
            [
                'id' => $prefix . 'score',
                'type' => 'text',
                'name' => esc_html__( 'Score' ),
                'placeholder' => esc_html__( 'Score' ),
            ],
            [
                'id' => $prefix . 'synopsis',
                'type' => 'text_area',
                'name' => esc_html__( 'Synopsis' ),
                'placeholder' => esc_html__( 'Synopsis' ),
            ],
            [
                'id' => $prefix . 'poster',
                'type' => 'file_input',
                'name' => esc_html__( 'Poster' ),
                'desc' => esc_html__( 'Anime Poster' ),
                'max_file_uploads' => '1',
                'max_status' => false,
            ],
        ],
    ];
    return $meta_boxes;
}
add_filter('rwmb_meta_box', 'mal_metabox');

/* INPUT */
function generate_info($post) {
?>
<div class="rwmb-meta-box">
    <div class="rwmb-field rwmb-text-wrapper">
        <div class="rwmb-label">
            <label for="anime-id">Jikan API</label>     
        </div>
        <div class="rwmb-input ui-shortable">
            <div class="rwmb-clone rwmb-text-clone">
                <input size="30" type="text" id="anime-id" class="rwmb-text" name="anime-id" placeholder="Enter Anime ID"/>
            </div>
            <a class="button-primary" id="generate">Generate</a>
        </div>
    </div>
</div>
<?php
}

/* Show The Form */
function add_generate_info() {
    add_metabox('generate_anime', 'Generate Info', 'generate_info', 'anime');
}
add_action('add_meta_boxes', 'add_generate_info');
0
, , Yuu-kunz 1 year 2020-07-20T10:10:40-05:00 0 Answers 77 views 0

Leave an answer

Browse
Browse