• Resolved somebuddy

    (@somebuddy)


    Hi!
    I add flexslider plugin to my custom theme: register custom post type slider, add metabox for sliders url. All working fine, but then I want to add new slider my wp show me some kind of this messages:

    Notice: Undefined index: post_type in /.../slider_post_type.php on line 141
    
    Notice: Undefined index: wptuts_slideurl in /.../slider_post_type.php on line 152
    
    Notice: Undefined index: reply_metabox_nonce in /.../reply_post_type.php on line 106

    As I think this Notices means that it looking for variables that will appears only after data will be saved by pushing publish data. Can somebody advice me how to avoid of appering of these messages?

    here comes code of these files:

    slider_post_type.php

    <?php
    // 1.0. Add post type for slider
    add_action('init', 'register_slides_posttype');
    function register_slides_posttype() {
    	$labels = array(
    		'name' 				=> _x( 'Слайды', 'post type general name' ),
    		'singular_name'		=> _x( 'Слайд', 'post type singular name' ),
    		'add_new' 			=> __( 'Добавить новый' ),
    		'add_new_item' 		=> __( 'Добавить новый слайд' ),
    		'edit_item' 		=> __( 'Редактировать слайд' ),
    		'new_item' 			=> __( 'Новый слайд' ),
    		'view_item' 		=> __( 'Смотреть слайд' ),
    		'search_items' 		=> __( 'Найти слайды' ),
    		'not_found' 		=> __( 'Слайд' ),
    		'not_found_in_trash'=> __( 'Слайд' ),
    		'parent_item_colon' => __( 'Слайд' ),
    		'menu_name'			=> __( 'Слайды' )
    	);
    
    	$supports = array('title','thumbnail','cat-slides');
    
    	$post_type_args = array(
    		'labels' 			=> $labels,
    		'singular_label' 	=> __('Slide'),
    		'public' 			=> true, // public type
    		'show_ui' 			=> true, // show on admin dashboard
    		'publicly_queryable'=> true, // allow querys from templates for this post type
    		'query_var'			=> true,
    		'capability_type' 	=> 'post', // group permission
    		'has_archive' 		=> false, // without archives
    		'hierarchical' 		=> true, // means that it'll be categories
    		'rewrite' 			=> array('slug' => 'slides', 'with_front' => false ), // rule to write url, dont shoe prefix 'slides'
    		'supports' 			=> $supports,
    		'menu_position' 	=> 27, // Menu position
    		'menu_icon' 		=> 'dashicons-images-alt2' // icon on admin dashboard
    	 );
    	 register_post_type('slides',$post_type_args);
    }
    
    // 2.0. Add taconomy for slider post type
    add_action( 'init', 'my_taxonomies_slider',0);
    function my_taxonomies_slider() {
      $labels = array(
        'name'              => _x( 'Категории слайдов', 'taxonomy general name' ),
        'singular_name'     => _x( 'Категория слайдов', 'taxonomy singular name' ),
        'search_items'      => __( 'Найти категорию слайдов' ),
        'all_items'         => __( 'Все категории слайдов' ),
        'parent_item'       => __( 'Родительская категория слайдов' ),
        'parent_item_colon' => __( 'Родительская категория слайдов:' ),
        'edit_item'         => __( 'Редактировать категорию слайдов' ),
        'update_item'       => __( 'Обновить категорию слайдов' ),
        'add_new_item'      => __( 'Добавить новую категорию слайдов' ),
        'new_item_name'     => __( 'Новая категория слайдов' ),
        'menu_name'         => __( 'Категории слайдов' ),
      );
      $args = array(
        'labels' => $labels,
        'hierarchical' => true, // if 'true' will using categories else - tags
      );
      register_taxonomy( 'cat-slides', 'slides', $args );
    }
    
    // 3.0. Add Meta box for picture's URL
    $slidelink_2_metabox = array(
    	'id' => 'slidelink', // metabox id
    	'title' => 'Ссылка на слайд', // title for metabox
    	'page' => array('slides'), // for all replys post type
    	'context' => 'normal',
    	'priority' => 'default',
    	'fields' => array( // single url-field description
    					array(
    						'name' 			=> 'URL слайда',
    						'desc' 			=> '',
    						'id' 				=> 'wptuts_slideurl',
    						'class' 			=> 'wptuts_slideurl',
    						'type' 			=> 'text',
    						'rich_editor' 	=> 0,
    						'max' 			=> 0
    					),
    				)
    );			
    
    add_action('admin_menu', 'wptuts_add_slidelink_2_meta_box');
    function wptuts_add_slidelink_2_meta_box() {
    	global $slidelink_2_metabox;		
    
    	foreach($slidelink_2_metabox['page'] as $page) {
    		add_meta_box($slidelink_2_metabox['id'], $slidelink_2_metabox['title'], 'wptuts_show_slidelink_2_box', $page, 'normal', 'default', $slidelink_2_metabox);
    	}
    }
    
    // 2.1. Function, defined post type for display metabox and function to dispay metabox
    function wptuts_show_slidelink_2_box()	{
    	global $post;
    	global $slidelink_2_metabox;
    	global $wptuts_prefix;
    	global $wp_version;
    
    	// use 'nonce' for checking form
    	echo '<input type="hidden" name="wptuts_slidelink_2_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    
    	echo '<table class="form-table">';
    
    	foreach ($slidelink_2_metabox['fields'] as $field) {
    
    		// get current data
    		$meta = get_post_meta($post->ID, $field['id'], true);
    
    		echo '<tr>',
    				'<th style="width:20%"><label for="', $field['id'], '">', stripslashes($field['name']), '</label></th>',
    				'<td class="wptuts_field_type_' . str_replace(' ', '_', $field['type']) . '">';
    		switch ($field['type']) {
    			case 'text':
    				echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta, '" size="30" style="width:97%" /><br/>', '', stripslashes($field['desc']);
    				break;
    		}
    		echo    '<td>',
    			'</tr>';
    	}
    
    	echo '</table>';
    }	
    
    // 2.3. Save entered data
    add_action('save_post', 'wptuts_slidelink_2_save');
    function wptuts_slidelink_2_save($post_id) {
    	global $post;
    	global $slidelink_2_metabox;
    
    	// checking nonce
    	if ( isset($_POST['at_nonce']) && !wp_verify_nonce($_POST['wptuts_slidelink_2_meta_box_nonce'], basename(__FILE__))) {
    		return $post_id;
    	}
    
    	// checking autosave
    	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    		return $post_id;
    	}
    
    	// checking permission for redacting
    	if ('page' == $_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;
    	}
    
    	foreach ($slidelink_2_metabox['fields'] as $field) {
    
    		$old = get_post_meta($post_id, $field['id'], true);
    		$new = $_POST[$field['id']];
    
    		if ($new && $new != $old) {
    			if($field['type'] == 'date') {
    				$new = wptuts_format_date($new);
    				update_post_meta($post_id, $field['id'], $new);
    			} else {
    				if(is_string($new)) {
    					$new = $new;
    				}
    				update_post_meta($post_id, $field['id'], $new);
    
    			}
    		} elseif ('' == $new && $old) {
    			delete_post_meta($post_id, $field['id'], $old);
    		}
    	}
    }

    reply_post_type.php
    `<?php
    // 1.0. Add post type for slider
    add_action(‘init’, ‘register_reply_posttype’);
    function register_reply_posttype() {
    $labels = array(
    ‘name’ => __( ‘Отзывы’),
    ‘singular_name’ => __( ‘Отзыв’),
    ‘add_new’ => __( ‘Добавить новый’ ),
    ‘add_new_item’ => __( ‘Добавить новый отзыв’ ),
    ‘edit_item’ => __( ‘Редактировать отзыв’ ),
    ‘new_item’ => __( ‘Новый отзыв’ ),
    ‘view_item’ => __( ‘Просмотреть отзыв’ ),
    ‘search_items’ => __( ‘Найти отзыв’ ),
    ‘not_found’ => __( ‘Отзыв’ ),
    ‘not_found_in_trash’=> __( ‘Отзыв’ ),
    ‘parent_item_colon’ => __( ‘Отзыв’ ),
    ‘menu_name’ => __( ‘Отзывы’ )
    );

    $supports = array(‘title’,’editor’,’thumbnail’);

    $post_type_args = array(
    ‘labels’ => $labels,
    ‘singular_label’ => __(‘Reply’),
    ‘public’ => true, // public type
    ‘show_ui’ => true, // show on admin dashboard
    ‘publicly_queryable’=> true, // allow querys from templates for this post type
    ‘query_var’ => true,
    ‘capability_type’ => ‘post’, // group permission
    ‘has_archive’ => false, // without archives
    ‘hierarchical’ => true, // means that it’ll be categories
    ‘rewrite’ => array(‘slug’ => ‘replys’, ‘with_front’ => false ), // rule to write url, dont shoe prefix ‘replys’
    ‘supports’ => $supports,
    ‘menu_position’ => 27, // Menu position
    ‘menu_icon’ => ‘dashicons-format-quote’ // icon on admin dashboard
    );
    register_post_type(‘replys’,$post_type_args);
    }

    // 2.0. Create metabox for author custom field
    add_action(‘admin_menu’, ‘add_author_reply_metabox’);
    $replylinks_2_metabox = array(
    ‘id’ => ‘reply_author_block’, // metabox id
    ‘title’ => ‘Автор отзыва’, // title for metabox
    ‘post’ => array(‘replys’), // for all replys post type
    ‘context’ => ‘normal’,
    ‘priority’ => ‘default’,
    ‘fields’ => array( // single url-field description
    array(
    ‘name’ => ‘Имя’,
    ‘desc’ => ”,
    ‘id’ => ‘reply_author_name’,
    ‘class’ => ‘reply_author_name’,
    ‘type’ => ‘text’,
    ‘rich_editor’ => 0,
    ‘max’ => 0
    ),
    )
    );
    // 2.1. Function, defined post type for display metabox and function to dispay metabox
    function add_author_reply_metabox() {
    global $replylinks_2_metabox;
    foreach($replylinks_2_metabox[‘post’] as $post) {
    add_meta_box($replylinks_2_metabox[‘id’], $replylinks_2_metabox[‘title’], ‘show_reply_metabox’, $post, ‘normal’, ‘default’, $replylinks_2_metabox);
    }
    }

    // 2.2. Metabox displaying function
    function show_reply_metabox() {
    global $post;
    global $replylinks_2_metabox;
    global $wp_prefix;
    global $wp_version;
    // use ‘nonce’ for checking form
    echo ‘<input type=”hidden” name=”reply_metabox_nonce” value=”‘, wp_create_nonce(basename(__FILE__)), ‘” />’;

    echo ‘<table class=”form-table”>’;

    foreach ($replylinks_2_metabox[‘fields’] as $field) {

    // get current data
    $meta = get_post_meta($post->ID, $field[‘id’], true);

    echo ‘<tr>’,
    ‘<th style=”width:20%”><label for=”‘, $field[‘id’], ‘”>’, stripslashes($field[‘name’]), ‘</label></th>’,
    ‘<td class=”field_type_’ . str_replace(‘ ‘, ‘_’, $field[‘type’]) . ‘”>’;
    switch ($field[‘type’]) {
    case ‘text’:
    echo ‘<input type=”text” name=”‘, $field[‘id’], ‘” id=”‘, $field[‘id’], ‘” value=”‘, $meta, ‘” size=”30″ style=”width:97%” /><br/>’, ”, stripslashes($field[‘desc’]);
    break;
    }
    echo ‘<td>’,
    ‘</tr>’;
    }

    echo ‘</table>’;
    }

    // 2.3. Save entered data
    add_action(‘save_post’, ‘save_replyname_item’);
    function save_replyname_item($post_id) {
    global $post;
    global $replylinks_2_metabox;

    // checking nonce
    if (!wp_verify_nonce($_POST[‘reply_metabox_nonce’], basename(__FILE__))) {
    return $post_id;
    }

    // checking autosave
    if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) {
    return $post_id;
    }

    // checking permission for redacting
    if (‘post’ == $_POST[‘post_type’]) {
    if (!current_user_can(‘edit_post’, $post_id)) {
    return $post_id;
    }
    } elseif (!current_user_can(‘edit_page’, $post_id)) {
    return $post_id;
    }

    foreach ($replylinks_2_metabox[‘fields’] as $field) {

    $old = get_post_meta($post_id, $field[‘id’], true);
    $new = $_POST[$field[‘id’]];

    if ($new && $new != $old) {
    if($field[‘type’] == ‘date’) {
    $new = format_date($new);
    update_post_meta($post_id, $field[‘id’], $new);
    } else {
    if(is_string($new)) {
    $new = $new;
    }
    update_post_meta($post_id, $field[‘id’], $new);

    }
    } elseif (” == $new && $old) {
    delete_post_meta($post_id, $field[‘id’], $old);
    }
    }
    }
    ?>`
    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Notice warnings in admin section’ is closed to new replies.