2016-07-02 41 views
1
<?php 
/** 
* @package Hello_Dolly 
* @version 1.0 
*/ 
/* 
Plugin Name: Movie Listing Plugin 
Plugin URI: This plugin lists Novies 
*/ 

//Exit if accessed directly 
if (! defined('ABSPATH')) { 
    exit; 
} 
function zulfi_movie_register_post_type() { 

    $singular = 'Movie Listing'; 
    $plural = 'Movie Listing'; 
    $slug = str_replace(' ', '_', strtolower($singular)); 
    $labels = array(
     'name'   => $plural, 
     'singular_name'  => $singular, 
     'add_new'   => 'Add New', 
     'add_new_item'  => 'Add New ' . $singular, 
     'edit'   => 'Edit', 
     'edit_item'  => 'Edit ' . $singular, 
     'new_item'  => 'New ' . $singular, 
     'view'   => 'View ' . $singular, 
     'view_item'   => 'View ' . $singular, 
     'search_term'   => 'Search ' . $plural, 
     'parent'   => 'Parent ' . $singular, 
     'not_found'   => 'No ' . $plural .' found', 
     'not_found_in_trash' => 'No ' . $plural .' in Trash' 
     ); 
    $args = array(
     'labels'    => $labels, 
      'public'    => true, 
      'publicly_queryable' => true, 
      'exclude_from_search' => false, 
      'show_in_nav_menus' => true, 
      'show_ui'    => true, 
      'show_in_menu'  => true, 
      'show_in_admin_bar' => true, 
      'menu_position'  => 4, 
      'menu_icon'   => 'dashicons-businessman', 
      'can_export'   => true, 
      'delete_with_user' => false, 
      'hierarchical'  => false, 
      'has_archive'   => true, 
      'query_var'   => true, 
      'capability_type'  => 'post', 
      'map_meta_cap'  => true, 
      // 'capabilities' => array(), 
      'rewrite'    => array( 
       'slug' => $slug, 
       'with_front' => true, 
       'pages' => true, 
       'feeds' => true, 
      ), 
      'supports'   => array( 
       'title', 
       'thumbnail', 

      ) 
    ); 
    register_post_type($slug, $args); 
} 
add_action('init', 'zulfi_movie_register_post_type'); 
function zulfi_movie_register_taxonomy() { 
    $singular = 'Gener'; 
    $plural = 'Geners'; 
    $slug = str_replace(' ', '_', strtolower($singular)); 
    $labels = array(
     'name'      => $plural, 
     'singular_name'    => $singular, 
     'search_items'    => 'Search ' . $plural, 
     'popular_items'    => 'Popular ' . $plural, 
     'all_items'     => 'All ' . $plural, 
     'parent_item'    => null, 
     'parent_item_colon'   => null, 
     'edit_item'     => 'Edit ' . $singular, 
     'update_item'    => 'Update ' . $singular, 
     'add_new_item'    => 'Add New ' . $singular, 
     'new_item_name'    => 'New ' . $singular . ' Name', 
     'separate_items_with_commas' => 'Separate ' . $plural . ' with commas', 
     'add_or_remove_items'  => 'Add or remove ' . $plural, 
     'choose_from_most_used'  => 'Choose from the most used ' . $plural, 
     'not_found'     => 'No ' . $plural . ' found.', 
     'menu_name'     => $plural, 
    ); 
    $args = array(
      'hierarchical'   => true, 
      'labels'    => $labels, 
      'show_ui'    => true, 
      'show_admin_column'  => true, 
      'update_count_callback' => '_update_post_term_count', 
      'query_var'    => true, 
      'rewrite'    => array('slug' => $$slug), 
     ); 
    register_taxonomy('geners', 'movie_listing', $args); 
} 
add_action('init', 'zulfi_movie_register_taxonomy'); 
function zulfi_movie_add_custom_meta() { 
    add_meta_box(
     'dwwp_meta', 
     'Listing info', 
     'zulfi_movie_meta_callback', 
     'movie_listing', 
     'normal', 
     'high' 
    ); 
} 
add_action('add_meta_boxes', 'zulfi_movie_add_custom_meta'); 
function zulfi_movie_meta_callback($post){ 
    wp_nonce_field(basename(__FILE__),'ml_movie_listings_nonce'); 

    $ml_stored_meta=get_post_meta($post->ID); 
    ?> 
    <div class="wrap movie-listing-form"> 
      <div class="wrap movie-listing-form"> 
      <label for="movie_id"><?php esc_html_e('Movie ID','ml-domain'); ?></label> 
      <input type="text" name="movie_id" id="movie_id" value="<?php if(!empty($ml_stored_meta['movie_id'])) echo  esc_attr($ml_stored_meta['movie_id'][0]); ?>"/> 
      </div> 
     <div class="form-group"> 
     <label for="mpaa_rating"><?php esc_html_e('Movie Listing ID','ml-domain'); ?></label> 
     <select name="mpaa_rating" id="mpaa_rating"> 
     <?php $option_values=array('G','PG','PG-13','R','NR'); 
     foreach($option_values as $key => $value){ 
     if($value==$ml_stored_meta['mpaa_rating'][0]){ 
     ?> 
     <option selected="selected" ><?php echo $value ?></option> 
     <?php 
     } 
     else { 
     ?> 
     <option><?php echo $value ?></option> 
     <?php 
     } 
     } 
     ?> 

     </select> 
     </div> 
      <div class="form-group"> 
     <label for="details"><?php esc_html_e('Details','ml-domain'); ?></label> 
     <?php $content=get_post_meta($post->ID,'details',true); 
     $editior='details'; 
     $settings=array('textarea_rows'=>'5','media_buttons'=>true); 
     wp_editor($content,$editior,$settings); 
     ?> 
     </div> 

      <div class="form-group"> 
       <label for="release_date"><?php esc_html_e('Release date','ml-domain'); ?></label> 
       <input type="date" name="release_date" id="release_date" value="<?php echo esc_attr($ml_stored_meta['release_date'][0]); ?>"/> 
      </div> 
      <div class="form-group"> 
       <label for="director"><?php esc_html_e('director','ml-domain'); ?></label> 
       <input type="text" name="director" id="director" value="<?php if(!empty($ml_stored_meta['director'])) echo  esc_attr($ml_stored_meta['director'][0]); ?>"/> 
      </div> 
      <div class="form-group"> 
       <label for="stars"><?php esc_html_e('stars','ml-domain'); ?></label> 
       <input type="text" name="stars" id="stars" value="<?php if(!empty($ml_stored_meta['stars'])) echo  esc_attr($ml_stored_meta['stars'][0]); ?>"/> 
      </div> 
      <div class="form-group"> 
       <label for="runtime"><?php esc_html_e('Run time','ml-domain'); ?></label> 
       <input type="text" name="runtime" id="runtime" value="<?php if(!empty($ml_stored_meta['runtime'])) echo  esc_attr($ml_stored_meta['runtime'][0]); ?>"/> 
      </div> 
       <div class="form-group"> 
       <label for="trailor"><?php esc_html_e('Youtube ','ml-domain'); ?></label> 
      <input type="text" name="trailor" id="trailor" value="<?php if(!empty($ml_stored_meta['trailor'])) echo esc_attr($ml_stored_meta['trailor'][0]); ?>"/> 
      </div> 
    </div> 
    <?php 
} 
add_action('save_post', 'zulfi_movie_save_meta'); 
function zulfi_movie_save_meta($post_id){ 

    $is_autosave = wp_is_post_autosave($post_id); 
    $is_revision = wp_is_post_revision($post_id); 
    $is_valid_nonce=(isset($_POST['ml_movie_listings_nonce']) && wp_verify_nonce($_POST['ml_movie_listings_nonce'],basename(__FILE__)))?'true':'false'; 
    if($is_autosave || $is_revision || $is_valid_nonce){ 
     return; 
    } 
    if($_POST['movie_id']) 
     { 
     update_post_meta($post_id,'move_id',sanitize_text_field($_POST['movie_id'])); 
     } 
     if($_POST['mpaa_rating']) 
     { 
     update_post_meta($post_id,'mpaa_rating',sanitize_text_field($_POST['mpaa_rating'])); 
     } 
     if($_POST['ratings']) 
     { 
     update_post_meta($post_id,'ratings',sanitize_text_field($_POST['ratings'])); 
     } 
     if($_POST['runtime']) 
     { 
     update_post_meta($post_id,'runtime',sanitize_text_field($_POST['runtime'])); 
     } 
     if($_POST['stars']) 
     { 
     update_post_meta($post_id,'stars',sanitize_text_field($_POST['stars'])); 
     } 
     if($_POST['trailor']) 
     { 
     update_post_meta($post_id,'trailor',sanitize_text_field($_POST['trailor'])); 
     } 
     if($_POST['release_date']) 
     { 
     update_post_meta($post_id,'release_date',sanitize_text_field($_POST['release_date'])); 
     } 
     if($_POST['runtime']) 
     { 
     update_post_meta($post_id,'runtime',sanitize_text_field($_POST['runtime'])); 
     } 

} 

這是我的代碼,我不知道什麼是錯的。據我所知,一切都是正確的。它是保存但沒有數據在我的自定義字段沒有顯示它似乎有一個問題是與zulfi_movie_save_meta函數,但我不知道到底是什麼問題。我保存的數據應顯示在自定義字段中。我的wordpress自定義文章沒有正確保存並且不顯示在管理菜單中?

+0

我檢查了代碼併發布了保存工作wordpress 4.5.3 Twentysixteen。你正在使用哪個WP版本? –

+0

我使用的是wordpress 4.2 –

回答

3

使用功能wp_insert_post(數組$ postarr,布爾$ wp_error = FALSE)插入後在分貝,這個功能也返回一個錯誤,如果後期在不被保存在數據庫,這樣你可以得到什麼是錯的,

您可以在這裏查看有關此功能的更多詳細信息https://developer.wordpress.org/reference/functions/wp_insert_post/

0

從上面的代碼中刪除以下行。

if($is_autosave || $is_revision || $is_valid_nonce){ 
    return; 
} 

糾正postmeta movie_id併爲導演添加貼子。然後你的整個代碼將正常工作。

相關問題