How To Create Custom Post With Meta Boxes Plugin In WordPress
<?php
/*
Plugin Name: Recipe Meta Box
Plugin URI:
Description: With the help of this plugin you can add recipe detail where add post or create recipe for your project.
Author: Sunil Sharma
Author URI: https://plus.google.com/u/0/115833273431242118637
*/
$paths = ABSPATH;
include_once $paths.'wp-load.php';
/*--------------------------------Create Custom Post Code Start---------------------------------------*/
function recipe_init() {
$args = array(
'public' => true,
'label' => 'Recipe Code',
'supports' => array( 'title', 'editor', 'thumbnail' )
);
register_post_type( 'recipe', $args );
}
add_action( 'init', 'recipe_init' );
/*--------------------------------Create Custom Post Code End---------------------------------------*/
/*--------------------------Create Metabox in Custom Post Code Start--------------------------------*/
function myplugin_add_custom_box() {
$screens = array( 'recipe' ); // Post Type
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'Recipe Detail', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
function myplugin_inner_custom_box( $post ) {
wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
$origin = get_post_meta( $post->ID, 'origin', true );
$preparation_time = get_post_meta( $post->ID, 'preparation_time', true );
$amount_of_persons = get_post_meta( $post->ID, 'amount_of_persons', true );
$benodigde_ingredienten = get_post_meta( $post->ID, 'benodigde_ingredienten', true );
$bereiding = get_post_meta( $post->ID, 'bereiding', true );
echo '<table>';
echo '<tr>';
echo '<td><label for="myplugin_new_field">';
_e( "Origin", 'myplugin_textdomain' );
echo '</label> </td>';
echo '<td><input type="text" id="myplugin_origin" name="myplugin_origin" value="' . esc_attr( $origin ) . '" size="25" /></td>';
echo '</tr><tr>';
echo '<td><label for="myplugin_new_field">';
_e( "Preparation Time", 'myplugin_textdomain' );
echo '</label> </td>';
echo '<td><input type="text" id="myplugin_preparation_time" name="myplugin_preparation_time" value="' . esc_attr( $preparation_time ) . '" size="25" /></td>';
echo '</tr><tr>';
echo '<td><label for="myplugin_new_field">';
_e( "Amount of Persons", 'myplugin_textdomain' );
echo '</label> </td>';
echo '<td><input type="text" id="myplugin_persons" name="myplugin_persons" value="' . esc_attr( $amount_of_persons ) . '" size="25" /></td>';
echo '</tr><tr>';
echo '<td><label for="myplugin_new_field">';
_e( "Benodigde Ingredienten", 'myplugin_textdomain' );
echo '</label> </td>';
echo '<td><textarea id="myplugin_benodigde_ingredienten" name="myplugin_benodigde_ingredienten" cols="40" rows="1" style="width: 600px; height: 104px;">' . esc_attr( $benodigde_ingredienten ) . '</textarea></td>';
echo '</tr><tr>';
echo '<td><label for="myplugin_new_field">';
_e( "Bereiding", 'myplugin_textdomain' );
echo '</label> </td>';
//echo '<td><input type="textarea" id="myplugin_bereiding" name="myplugin_bereiding" value="' . esc_attr( $bereiding ) . '" /></td>';
echo '<td><textarea id="myplugin_bereiding" name="myplugin_bereiding" cols="40" rows="1" style="width: 600px; height: 104px;">'. esc_attr( $bereiding ). '</textarea></td>';
echo '</tr>';
echo '</table>';
}
function myplugin_save_postdata( $post_id ) {
if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) )
return $post_id;
$nonce = $_POST['myplugin_inner_custom_box_nonce'];
if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
$myplugin_origin = sanitize_text_field( $_POST['myplugin_origin'] );
$myplugin_preparation_time = sanitize_text_field( $_POST['myplugin_preparation_time'] );
$myplugin_persons = sanitize_text_field( $_POST['myplugin_persons'] );
$myplugin_benodigde_ingredienten = sanitize_text_field( $_POST['myplugin_benodigde_ingredienten'] );
$myplugin_bereiding = sanitize_text_field( $_POST['myplugin_bereiding'] );
update_post_meta( $post_id, 'origin', $myplugin_origin );
update_post_meta( $post_id, 'preparation_time', $myplugin_preparation_time );
update_post_meta( $post_id, 'amount_of_persons', $myplugin_persons );
update_post_meta( $post_id, 'benodigde_ingredienten', $myplugin_benodigde_ingredienten );
update_post_meta( $post_id, 'bereiding', $myplugin_bereiding );
}
add_action( 'save_post', 'myplugin_save_postdata' );
/*-------------------------Create Metabox in Custom Post Code End--------------------------------*/
?>
No comments:
Post a Comment