在 Wordpress wp_options table 中保存 HTML link 时出现问题
Problem saving HTML link inside Wordpress wp_options table
我正在使用 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
table 中),我有一些 HTML
和问题出在 link
里面 HTML
.
正如您在下面的示例中看到的,这是在 textarea
字段中输入的 HTML
(在保存表单之前):
<li><a rel="nofollow" target="_blank" href="https://www.blablabla.com/the-rest-of-the-link">link</a></li>
这是它在 wp_options
table 中的保存方式:
<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\"
这里可能有什么问题?
工作解决方案:
在textarea上替换这个:
<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links'] : ''); ?>
通过这个:
<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links']) : ''; ?>
并在 echo
上执行此操作:
echo stripslashes($tag_meta['related_links']);
已编辑:
答案更正 @bossman
的巨大贡献
我正在使用 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
table 中),我有一些 HTML
和问题出在 link
里面 HTML
.
正如您在下面的示例中看到的,这是在 textarea
字段中输入的 HTML
(在保存表单之前):
<li><a rel="nofollow" target="_blank" href="https://www.blablabla.com/the-rest-of-the-link">link</a></li>
这是它在 wp_options
table 中的保存方式:
<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\"
这里可能有什么问题?
工作解决方案:
在textarea上替换这个:
<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links'] : ''); ?>
通过这个:
<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links']) : ''; ?>
并在 echo
上执行此操作:
echo stripslashes($tag_meta['related_links']);
已编辑:
答案更正 @bossman
的巨大贡献