Your Location is: Home > Php

Problem saving HTML link inside Wordpress wp_options table

From: Sao View: 3319 bpy 

Question

I'm saving some extra information on each wordpress tag using an textarea field and a function:

This is the textarea field (using 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>

This is the saving function:

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 );
    }
}

The problem is that in one of those saved fields (in wp_options table), I have a bit of HTML and the problem resides on the link inside of that HTML.

As you can see on the example bellow, this is the HTML entered on the textarea field (before saving the form):

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

This is the way it's being saved in wp_options table:

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

Please note: The saving process also ads extra's \ each time I save the options. eg:

Saving one:

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

Saving two:

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

etc..

And this is the way it's being outputed (by the echo):

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

What might be wrong here?

Best answer

Working solution:

Replace this on the textarea:

<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links'] : ''); ?>

by this:

<?php echo stripslashes($tag_meta['related_links'] ? $tag_meta['related_links']) : ''; ?>

and on the echo do this:

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

Edited:

Answer corrected with great contribution of @bossman