douni1396 2010-10-28 14:43
浏览 9

如何使用str_ireplace()在发布/保存例程期间过滤帖子内容?

I'm trying to create a function that does a text replacement on the post content when its saved (the_content).

The stub function is below, but how do I obtain a reference to the post content, then return the filtered content back to the "publish_post" routine?

However, my replacement is either not working and/or not passing the updated post_content to the publish function. The values never get replaced.

function my_function() {
    global $post;
    $the_content = $post->post_content;
    $text = " test ";
    $post->post_content = str_ireplace($text, '<b>'.$text.'</b>', $the_content  );
    return $post->post_content;
    }
add_action('publish_post', 'my_function');
  • 写回答

3条回答 默认 最新

  • doutuan4361 2010-10-28 15:07
    关注

    Probably easier to do something like this:

    function my_function($post) {
      $content = str_ireplace(" test ", '<b>'.$text.'</b>', $post->content);
    
      return $content;
    }
    

    Less focus was on the interior of the function, but the idea is that you pass the object to the function in the () then call the directly rather than globalizing the value. It should be more direct that way.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题