【问题标题】:Using JSON to update a custom post type in Wordpress使用 JSON 更新 Wordpress 中的自定义帖子类型
【发布时间】:2012-01-24 13:45:43
【问题描述】:

我有一个自定义帖子类型,我们称之为产品。当用户将此产品拖到购物车(可放置的 jQuery UI)时,我希望自定义帖子类型中名为“金额”的键减少 1。

到目前为止,我通过 jQuery $.ajax 有一个 JSON 函数,如下所示:

$.ajax({ url: 'http://localhost:8888/MAMP/nogg/wordpress/wp-content/themes/twentyeleven/functions.php',
    data: { postid: +id },
    type: 'post',
    success: function(output) {
        alert("amount is reduced by 1.");
    }
});

这会将帖子的ID发送到functions.php,然后我用它来获取我的functions.php中的数据

if(isset($_POST['postid']) && !empty($_POST['postid'])) {
    $postid = $_POST['postid'];
    $response = json_decode($postid);
    remove_amount($response);
}

使用 postid 调用函数。

function remove_amount($postid) {
    $amount = get_post_meta($postid, 'amount', true);
    update_post_meta($postid, 'amount', $amount--);
}

这给了我一个 500 错误,我确保它是已发送的正确 ID,并检查了包含密钥(金额)的字段的名称。

那么我的愚蠢自我在这里缺少什么?

【问题讨论】:

    标签: php json wordpress


    【解决方案1】:

    您不需要json_decode $_POST['postid'] 变量。

    $.ajax 方法序列化您的数据对象并在您的请求标头中发送数据,就像普通的POST 一样。 jQuery 不会将 JSON 发送到您的服务器。 (您可以更改您的 ajax 参数以实际发送 JSON,但我不会因为 wordpress 安装而使您的生活复杂化。您使用 $.ajax 的方式很好。)

    试试这样:

    if(isset($_POST['postid']) && !empty($_POST['postid'])) {
        // Make sure you do something in this function to prevent SQL injection.
        remove_amount($_POST['postid']);
    }
    

    另外,您的数据对象中的+id 是什么?这是故意的吗?除此之外,您需要向我们提供导致 HTTP 500 的 PHP 错误。

    【讨论】:

    • 是的,这很好。在来到这里之前,我只是把它扔在那里作为最后的努力。这两种方式都行不通。感谢您的回答,澄清了何时以及为什么使用 json_decode! :)
    • 啊,是的!我可以查看 php_error.log 以了解详细信息,完全间隔在那里。错误日志显示:PHP 致命错误:在第 706 行的 /Applications/MAMP/bin/mamp/nogg/wordpress/wp-content/themes/twentyeleven/functions.php 中调用未定义的函数 get_post_meta(),这完全让我难以置信。 functions.php 中是否有某些东西阻碍了它处理 json?我应该尝试用该函数加载一个新的 .php 文件吗?
    【解决方案2】:

    如果您已将 $single 参数设置为 true,get_post_meta 将返回一个字符串。

    那么,您的错误是否与尝试减少字符串值有关?

    在递减之前将您的金额 val 转换为 int 怎么样?

    function remove_amount($postid) {
        (int)$amount = get_post_meta($postid, 'amount', true);
        update_post_meta($postid, 'amount', $amount--);
    }
    

    您收到的错误消息行 (706) 是否与您正在处理更新元数据的行相对应?

    【讨论】:

      【解决方案3】:

      好的,我解决了。显然 WP 函数文件中有一些内容不赞成处理这样的 json 内容。因此无法识别标准的 WP 功能(如 get_post_meta),我所做的是创建一个空白页面,让它使用带有 php 代码的自定义模板,然后在我链接到该 WP 页面的 jquery 代码中。

      $.ajax({ url: 'http://localhost:8888/MAMP/nogg/wordpress/?page_id=43',
                     data: {postid2: id },
                     type: 'post',
                     success: function(output) {
            }
      });
      

      并且 page_id=43 是使用以下模板的页面:

      <?php
      /**
      * Template Name: ajax template
      * Description: ajax *
      * @package WordPress
      */
      
      
      if(isset($_POST['postid']) && !empty($_POST['postid'])) {
      remove_amount($_POST['postid']);
      }
      
      function remove_amount($postid) {
      $amount = get_post_meta($postid, 'amount', true);
      if($amount > 0):
          update_post_meta($postid, 'amount', $amount-1);
          echo $amount-1;
      endif;
      }
      

      现在代码可以正常运行了,现在我只需要按照斯蒂芬所说的添加一些 sql-injection 保护即可。感谢您的回答!让我朝着正确的方向前进!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        相关资源
        最近更新 更多