You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress页面间传值报错:sizeof()参数需为可数类型

Fixing the sizeof() Warning & Properly Passing Data Between WordPress Pages

Hey there, let's break down how to fix that annoying warning and get your page-to-page data transfer working smoothly.

First, Let's Tackle the sizeof() Warning

That warning is popping up because modern PHP versions (7.2+) require the sizeof() function to be passed an array or an object that implements the Countable interface. The allow-php-in-posts-and-pages plugin you're using has outdated code on line 175 of allowphp.php — it's probably feeding a non-countable variable (like a string or null) into sizeof().

Here are two quick fixes for the warning:

  • Option 1: Update or Replace the Plugin
    This plugin seems unmaintained if it's throwing this error. Check the WordPress plugin repository for an updated version, or switch to a more modern alternative (like using custom page templates instead of embedding PHP directly in posts/pages — it's safer too).
  • Option 2: Manually Patch the Plugin Code
    Access your site's files via FTP or cPanel, navigate to wp-content/plugins/allow-php-in-posts-and-pages/allowphp.php, and find line 175. If the line looks something like $count = sizeof($some_variable);, modify it to handle non-countable values:
    // Replace the original sizeof line with this
    $count = is_countable($some_variable) ? sizeof($some_variable) : 0;
    
    Or force-cast the variable to an array as a fallback:
    $count = sizeof( (array)$some_variable );
    

Now, Let's Get Data Transfer Working Correctly

Instead of relying on that plugin (which poses security risks by letting you embed raw PHP in pages), here are two reliable methods to pass text box values between WordPress pages:

Method 1: Use GET Parameters (Simple, Visible in URL)

This is the easiest approach for basic data transfer:

  1. On your first page (the one with the text box):
    Add a form that submits to your second page via the GET method:
    <form method="get" action="https://your-site.com/second-page-slug/">
      <label for="user-input">Enter your text:</label>
      <input type="text" id="user-input" name="user_text" required>
      <button type="submit">Send to Next Page</button>
    </form>
    
  2. On your second page:
    Retrieve and display the value (make sure to sanitize user input for security):
    <?php
    if (isset($_GET['user_text'])) {
      $user_input = sanitize_text_field($_GET['user_text']);
      echo "<p>You entered: <strong>$user_input</strong></p>";
    } else {
      echo "<p>No input received.</p>";
    }
    ?>
    

Method 2: Use PHP Sessions (Hidden, Better for Sensitive Data)

If you don't want the data to show up in the URL, use sessions:

  1. Enable sessions in your theme:
    Add this code to your theme's functions.php file to start a session when WordPress loads:
    add_action('init', 'start_custom_wordpress_session');
    function start_custom_wordpress_session() {
      if (!session_id()) {
        session_start();
      }
    }
    
  2. Modify the first page's form:
    Use POST instead of GET, and add code to store the input in the session:
    <form method="post" action="https://your-site.com/second-page-slug/">
      <label for="user-input">Enter your text:</label>
      <input type="text" id="user-input" name="user_text" required>
      <button type="submit">Send to Next Page</button>
    </form>
    
    <?php
    // Handle form submission
    if (isset($_POST['user_text'])) {
      $_SESSION['stored_user_input'] = sanitize_text_field($_POST['user_text']);
      wp_redirect(get_permalink(get_page_by_path('second-page-slug')));
      exit;
    }
    ?>
    
  3. Retrieve the value on the second page:
    <?php
    if (isset($_SESSION['stored_user_input'])) {
      $user_input = $_SESSION['stored_user_input'];
      echo "<p>You entered: <strong>$user_input</strong></p>";
      // Clear the session variable after use to avoid reusing it accidentally
      unset($_SESSION['stored_user_input']);
    } else {
      echo "<p>No input received.</p>";
    }
    ?>
    

Important Security Note

Always sanitize user input using WordPress functions like sanitize_text_field(), sanitize_email(), or esc_html() to prevent cross-site scripting (XSS) attacks and other security vulnerabilities.

内容的提问来源于stack exchange,提问作者user3075338

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:15:25