|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: PHP Memory Info |
| 4 | + * Description: Adds a Tools -> PHP Memory page to display PHP ini and WordPress memory settings and current usage. |
| 5 | + * Version: 1.0 |
| 6 | + * Author: ManagingWP |
| 7 | + * Author URI: https://github.com/managingwp/wordpress-code-snippets |
| 8 | + * Type: mu-plugin |
| 9 | + * Status: Complete |
| 10 | +*/ |
| 11 | + |
| 12 | +// Exit if accessed directly |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { |
| 14 | + exit; |
| 15 | +} |
| 16 | + |
| 17 | +// Hook into admin menu to add the Tools > PHP Memory page |
| 18 | +add_action( 'admin_menu', 'pm_add_php_memory_page' ); |
| 19 | + |
| 20 | +/** |
| 21 | + * Register the PHP Memory page under Tools |
| 22 | + */ |
| 23 | +function pm_add_php_memory_page() { |
| 24 | + add_management_page( |
| 25 | + 'PHP Memory', // Page title |
| 26 | + 'PHP Memory', // Menu title |
| 27 | + 'manage_options', // Capability |
| 28 | + 'php-memory-info', // Menu slug |
| 29 | + 'pm_render_php_memory_page' // Callback to render the page |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Render the PHP Memory Info page |
| 35 | + */ |
| 36 | +function pm_render_php_memory_page() { |
| 37 | + // Check permissions |
| 38 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // PHP ini settings |
| 43 | + $ini_settings = array( |
| 44 | + 'file_uploads' => ini_get( 'file_uploads' ), |
| 45 | + 'upload_max_filesize' => ini_get( 'upload_max_filesize' ), |
| 46 | + 'post_max_size' => ini_get( 'post_max_size' ), |
| 47 | + 'memory_limit' => ini_get( 'memory_limit' ), |
| 48 | + 'max_execution_time' => ini_get( 'max_execution_time' ), |
| 49 | + 'max_input_time' => ini_get( 'max_input_time' ), |
| 50 | + ); |
| 51 | + |
| 52 | + // WordPress memory settings |
| 53 | + $wp_memory_limit = defined( 'WP_MEMORY_LIMIT' ) ? WP_MEMORY_LIMIT : 'Not defined'; |
| 54 | + $wp_max_memory_limit = defined( 'WP_MAX_MEMORY_LIMIT' ) ? WP_MAX_MEMORY_LIMIT : 'Not defined'; |
| 55 | + |
| 56 | + // Current memory usage in MB |
| 57 | + $current_memory_usage = round( memory_get_usage() / 1024 / 1024, 2 ); |
| 58 | + |
| 59 | + // Output |
| 60 | + echo '<div class="wrap">'; |
| 61 | + echo '<h1>PHP Memory Info</h1>'; |
| 62 | + |
| 63 | + echo '<h2>PHP ini settings for file uploads</h2>'; |
| 64 | + echo '<table class="widefat fixed"><thead><tr><th>Setting</th><th>Value</th></tr></thead><tbody>'; |
| 65 | + foreach ( $ini_settings as $key => $value ) { |
| 66 | + echo '<tr><td>' . esc_html( $key ) . '</td><td>' . esc_html( $value ) . '</td></tr>'; |
| 67 | + } |
| 68 | + echo '</tbody></table>'; |
| 69 | + |
| 70 | + echo '<h2>WordPress Memory Settings</h2>'; |
| 71 | + echo '<table class="widefat fixed"><thead><tr><th>Setting</th><th>Value</th></tr></thead><tbody>'; |
| 72 | + echo '<tr><td>WP_MEMORY_LIMIT</td><td>' . esc_html( $wp_memory_limit ) . '</td></tr>'; |
| 73 | + echo '<tr><td>WP_MAX_MEMORY_LIMIT</td><td>' . esc_html( $wp_max_memory_limit ) . '</td></tr>'; |
| 74 | + echo '</tbody></table>'; |
| 75 | + |
| 76 | + echo '<h2>What these WordPress settings do</h2>'; |
| 77 | + echo "<p><strong>WP_MEMORY_LIMIT:</strong> The max amount of PHP memory WordPress will try to use for front-end operations (themes, plugins, media processing). It's a soft target—it cannot exceed PHP's own memory_limit in php.ini.</p>"; |
| 78 | + echo "<p><strong>WP_MAX_MEMORY_LIMIT:</strong> A higher memory ceiling for admin tasks: dashboard, cron jobs, updates, and other background processes. Again, bounded by PHP's own memory_limit in php.ini.</p>"; |
| 79 | + |
| 80 | + echo '<h2>Current memory usage</h2>'; |
| 81 | + echo '<p>' . esc_html( $current_memory_usage ) . ' MB</p>'; |
| 82 | + |
| 83 | + echo '</div>'; |
| 84 | +} |
0 commit comments