每个帖子和页面编辑的底部都会显示wordpress插件开发页面表单

时间:2018-08-23 00:34:21

标签: php html wordpress plugins

我正在为自定义数据表开发一个wordpress CRUD插件,并且运行良好。但是,插件中用于插入或编辑新记录的页面表单也会显示在所有页面的底部,并在管理区域内发布新页面/帖子以及编辑页面/帖子。

Screenshot of page html from plugin displaying at the bottom of the add new page

插件中的其他页面未按预期显示在wordpress中的其他任何位置。我完全不知道自己做错了什么,希望有人可以指出正确的方向来解决这个问题。

如果我停用插件,问题仍然存在。

我已使用样板模板作为开发可在此处找到的插件的基础:

https://wppb.me

这将添加菜单选项并为其回调:

    $sub2_cb = array(&$this,'new_award_category_page');     // callback function. name

        add_submenu_page( 
            'Settings', 
            'Award Categories Add New', 
            'Add New', 
            'manage_options',
            'add-category',
            $sub2_cb
         );

这是包含页面的回调函数:

    public function new_award_category_page() {
    include_once( 'partials/new-award-category-page.php' );     
}

正在通过wp_list_table中的行编辑链接调用同一页面,以编辑现有记录:

function column_category($item){

    //Build row actions
    $actions = array(
        'edit' => sprintf('<a href="?page=add-category&id=%s">%s</a>', $item['id'], __('Edit', 'new_award_category_page')),
    );

    //Return the title contents
    return sprintf('%1$s <span style="color:silver"></span>%3$s',
        /*$1%s*/ $item['category'],
        /*$2%s*/ $item['id'],
        /*$3%s*/ $this->row_actions($actions)
    );
}

这是包含文件的页面php / html,用于检查是否插入或更新新记录,验证并显示表单(分为几部分以放在此处):

global $wpdb;
$table_name =  'wp_categories'; // do not forget about tables prefix

$message = '';
$notice = '';

// this is default $item which will be used for new records
$default = array(
    'id' => null,
    'category' => '',
    'paragraph_1' => '',
    'paragraph_2' => '',
    'img' => '',
    'manager' => ''
);

// here we are verifying does this request is post back and have correct nonce
if (wp_verify_nonce($_REQUEST['nonce'], basename(__FILE__))) {
    // combine our default item with request params
    $item = shortcode_atts($default, $_REQUEST);
    // validate data, and if all ok save item to database
    // if id is zero insert otherwise update
    $item_valid = award_category_validate($item);
    if ($item_valid === true) {
        if ($item['id'] == 0) {
            $result = $wpdb->insert($table_name, $item);
            $item['id'] = $wpdb->insert_id;
            if ($result) {
                $message = __('Item was successfully saved', 'new_award_category_page');
            } else {
                $notice = __('There was an error while saving item', 'new_award_category_page');
            }
        } else {
            $result = $wpdb->update($table_name, $item, array('id' => $item['id']));
            if ($result) {
                $message = __('Item was successfully updated', 'new_award_category_page');
            } else {
                $notice = __('There was an error while updating item', 'new_award_category_page');
            }
        }
    } else {
        // if $item_valid not true it contains error message(s)
        $notice = $item_valid;
    }
}
else {
    // if this is not post back we load item to edit or give new one to create
    $item = $default;
    if (isset($_REQUEST['id'])) {
        $item = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $_REQUEST['id']), ARRAY_A);
        if (!$item) {
            $item = $default;
            $notice = __('Item not found', 'new_award_category_page');
        }
    }
}

hhhh

function award_category_validate($item) {

$messages = array();

if (empty($item['category'])) $messages[] = __('Category title is required ', 'new_award_category_page');
if (empty($item['paragraph_1'])) $messages[] = __('Introductory paragraph is required', 'new_award_category_page');
if (empty($item['img'])) $messages[] = __('Representation image is required', 'new_award_category_page');

if (empty($messages)) return true;
return implode('<br />', $messages);

}

div class="wrap">
	<h2><?php _e('Awards Entry Category Maintenenace', 'new_award_category_page')?></h2>

    <?php if (!empty($notice)): ?>
    <div id="notice" class="error"><p><?php echo $notice ?></p></div>
    <?php endif;?>
    <?php if (!empty($message)): ?>
    <div id="message" class="updated"><p><?php echo $message ?></p></div>
    <?php endif;?>

	<div id="poststuff">
		<div id="post-body" class="metabox-holder columns-1">
			<div id="post-body-content">
				<div class="meta-box-sortables ui-sortable">
					<form method="post">
						<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
						<?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
						<input type="hidden" name="id" value="<?php echo $item['id'] ?>"/>
						
						<table class="form-table">
							<tr valign="top">
								<td scope="row"><label for="tablecell"><?php esc_attr_e('Category Title:', 'WpAdminStyle'); ?></label>
								</td>
								<td>
									<input type="text" name="category" id="category" value="<?php echo $item['category'] ?>" class="large-text" required />
								</td>
							</tr>			
							
							<tr valign="top">
								<td scope="row"><label for="tablecell"><?php esc_attr_e('Introductory Paragraph:', 'WpAdminStyle'); ?></label>
								</td>
								<td>
									<textarea id="" name="paragraph_1" cols="80" rows="8" class="large-text" required><?php echo $item['paragraph_1'] ?></textarea>
								</td>
							</tr>			

							<tr valign="top">
								<td scope="row"><label for="tablecell"><?php esc_attr_e('Supplimentary Paragraph:', 'WpAdminStyle'); ?></label>
								</td>
								<td>
									<textarea id="paragraph_2" name="paragraph_2" cols="80" rows="8" class="large-text"><?php echo $item['paragraph_2'] ?></textarea>
								</td>
							</tr>			
										
							<tr valign="top">
								<td scope="row"><label for="tablecell"><?php esc_attr_e('Category Representation Image:', 'WpAdminStyle'); ?> required></label>
								</td>
								<td>
									<input type="text" name="img" id="img" class="regular-text" value="<?php echo $item['img'] ?>">
									<input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image" required>								
								</td>
							</tr>	

							<tr valign="top">
								<td scope="row">
								</td>
								<td>
									<img id="previewImg" src="<?php echo $item['img'] ?>" style="width:150px;height:auto;">										
								</td>
							</tr>	
									
							<tr valign="top">
								<td scope="row"><label for="tablecell"><?php esc_attr_e('Owner:', 'WpAdminStyle'); ?></label>
								</td>
								<td>
									<input type="text" name="manager" value="<?php echo $item['manager'] ?>" class="large-text" required />
								</td>
							</tr>		
											
							<tr valign="top">
								<td scope="row">								
								</td>
								<td>
									<div align="right">
										<input type="submit" value="<?php _e('Save', 'new_award_category_page')?>" id="submit" class="button-primary" name="submit">
									</div>
								</td>
							</tr>						
						</table>						
					</form>
				</div>
			</div>
		</div>
		<br class="clear">
	</div>
</div>	

预先感谢您的帮助。

0 个答案:

没有答案