如何从Wordpress中的php文件打印到控制台

gra*_*ury 3 php wordpress

我有一个php文件,它是wordpress插件的一部分。我需要调试一个问题。我想找出一个变量的值是什么。如何将变量的值打印到控制台?已建议使用echo或chrome或firefox扩展名。我无法获得回显以输出到控制台(echo “$variablename";),也无法使用firephp扩展名firefox。

Jos*_*ost 6

要回答您的问题,您可以执行以下操作:

echo '<script>console.log("PHP error: ' . $error . '")</script>';
Run Code Online (Sandbox Code Playgroud)

但我建议改用@Ishas建议的方法之一。确保$error不包含任何可能使脚本混乱的内容。


mod*_*diX 6

从 WordPress 中的 PHP 登录到 DevTools 控制台

\n

使用 Firefox DevTools 中的 console_log 调试 WordPress 中的 WooCommerce

\n

在这里,您可以看到我在 WooCommerce 中调试优惠券逻辑时针对实际问题的解决方案。该解决方案仅用于调试目的。(注:截图不是最新的,它也会暴露私人成员。)

\n

特征

\n
    \n
  • 允许在渲染开始之前和之后打印
  • \n
  • 在前端和后端工作
  • \n
  • 打印任意数量的变量
  • \n
  • 对数组和对象进行编码
  • \n
  • 公开对象的私有成员和受保护成员
  • \n
  • 也记录到日志文件中
  • \n
  • 在生产环境中安全、轻松地选择退出(如果您保留通话)
  • \n
  • 打印调用者类、函数和钩子(生活质量提高)
  • \n
\n

解决方案

\n

wp-debug.php

\n
function console_log(): string {\n    list( , $caller ) = debug_backtrace( false );\n    $action = current_action();\n    $encoded_args = [];\n    foreach ( func_get_args() as $arg ) try {\n        if ( is_object( $arg ) ) {\n            $extract_props = function( $obj ) use ( &$extract_props ): array {\n                $members = [];\n                $class = get_class( $obj );\n                foreach ( ( new ReflectionClass( $class ) )->getProperties() as $prop ) {\n                    $prop->setAccessible( true );\n                    $name = $prop->getName();\n                    if ( isset( $obj->{$name} ) ) {\n                        $value = $prop->getValue( $obj );\n                        if ( is_array( $value ) ) {\n                            $members[$name] = [];\n                            foreach ( $value as $item ) {\n                                if ( is_object( $item ) ) {\n                                    $itemArray = $extract_props( $item );\n                                    $members[$name][] = $itemArray;\n                                } else {\n                                    $members[$name][] = $item;\n                                }\n                            }\n                        } else if ( is_object( $value ) ) {\n                            $members[$name] = $extract_props( $value );\n                        } else $members[$name] = $value;\n                    }\n                }\n                return $members;\n            };\n\n            $encoded_args[] = json_encode( $extract_props( $arg ) );\n        } else {\n            $encoded_args[] = json_encode( $arg );\n        }\n    } catch ( Exception $ex ) {\n        $encoded_args[] = \'`\' . print_r( $arg, true ) . \'`\';\n    }\n    $msg = \'``, `\'\n        . ( array_key_exists( \'class\', $caller ) ? $caller[\'class\'] : "\\x3croot\\x3e" )\n        . \'\\\\\\\\\'\n        . $caller[\'function\'] . \'()`, \'\n        . ( strlen( $action ) > 0 ? \'``, `\' . $action . \'`, \' : \'\' )\n        . \'` \xe2\x9e\xa1\xef\xb8\x8f `, \' . implode( \', \', $encoded_args );\n    $html = \'<script type="text/javascript">console.log(\' . $msg . \')</script>\';\n    add_action( \'wp_enqueue_scripts\', function() use ( $html ) {\n        echo $html;\n    } );\n    add_action( \'admin_enqueue_scripts\', function() use ( $html ) {\n        echo $html;\n    } );\n    error_log( $msg );\n    return $html;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

wp-config.php(部分)

\n
// ...\n\ndefine( \'WP_DEBUG\', true );\n\n// ...\n\n/** Include WP debug helper */\nif ( defined( \'WP_DEBUG\' ) && WP_DEBUG && file_exists( ABSPATH . \'wp-debug.php\' ) ) {\n    include_once ABSPATH . \'wp-debug.php\';\n}\nif ( ! function_exists( \'console_log\' ) ) {\n    function console_log() {\n    }\n}\n\n/** Sets up WordPress vars and included files. */\nrequire_once( ABSPATH . \'wp-settings.php\' );\n
Run Code Online (Sandbox Code Playgroud)\n

用法

\n
    \n
  • <head>在渲染HTML 之前:
  • \n
\n
console_log( $myObj, $myArray, 123, "test" );\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • HTML<head>渲染后(在模板等中/当上述不起作用时使用):
  • \n
\n
echo console_log( $myObj, $myArray, 123, "test" );\n
Run Code Online (Sandbox Code Playgroud)\n

输出格式

\n
 <caller class>\\<caller function>()  <caller action/hook>  \xe2\x9e\xa1\xef\xb8\x8f  <variables ...>\n
Run Code Online (Sandbox Code Playgroud)\n

特别感谢

\n\n


lsh*_*has 5

如果您正在考虑使用JavaScript控制台,则无法通过PHP执行此操作。

您可以选择以下几种方式:

为了快速检查变量值,我将使用var_dump,它还会向您显示变量的数据类型。当您请求页面时,它将输出到浏览器。