0

I'm using a PHP code to save data from TMDB on wordpress metabox fields, every time i create a new article the log gives me many errors:

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

This is the code i use:

$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, $args[0], true);
$args[1] = __($args[1], '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, $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>'
. '<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, $args[0], true);
$args[1] = __($args[1], '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, $key, $value);
} else {
add_post_meta($post->ID, $key, $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; } }

The error disappears if I enter this just before line 71 and 176

line 71

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

line 176

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

but if I try to update the field and save the post the field does not update

pippo
  • 3
  • 3

0 Answers0