Mobile app version of vmapp.org
Login or Join
Margaret670

: How to fix two PHP errors I cannot get the plugin author to answer I am the administrator of my website and when I turn debug on to track down a problem on the site, the debug.log is cluttered

@Margaret670

Posted in: #Php #Wordpress

I am the administrator of my website and when I turn debug on to track down a problem on the site, the debug.log is cluttered with hundreds of lines of PHP notices each day about two PHP problems in a plugin, which hides the debug information I am looking for. I tried contacting the author of the plugin to get a fix so I can stop the buildup of the log but he hasn't responded to questions in the forum for the plugin.

What do I need to do to fix these two errors in the WordPress plugin so they stop adding hundreds of PHP notices in the debug.log?


Trying to get property of non-object in /plugins/custom-author-byline/custom-author-byline.php on line 27


The code on line 27 is

$custom_author_uri = get_post_meta($post->ID, 'uri', TRUE);



Undefined index: author_noncename in /plugins/custom-author-byline/custom-author-byline.php on line 80


The code on line 80 is

if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {


I am asking this question here because 8 months ago I asked about how to fix these errors in the support forum for the plugin. No one has responded and looking at the support forum, some questions have suggested answers or more comments by other users but nothing from the author for over 6 years.

I hope someone here can help me with this.

Additional information:
The full code around line 27 is the following function.

// Replaces the_author_link() output with your custom entry or return the logged in user if there is no custom entry
function custom_author_uri( $author_uri ) {
//global $authordata;
global $post, $authordata;
$custom_author_uri = get_post_meta($post->ID, 'uri', TRUE);
if($custom_author_uri)
return $custom_author_uri;
return $author_uri;
}
add_filter( 'author_link', 'custom_author_uri' );


The full code involving line 80 is the following function.

function cab_save_postdata( $post_id ) {
global $post, $cab_new_meta_boxes;

foreach($cab_new_meta_boxes as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], 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;
}

$data = $_POST[$meta_box['name']];

if(get_post_meta($post_id, $meta_box['name']) == "")
add_post_meta($post_id, $meta_box['name'], $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'], true))
update_post_meta($post_id, $meta_box['name'], $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
}
}
add_action('admin_menu', 'cab_create_meta_box');
add_action('save_post', 'cab_save_postdata');

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Margaret670

1 Comments

Sorted by latest first Latest Oldest Best

 

@Shakeerah822

Since your "code works OK" and these are just E_NOTICE messages (as opposed to warnings or errors) then you should be able to modify your code like the following in order to workaround these messages:


Trying to get property of non-object...


We need to check that the $post variable is of the expected type before attempting to process it.

// Replaces the_author_link() output with your custom entry or return the logged in user if there is no custom entry
function custom_author_uri( $author_uri ) {
//global $authordata;
global $post, $authordata;
if (is_object($post) && property_exists($post,'ID')) {
$custom_author_uri = get_post_meta($post->ID, 'uri', TRUE);
if ($custom_author_uri) {
return $custom_author_uri;
}
}
return $author_uri;
}
add_filter( 'author_link', 'custom_author_uri' );



Undefined index: author_noncename


function cab_save_postdata( $post_id ) {
global $post, $cab_new_meta_boxes;

foreach($cab_new_meta_boxes as $meta_box) {
if (empty($_POST[$meta_box['name'].'_noncename'])) {
return $post_id;
}
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], 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;
}

$data = $_POST[$meta_box['name']];

if(get_post_meta($post_id, $meta_box['name']) == "")
add_post_meta($post_id, $meta_box['name'], $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'], true))
update_post_meta($post_id, $meta_box['name'], $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
}
}
add_action('admin_menu', 'cab_create_meta_box');
add_action('save_post', 'cab_save_postdata');


Maybe you could fail sooner in this second function - but without knowing the code, that is difficult to say. You may need to add further checks if you are still getting "Undefined index" messages.

(Although the nagging thought in the back of my mind is why these functions are being called at all in such circumstances?)

These changes shouldn't make the code run any differently (since you said it "works OK") - it simply avoids the nagging E_NOTICE message(s), assuming that these conditions are normal and expected.

As suggested in your other question, why you are getting these E_NOTICE messages now may simply be a difference in the default error_reporting level after updating to PHP 7. (?)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme