【发布时间】: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 试过了!谢谢你的建议。不幸的是它也不起作用。但是...我应该在
<textarea中使用htmlentities()保存functionOR 替换stripslashes()吗? -
保存时尝试:
update_option('your_option', htmlentities(stripslashes($_POST['tag_meta'])));。阅读时尝试使用html_entity_decode(get_option('your_option'));。 -
@Bossman 我没有尝试过你的建议,但它让我思考,我想知道你对这个工作解决方案的看法:我在 textarea 上替换它:@ 987654349@ 这个人:
<?php echo stripslashes($tag_meta['related_links'] ? stripslashes($tag_meta['related_links']) : ''); ?>在echo我做了这个:echo stripslashes($tag_meta['related_links']);它有效,但我想知道是否可以这样做...再次感谢。