注意未定义的偏移量:2

问题描述

我正在使用 PHP 代码将来自 TMDB 的数据保存在 wordpress MetaBox 字段上,每次我创建新文章时,日志都会给我很多错误

PHP Notice Undefined index: sp_nonce_name in C:\xampp\htdocs\clean\wp-content\themes\moviewp\lib\MetaBox.PHP on line 176
PHP Notice Undefined offset: 2 in C:\xampp\htdocs\clean\wp-content\themes\moviewp\lib\MetaBox.PHP on line 71

这是我使用的代码

$sp_Boxes = array ( 
'Generator' => array (
array( 'fetch','fetch','fetch' ),),/*====================================*\
    function informations
\*====================================*/

'informations' => array (
        array( 'youtube_id','Trailer'),array( 'imdbrating','rating' ),array( 'Runtime','Runtime' ),array( 'release_date','Year' ),array( 'imdb_id','IMDb' ),array( 'id','TMDb' ),array( 'Rated','Certification' ),array( 'poster_path','Poster Path' ),array( 'backdrop_path','Backdrop' ),/*====================================*\
    function Player
\*====================================*/

'Movie Player' => array (
        array( '720p','720p'),array( '1080p','1080p'),array( 'download','Download'),array( 'player_desc','player_desc','playerdescription' ),);
add_action( 'admin_menu','sp_add_custom_Box' );
add_action( 'save_post','sp_save_postdata',1,2 );

/*====================================*\
    function custom Box
\*====================================*/

function sp_add_custom_Box() {
global $sp_Boxes;
if ( function_exists( 'add_Meta_Box' ) ) {
foreach ( array_keys( $sp_Boxes ) as $Box_name ) {
add_Meta_Box( $Box_name,__( $Box_name,'sp' ),'sp_post_custom_Box','post','normal','high' );
} } }
function sp_post_custom_Box ( $obj,$Box ) {
global $sp_Boxes;
static $sp_nonce_flag = false;
if ( ! $sp_nonce_flag ) {
echo_sp_nonce();
$sp_nonce_flag = true;
}foreach ( $sp_Boxes[$Box['id']] as $sp_Box ) {
echo field_html( $sp_Box );
} }
function field_html ( $args ) {
switch ( $args[2] ) {
case 'textarea':
return text_area( $args );
case 'moviedescription':
return movie_desc( $args );
case 'playerdescription':
return player_desc( $args );
case 'gendescription':
return gen_desc( $args );
case 'tvdescription':
return tv_desc( $args );
case 'fetch':
return fetch( $args );
case 'inizio':
return inizio_1( $args );
case 'fine':
return fine_1( $args );
case 'spazio':
return spazio_1( $args );
case 'checkBox':
case 'radio':
case 'button':
case 'text':
return text_button( $args );
case 'submit':
default:
return text_field( $args );
} }
function text_field ( $args ) {
global $post;
$args[2] = get_post_meta($post->ID,$args[0],true);
$args[1] = __($args[1],'sp' );
$label_format =
'<label style="font-weight:bold;display:none;" for="%1$s">%2$s &nbsp;&nbsp;</label>'
. '<input title="%2$s" placeholder="%2$s" onclick="this.select();execCommand(\'copy\');" style="width: 422px;display: inline-block;margin-bottom:10px;" type="text" name="%1$s" value="%3$s" />&nbsp;';
return vsprintf( $label_format,$args );
}
function text_button ( $args ) {
global $post;
$args[2] = get_post_meta($post->ID,'sp' );
$label_format = '<input type="button" class="button button-primary button-large %1$s" style="cursor:pointer;display: inline-block;" name="%1$s" value="%1$s" /><br /><br />';
return vsprintf( $label_format,$args );
}
function text_area ( $args ) {
global $post;
$args[2] = get_post_meta($post->ID,'sp' );
$label_format =
'<label style="font-weight:bold;display:none;" for="%1$s">%2$s &nbsp;&nbsp;</label>'
. '<textarea style="width: 422px;display: inline-block;" name="%1$s">%3$s</textarea>';
return vsprintf( $label_format,$args );
}
function fetch ( $args ) {
global $post;
$args[2] = get_post_meta($post->ID,'sp' );
$label_format = '<form id="fetch_details_form"><h2 style="font-weight: 600;padding: 8px 12px;margin-left:-10px;border-bottom: 1px solid #eee;"><span>Generator %3$s</span></h2><input type="text" id="term" class="textInput" autocomplete="off" placeholder="Enter the title...."><div id="major-publishing-actions" style="overflow:hidden">
<div id="publishing-action" class="fetch-details"><span style="float:left;position:relative;top:3px">&nbsp;&nbsp;&nbsp;&nbsp;<span id="hideme"><input type="radio" name="test" value="movie" required checked> Movie&nbsp;&nbsp;&nbsp;&nbsp;<input id="TV" type="radio" name="test" value="tv"><span id="TV"> TV Show &nbsp;&nbsp;&nbsp;&nbsp;</span><span id="movieform"><input title="%2$sm" placeholder="IMDb ID" style="text-align:left; height: 28px;display: inline-block;" type="text" id="%1$sm" name="%1$sm" value="%3$s" />&nbsp;<input type="button" class="button button-primary %1$smovie" style="cursor:pointer;display: inline-block;" id="%1$smovie" name="%1$smovie" value="%1$smovie" /></span> &nbsp;&nbsp;<span id="tvform" class="hide"><input title="%2$st" placeholder="TMDB ID" style="text-align:left; height: 28px;display: inline-block;" type="text" id="%1$st" name="%1$st" value="%3$s" />&nbsp;<input type="button" id="%1$stv" class="button button-primary %1$stv" style="cursor:pointer;display: inline-block;" name="%1$stv" value="%1$stv" /></span></span></span><span id="publishme"></span><span style="float:right;position:relative;margin-right:30px;" id="api_status"><i class="fa fa-circle" style="color: #ff602acc;" aria-hidden="true"></i>&nbsp;&nbsp;API is online</span><div id="message"></div></div></div><ul id="pagination"></ul></form>';
return vsprintf( $label_format,$args );
}
function inizio_1 ( $args ) {
global $post;
$args = "";
$label_format = '<div id="wid">';
return vsprintf( $label_format,$args );
}
function fine_1 ( $args ) {
global $post;
$args = "";
$label_format = '</div>';
return vsprintf( $label_format,$args );
}
function spazio_1 ( $args ) {
global $post;
$args = "";
$label_format = '<span style="padding:20px;">&nbsp;</span>';
return vsprintf( $label_format,$args );
}
function tv_desc ( $args ) {
global $post;
$args = "";
$label_format = '<span style="font-size: 12px;color: #777;background-color: #fff;">&nbsp;<b style="color: #00A0D2;">TMDB</b>&nbsp;&#8594;  https://themoviedb.org/tv/1399/ &nbsp;&#8594; <b>1399</b></span><br /><br />';
return vsprintf( $label_format,$args );
}
function movie_desc ( $args ) {
global $post;
$args = "";
$label_format = '<span style="font-size: 12px;color: #777;background-color: #fff;">&nbsp;<b style="color: #00A0D2;">IMDB</b>&nbsp;&#8594;  https://imdb.com/title/tt0451279/ &nbsp;&#8594; <b>tt0451279</b></span><br /><br />';
return vsprintf( $label_format,$args );
}
function player_desc ( $args ) {
global $post;
$args = "";
$label_format = '<br /><span style="font-size: 12px;color: #777;margin-top:10px;background-color: #fff;">&#8594; Enter the video host iframe url in this order <b>720p > 1080p</b> (don\'t leave the first field blank). <br>&#8594; If you don\'t want to activate the quality selector enter only 1 link (in any resolution you want) in the first</a> field.<br />&#8594; If you want to activate the download insert the link in the third field,leave blank to disable it.</span>';
return vsprintf( $label_format,$args );
}
function gen_desc ( $args ) {
global $post;
$args = "";
$label_format = '<span style="color: #777;font-size: 12px;margin-top:5px;background-color: #fff;">Enter the IMDB id in the first field to generate a movie,or enter the TMDB id in the second field if you want to generate a tv show.<br></span><br />';
return vsprintf( $label_format,$args );
}
function sp_save_postdata($post_id,$post) {
global $sp_Boxes;
if ( ! wp_verify_nonce( $_POST['sp_nonce_name'],plugin_basename(__FILE__) ) ) {
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; }
foreach ( $sp_Boxes as $sp_Box ) {
foreach ( $sp_Box as $sp_fields ) {
$my_data[$sp_fields[0]] =  $_POST[$sp_fields[0]];
} }
foreach ($my_data as $key => $value) {
if ( 'revision' == $post->post_type  ) {
return; }
$value = implode(',',(array)$value);
if ( get_post_meta($post->ID,$key,FALSE) ) {
update_post_Meta($post->ID,$value);
} else {
add_post_Meta($post->ID,$value);
}if (!$value) {
delete_post_Meta($post->ID,$key);
} } }
function echo_sp_nonce () {
echo sprintf(
'<input type="hidden" name="%1$s" id="%1$s" value="%2$s" />','sp_nonce_name',wp_create_nonce( plugin_basename(__FILE__) )
);
}if ( !function_exists('get_custom_field') ) {
function get_custom_field($field) {
global $post;
$custom_field = get_post_meta($post->ID,$field,true);
echo $custom_field; } }

如果我在第 71 行和第 176 行之前输入这个错误就会消失

第 71 行

if ( ! isset($args[2])) {
   $args[2] = null;
}

第 176 行

if(!empty($_POST[$sp_fields[0]]))
if($_POST[$sp_fields[0]]!="")

但是如果我尝试更新该字段并保存帖子,该字段不会更新

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)