【问题标题】:Problem saving HTML link inside Wordpress wp_options table在 Wordpress wp_options 表中保存 HTML 链接时出现问题
【发布时间】:2020-10-02 09:24:11
【问题描述】:

我正在使用 textarea 字段和函数为每个 wordpress tag 保存一些额外信息:

这是textarea 字段(使用stripslashes):

<textarea name="Tag_meta[related_links]" id="Tag_meta[related_links]" size="25" placeholder="enter here the links html"><?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links'] : ''); ?></textarea>

这是保存功能:

add_action ( 'edit_term', 'save_termmeta_tag');
   // save extra category extra fields callback function
function save_termmeta_tag( $term_id ) {
    if ( isset( $_POST['Tag_meta'] ) ) {
        $t_id = $term_id;
        $tag_meta = get_option( "tag_$t_id");
        $tag_keys = array_keys($_POST['Tag_meta']);
            foreach ($tag_keys as $key){
            if (isset($_POST['Tag_meta'][$key])){
                $tag_meta[$key] = $_POST['Tag_meta'][$key];
            }
        }
        //save the option array
        update_option( "tag_$t_id", $tag_meta );
    }
}

问题是在其中一个保存的字段中(在wp_options 表中),我有一点HTML,问题在于link 里面的@987654331 @。

正如您在下面的示例中看到的,这是在textarea 字段中输入的HTML(在保存表单之前):

<li><a rel="nofollow" target="_blank" href="https://www.blablabla.com/the-rest-of-the-link">link</a></li>

这是它在wp_options 表中的保存方式:

<li><a rel=\"nofollow\" target=\"_blank\" href=\"https://www.blablabla.com/the-rest-of-the-link\">link</a></li>

请注意:每次我保存选项时,保存过程也会广告额外的\。例如:

保存一个

<li><a rel=\"nofollow\" target=\"_blank\" href=\"https://www.blablabla.com/the-rest-of-the-link\">link</a></li>

保存两个

<li><a rel=\\\"nofollow\\\" target=\\\"_blank\\\" href=\\\"https://www.blablabla.com/the-rest-of-the-link\\\">link</a></li>

等等。

这就是它的输出方式(echo):

http://www.mydomainname.com/https://www.blablabla.com/the-rest-of-the-link\"

这里可能有什么问题?

【问题讨论】:

  • 您是否尝试过在保存时使用htmlentities() (php.net/manual/en/function.htmlentities.php)?
  • @Bossman 试过了!谢谢你的建议。不幸的是它也不起作用。但是...我应该在 &lt;textarea 中使用 htmlentities() 保存 function OR 替换 stripslashes() 吗?
  • 保存时尝试:update_option('your_option', htmlentities(stripslashes($_POST['tag_meta'])));。阅读时尝试使用html_entity_decode(get_option('your_option'));
  • @Bossman 我没有尝试过你的建议,但它让我思考,我想知道你对这个工作解决方案的看法:我在 textarea 上替换它:@ 987654349@ 这个人&lt;?php echo stripslashes($tag_meta['related_links'] ? stripslashes($tag_meta['related_links']) : ''); ?&gt; echo 我做了这个echo stripslashes($tag_meta['related_links']); 它有效,但我想知道是否可以这样做...再次感谢。

标签: php wordpress textarea


【解决方案1】:

工作解决方案:

在 textarea 上替换它

&lt;?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links'] : ''); ?&gt;

由此

&lt;?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links']) : ''; ?&gt;

echo 上这样做:

echo stripslashes($tag_meta['related_links']);

已编辑

@bossman的巨大贡献更正答案

【讨论】:

  • 我现在明白了。这是因为它是一个三元运算符。你不需要 stripslashes() 在条件 echo $tag_meta['related_links'] ? stripslashes($tag_meta['related_links']) : ''); 将工作。您基本上是在问一个问题,如果 $tag_meta['related_links'] 评估为 true 则执行此操作,否则执行此操作。 $result = $condition ? 'foo' : 'bar';
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
相关资源
最近更新 更多