title: How to change a Wordpress core DB function without editing core files?
body: <p>I have been fighting with this problem for several days now &amp; I would appreciate input from (PHP / Wordpress) developers that are more experienced than me.</p>
  
  <p>I'm making a debugging plugin for Wordpress, but one of the errors that I need to change comes from this file:
  <a href="https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/wp-db.php#L1344" rel="nofollow noreferrer">/site/public/wp-includes/wp-db.php</a></p>
  
  <p>I need to change the structure and add more information to the <code>$error_str</code> in lines 1363 and 1366 so the output better suits our log reader and contains more useful information.</p>
  
  <p>To do that I think I need to change the <code>print_error()</code> in line 1344</p>
  
  <p><strong>The problems are:</strong></p>
  
  <ol>
  <li>That the file is a core file in Wordpress, so I can't edit it directly.</li>
  <li>It also have a global <code>$wpdb</code> variable that is used to call: <code>$wpdb-&gt;query()</code> &amp; <code>$wpdb-&gt;get_row()</code> (also core functions in same file &amp; class) that both in turn calls the <code>print_error()</code> function.</li>
  <li>Other plugins are using these functions &amp; I can't mess up their functionality but I should change their output too.</li>
  </ol>
  
  <p>I think the most likely solution is to <code>inherit &amp; override</code> the <code>print_error()</code> (and maybe also <code>query()</code> &amp; <code>get_row()</code> so I can re-direct them to my new <code>print_error()</code>), but other plugins are using the <code>$wpdb</code> &amp; <code>those functions</code>, I can (should) change the <code>error_log()</code> output for those plugins too, but I can't edit those plugins either &amp; I can't mess them up with a faulty solution.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li>How can I replace the <code>print_error()</code>?</li>
  <li>What is the best way to do this?  </li>
  <li>How do I deal with the global <code>$wpdb</code> variable?</li>
  <li>Do any of you have a better solution? I would very much appreciate it.</li>
  </ul>
  
  <p>Link to the <code>print_error()</code> documentation (with source code):
  <a href="https://developer.wordpress.org/reference/classes/wpdb/print_error/" rel="nofollow noreferrer">https://developer.wordpress.org/reference/classes/wpdb/print_error/</a></p>
  
  <p>Link directly to the source code: <a href="https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/wp-db.php#L1344" rel="nofollow noreferrer">https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/wp-db.php#L1344</a></p>