Title: [PATCH] undefined variable warning
Last modified: August 21, 2016

---

# [PATCH] undefined variable warning

 *  [flynsarmy](https://wordpress.org/support/users/flynsarmy/)
 * (@flynsarmy)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/)
 * > Notice: Undefined property: stdClass::$id in /path/to/wp-content/plugins/threewp-
   > broadcast/include/threewp_broadcast/BroadcastData.php on line 54
 * In the sql() method change
 *     ```
       $bcd->id = $result->id;
       ```
   
 * to
 *     ```
       if ( !empty($result->id) )
       	$bcd->id = $result->id;
       ```
   
 * A bit of background on this issue: The method is called by _sql\_get\_broadcast\
   _datas()_ in _ThreeWP\_Broadcast.php_ with:
 *     ```
       $query = sprintf( "SELECT * FROM<code>%s</code>WHERE<code>blog_id</code>= '%s' AND<code>post_id</code> IN ('%s')",
       	$this->broadcast_data_table(),
       	$blog_id,
       	implode( "', '", $post_ids )
       );
       $results = $this->query( $query );
       foreach( $results as $index => $result )
       	$results[ $index ][ 'data' ] = BroadcastData::sql( $result );
       ```
   
 * Problem is that the broadcast_data_table doesn’t have an ID column, so in your
   sql() method the line `$bcd->id = $result->id;` will never actually work.
 * [http://wordpress.org/plugins/threewp-broadcast/](http://wordpress.org/plugins/threewp-broadcast/)

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

 *  Thread Starter [flynsarmy](https://wordpress.org/support/users/flynsarmy/)
 * (@flynsarmy)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373602)
 * Another. This time when broadcasting:
 * > Undefined property: threewp_broadcast\broadcasting_data::$attachment_id in /
   > path/to/wp-content/plugins/threewp-broadcast/ThreeWP_Broadcast.php on line 
   > 2131
 * This line is the culprit:
 *     ```
       $a->new = get_post( $o->attachment_id );
       ```
   
 * in this block:
 *     ```
       foreach( $bcd->attachment_data as $key => $attachment )
       {
       	if ( $key != 'thumbnail' )
       	{
       		$o = clone( $bcd );
       		$o->attachment_data = $attachment;
       		if ( $o->attachment_data->post->post_parent > 0 )
       			$o->attachment_data->post->post_parent = $bcd->new_post[ 'ID' ];
       		$this->maybe_copy_attachment( $o );
       		$a = new \stdClass();
       		$a->old = $attachment;
       		if ( !empty($o->attachment_id) )
       			$a->new = get_post( $o->attachment_id );
       		else
       			$a->new = null;
       		$bcd->copied_attachments[] = $a;
       	}
       }
       ```
   
 * Now I haven’t dug deep enough to figure out exactly what’s going on here. But
   I guess your options are to either it to:
 *     ```
       if ( !empty($o->attachment_id) )
       	$a->new = get_post( $o->attachment_id );
       else
       	$a->new = null;
       ```
   
 * which then presents a problem in that it leads to a few more issues where you’re
   calling `->new->guid` which doesn’t exist…
 * Line 2148 change
 *     ```
       foreach( $bcd->copied_attachments as $a )
       {
       	// Replace the GUID with the new one.
       	$modified_post->post_content = str_replace( $a->old->guid, $a->new->guid, $modified_post->post_content );
       	// And replace the IDs present in any image captions.
       	$modified_post->post_content = str_replace( 'id="attachment_' . $a->old->id . '"', 'id="attachment_' . $a->new->id . '"', $modified_post->post_content );
       }
       ```
   
 * to
 *     ```
       foreach( $bcd->copied_attachments as $a )
       {
       	if ( !$a->new )
       		continue;
   
       	// Replace the GUID with the new one.
       	$modified_post->post_content = str_replace( $a->old->guid, $a->new->guid, $modified_post->post_content );
       	// And replace the IDs present in any image captions.
       	$modified_post->post_content = str_replace( 'id="attachment_' . $a->old->id . '"', 'id="attachment_' . $a->new->id . '"', $modified_post->post_content );
       }
       ```
   
 * and line 2171ish change
 *     ```
       foreach( $bcd->copied_attachments as $ca )
       {
       	if ( $ca->old->id != $id )
       		continue;
       	$new_ids[] = $ca->new->ID;
       }
       ```
   
 * to
 *     ```
       foreach( $bcd->copied_attachments as $ca )
       {
       	if ( $ca->new && $ca->old->id != $id )
       		continue;
       	$new_ids[] = $ca->new->ID;
       }
       ```
   
 * ———-
 * Alternatively you could simply not add _$a_ to the _copied\_attachments_ list
   if it doesn’t exist. Back up at line 2120 change
 *     ```
       $bcd->copied_attachments[] = $a;
       ```
   
 * to
 *     ```
       if ( $a->new )
       	$bcd->copied_attachments[] = $a;
       ```
   
 * Please enable WP_DEBUG to see these errors.
 *  Thread Starter [flynsarmy](https://wordpress.org/support/users/flynsarmy/)
 * (@flynsarmy)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373606)
 * Finally when redirecting after submitting a broadcasted post, I get an error 
   about _POST not existing (in /wp-admin/post.php). This is probably caused by 
   you unsetting it. You shouldnt’ do that. Around line 1965 instead of
 *     ```
       // POST is no longer needed. Remove it so that other plugins don't use it.
       unset( $_POST );
       ```
   
 * use
 *     ```
       $_POST = array();
       ```
   
 *  Plugin Author [edward_plainview](https://wordpress.org/support/users/edward_plainview/)
 * (@edward_plainview)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373710)
 * The first error is because the plugin wasn’t properly upgraded. Deactivate and
   reactivate it and the wp__3wp_broadcast_broadcastdata table should receive an
   ID column.
 * I would love to figure out why WordPress’ upgrade function doesn’t call the plugin
   activation method…
 * The second error should never occur, since all attachments that exist in the 
   first post must also exist in the new post. What would be far more interesting
   to find it would be why the new attachment isn’t being copied as it should. THAT
   is the problem.
 * The third error I’ve now fixed. Will release a new version in a day or two.
 * Thank you for your help. Keep it coming.
 *  Thread Starter [flynsarmy](https://wordpress.org/support/users/flynsarmy/)
 * (@flynsarmy)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373755)
 * Thanks for the fix for issues 1 and 3. I deleted and re-added the plugin just
   to make sure all code is synched 100% with yours and there was a fresh activation
   so the table has the id column.
 * As for issue two though, I don’t know what to tell you but it IS happening. I
   have a post with two attachments (both 404ing but I doubt that’d affect attachment_id
   property). I edited around line 2130 to dump the object just before you attempt
   to grab its attachment_id property:
 *     ```
       if ( $key != 'thumbnail' )
       {
       	$o = clone( $bcd );
       	$o->attachment_data = $attachment;
       	if ( $o->attachment_data->post->post_parent > 0 )
       		$o->attachment_data->post->post_parent = $bcd->new_post[ 'ID' ];
       	$this->maybe_copy_attachment( $o );
       	$a = new \stdClass();
       	$a->old = $attachment;
       	if ( empty($o->attachment_id) )
       	{
       		var_dump($o);
       		exit;
       	}
       	$a->new = get_post( $o->attachment_id );
       	$bcd->copied_attachments[] = $a;
       }
       ```
   
 * Here is the output: [http://pastebin.com/BspmvRLt](http://pastebin.com/BspmvRLt)
 * EDIT: Just added `var_dump($key)` above the `var_dump($o)` just incase it helps.
   The result was `int 23406`.
 *  Plugin Author [edward_plainview](https://wordpress.org/support/users/edward_plainview/)
 * (@edward_plainview)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373779)
 * > I have a post with two attachments (both 404ing but I doubt that’d affect attachment_id
   property).
 * Actually, I’m figuring it would. 🙂
 * Attachments, according to WordPress, must exist. It makes no sense to attach 
   files (posts) that don’t exist in the media library.
 * If you test a post with attachments that exist, do you still get the errors?
 *  Thread Starter [flynsarmy](https://wordpress.org/support/users/flynsarmy/)
 * (@flynsarmy)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373780)
 * No, they exist fine in the database. It’s just the files on disk that don’t exist.
   I coudl throw in dummy files with the correct filenames and get the exact same
   error.
 *  Plugin Author [edward_plainview](https://wordpress.org/support/users/edward_plainview/)
 * (@edward_plainview)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373781)
 * In copy_attachment( $o ), at around line 2390 you’ll find:
 * if ( ! file_exists( $o->attachment_data->filename_path ) )
    return false;
 * Meaning that the files must exist on disk. Dummy files would prevent the error
   from happening, I figure.

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

The topic ‘[PATCH] undefined variable warning’ is closed to new replies.

 * ![](https://ps.w.org/threewp-broadcast/assets/icon.svg?rev=1013783)
 * [Broadcast](https://wordpress.org/plugins/threewp-broadcast/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/threewp-broadcast/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/threewp-broadcast/)
 * [Active Topics](https://wordpress.org/support/plugin/threewp-broadcast/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/threewp-broadcast/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/threewp-broadcast/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [edward_plainview](https://wordpress.org/support/users/edward_plainview/)
 * Last activity: [12 years, 5 months ago](https://wordpress.org/support/topic/patch-undefined-variable-warning/#post-4373781)
 * Status: not resolved